画线,如何计算坐标

public void DrawAttackLineByPos(Vector2 from, Vector2 to)
{
Debug.Log("DrawLine from " + from + " to " + to);
var graph = new GGraph
{
name = "AttackLine",
};
graph.SetXY(from.x, from.y);
graph.DrawRect(1, 1, 1, Color.red, Color.red);
var shape = graph.shape;
var line = shape.graphics.GetMeshFactory<LineMesh>();
line.lineWidthCurve = AnimationCurve.Linear(0, 1, 1, 1);
line.roundEdge = true;
//line.gradient = lineGradient;
line.path.Create(new GPathPoint {
new GPathPoint(new Vector3(from.x, from.y, 0)),
new GPathPoint(new Vector3(to.x, to.y, 0))
});
shape.graphics.SetMeshDirty();
GTween.To(0, 1, 3).SetEase(EaseType.Linear).SetTarget(shape.graphics).OnUpdate((GTweener t) =>
{
((NGraphics)t.target).GetMeshFactory<LineMesh>().fillEnd = t.value.x;
((NGraphics)t.target).SetMeshDirty();
});
GTween.To(0, 1, 3).SetEase(EaseType.Linear).SetTarget(shape.graphics).OnComplete((GTweener t) =>
{
//GRoot.inst.RemoveChild(graph);
});
GRoot.inst.AddChild(graph);
}
大佬好:
我现在想从界面的一个组件A向组件B画线,这个坐标要如何计算呢?
现在传入的是2个组件的坐标给Vector2 from, Vector2 to
,但是画线后有明显的偏移,请问这个真正计算的时候包含unity坐标转换吗?
谢谢。
Snipaste_2024-01-06_21-15-51.png
已邀请:

谷主

赞同来自:

from和to你都得转换到groot的坐标系

lazy

赞同来自:

我在lua里面调用,谷主明示哈:

Snipaste_2024-01-08_21-16-56.png

 
要怎么改呢?
 

谷主

赞同来自:

local坐标指的是自身的坐标系的坐标,对fromHeroPanel来说,它的左上角是0,0,不是fromHeroPanel.x/y

lazy

赞同来自:

谢谢谷主大佬的解答,但是我放到游戏中,在右上角了现在。。这个是不是我在坐标转换的时候没有进行哪个操作导致的?谢谢。拜谢大佬。代码修改:
Snipaste_2024-01-10_09-04-06.png

实际显示:
Snipaste_2024-01-10_09-04-26.png

 

谷主

赞同来自:

上面已经明明用对了LocalToRoot,你突然改写为GlobalToLocal是啥意思。

要回复问题请先登录注册