| | |
| | | public static TweenerCore<Vector2, Vector2, DG.Tweening.Plugins.Options.VectorOptions> MoveToTarget( |
| | | RectTransform transform, RectTransform target, Vector2 offset, Action onComplete = null, float speed = 500f) |
| | | { |
| | | // 获取目标节点的世界坐标(锚点位置) |
| | | Vector3 worldPos = target.position; |
| | | |
| | | // 获取目标节点的世界坐标(中心点) |
| | | Vector3 worldPos = target.TransformPoint(target.rect.center + offset); |
| | | // 如果需要加 offset,需考虑 scale |
| | | Vector3 offsetWorld = target.TransformVector(offset); |
| | | worldPos += offsetWorld; |
| | | |
| | | RectTransform canvasRect = transform.parent as RectTransform; |
| | | |
| | |
| | | null, |
| | | out localPoint); |
| | | |
| | | // 创建RawImage |
| | | float distance = Vector2.Distance(transform.anchoredPosition, localPoint); |
| | | |
| | | float duration = distance / speed; // 假设速度为1000单位/秒,可以根据需要调整 |
| | | float duration = distance / speed; |
| | | |
| | | var tween = transform.DOAnchorPos(localPoint, duration).SetEase(Ease.Linear); |
| | | tween.onComplete += () => |
| | | { |
| | | onComplete?.Invoke(); |
| | | }; |
| | | |
| | | // MarkStartAndEnd(transform as RectTransform, target); |
| | | |
| | | |
| | | // // 1. 获取目标的世界坐标(加 offset) |
| | | // Vector3 targetWorldPos = target.TransformPoint(target.anchoredPosition + offset); |
| | | |
| | | // // 2. 获取源节点的 parent |
| | | // RectTransform sourceParent = transform.parent as RectTransform; |
| | | // if (sourceParent == null) |
| | | // { |
| | | // BattleDebug.LogError("源节点没有父节点,无法转换坐标!"); |
| | | // return null; |
| | | // } |
| | | |
| | | // // 3. 把目标世界坐标转换到源 parent 的本地坐标 |
| | | // Vector2 targetAnchoredPos; |
| | | // RectTransformUtility.ScreenPointToLocalPointInRectangle( |
| | | // sourceParent, |
| | | // RectTransformUtility.WorldToScreenPoint(CameraManager.uiCamera, targetWorldPos), |
| | | // CameraManager.uiCamera, |
| | | // out targetAnchoredPos); |
| | | |
| | | // // 4. DOTween 移动 |
| | | // var tween = transform.DOAnchorPos(targetAnchoredPos, duration).SetEase(Ease.Linear); |
| | | // tween.onComplete += () => |
| | | // { |
| | | // onComplete?.Invoke(); |
| | | // }; |
| | | |
| | | return tween; |
| | | } |