如何在Unity中修改最终位置LineRenderer?

How to modified final position LineRenderer in Unity?

我在一个项目中使用 LineRendererUnity+Vuforia 用于 AR。这个想法是用节点和它的连接创建思维导图(这个用 LineRenderer)。我已经这样做了。但是我不能这样做,当它被拖动时,子节点会改变最终位置。

我试图禁用检查 Use World Space,它正在与父节点一起工作,因为当我将它拖动到线时,它也会移动。

例如,如果我移动节点 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);
    }
}

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

项目(分支开发)的存储库github:MindMap Repository

我会简单地将它留在 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);
    }
}

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


在智能手机上打字,但我希望思路清晰