首页 > 解决方案 > 如何在 Unity 中修改最终位置 LineRenderer?

问题描述

LineRenderer在一个带有Unity+Vuforia的 AR 项目中使用。这个想法是创建带有节点及其连接的 MindMap(这与 LineRenderer)。我已经这样做了。但是我不能这样做,当拖动它时,子节点会改变最终位置。

我试图禁用 check Use World Space,它正在使用父节点,因为当我将它拖到 Lines 时,它也会移动。

例如,如果我移动节点 1,所有东西都会移动,因为它是父节点,但如果我移动节点 2,LineRenderer 2 不会跟随。这是我不想发生的事情。

绘制节点

这是我绘制LineRenderer 的代码

public class DrawLineScript : MonoBehaviour
{
    public GameObject obj;

    public void DrawLine(GameObject parent, GameObject child){
        if(parent.name != "Spawner"){
            GameObject newLineGen = Instantiate(obj);   
            newLineGen.transform.SetParent(GameObject.Find(parent.name).transform);
            LineRenderer line = newLineGen.GetComponent<LineRenderer>();

            line.name = "Line " + child.name;

            line.positionCount = 2;
            line.SetPosition(0, parent.transform.position);
            line.SetPosition(1, child.transform.position); 
        }   
    }
}

这是我拖动节点的代码

public class DragScript : MonoBehaviour
{
    private Vector3 mOffset;
    private float mZCoord;

    public GameObject nodo;

    void OnMouseDown()
    {
        mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
        mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
    }

    private Vector3 GetMouseAsWorldPoint()
    {
        // Pixel coordinates of mouse (x,y)
        Vector3 mousePoint = Input.mousePosition;

        // z coordinate of game object on screen
        mousePoint.z = mZCoord;

        // Convert it to world points
        return Camera.main.ScreenToWorldPoint(mousePoint);
    }

    void OnMouseDrag()
    {
        transform.position = GetMouseAsWorldPoint() + mOffset;
    } 
}

我认为Update()与允许更新与子节点关联的 LineRenderer 的最终位置的事件一起使用。我试图将下一个脚本放入 GameObject LineRenderer:

public class UpdateLineScript : MonoBehaviour
{
    public GameObject obj;
    private Vector3 beginPosOffset;
    private Vector3 endPosOffset;   
    private Transform dentiny;
    private LineRenderer line;

    void Start() 
    {
        line = obj.GetComponent<LineRenderer>();

        Vector3[] pos = new Vector3[line.positionCount];
        line.GetPositions(pos);

        //Get offset
        beginPosOffset = transform.position - pos[0];
        endPosOffset = transform.position - pos[1];
    }

    void Update()
    {
        Vector3 newBeginPos = transform.position + beginPosOffset;
        Vector3 newEndPos = transform.position + endPosOffset;

        //Apppy new position with offset
        line.SetPosition(0, newBeginPos);
        line.SetPosition(1, newEndPos);
    }
}

但是线条出现在任何地方。

更新 LineRenderer

Repository github 与项目(分支开发):MindMap Repository

标签: c#unity3daugmented-realityvuforia

解决方案


我只需将其保留在Use World Space并将此组件放在与以下对象相同的对象上LineRenderer

public class UpdateLineScript : MonoBehaviour
{
    [SerializeField] LineRenderer line;

    private Transform _parentTransform;
    private Transform _childTransform;

    private Vector3[] positions = new Vector3[2];

    public void Initialize (Transform parentTransform, Transform childTransform, string newName)
    {
        name = newName;

        if(!line) line = GetComponent<LineRenderer>();

        _parentTransform = parentTransform;
        _childTransform = childTransform;

        line.positionCount = 2;
    }

    private void Update()
    {
        positions[0] = parentTransform.position;
        positions[1] = childTransform.position; 

        line.SetPositions(positions);
    }
}

然后,无论您在哪里生成新行,Initialize都带有两个Transform引用。从那里开始,它本身就可以得到相应的位置

public class DrawLineScript : MonoBehaviour
{
    // Make this directly of type UpdateLineRenderer
    // this way you don't need GetComponent later
    public UpdateLineScript prefab;

    public void DrawLine(GameObject parent, GameObject child)
    {
        if(parent.name == "Spawner") return;

        var newLine = Instantiate(prefab, parent.transform);   

        // Here you pass in all required information
        newLine.Initialize(parent.transform, child.transform, "Line " + child.name);
    }
}

现在,如何移动节点并不重要。第一个脚本只是不断地复制两个节点对象的绝对世界位置。


在智能手机上打字,但我希望这个想法很清楚


推荐阅读