首页 > 解决方案 > 如何在 Unity 中存储来自 InputField 的 2 个游戏对象

问题描述

我正在尝试制作一个移动应用程序,在 Canvas Image 上显示从一个航路点到另一个航路点的用户路径。我在这里使用 Dijkstra 算法https://github.com/hasanbayatme/unity-dijkstras-pathfinding

用户应输入起始节点,然后输入目标节点。程序应该能够读取这两个输入并将它们存储到 Graph.GetShortestPath(start, end); 然后从节点绘制线,存储在路径数组中。我使用这里的代码来读取输入 https://answers.unity.com/questions/1520095/how-can-i-find-objects-with-using-inputfield.html

似乎它读取了它们并找到了我需要的节点,但我一直在存储第二个输入并显示路径。

这是我尝试的代码:

{
    public InputField inputField;
    private LineRenderer lineRenderer;
    protected Node m_From;
    protected Node m_To;
    protected FindPath m_Path = new FindPath();
    List<GameObject> targets = new List<GameObject>();
   [SerializeField]
    protected Graph m_Graph;

/// <summary>
    /// Indicates an object has been found
    /// </summary>
    /// <param name="target">The target.</param>
    public void TurnOn(GameObject target)
    {
        targets.Add(target);
        GameObject lineObject = new GameObject();
        lineRenderer = lineObject.AddComponent<LineRenderer>();
        lineRenderer.startWidth = 0.3f;
        lineRenderer.endWidth = 0.3f;

        // Customize the input field according to the gameObject
        // Do whathever you want
        if (m_From == null)
        {
            m_From = targets[0];
        }
        else
        {
            m_To = targets[1];
        }

        m_Path = m_Graph.GetShortestPath(m_From, m_To);

        Vector3[] checkPointArray = new Vector3[m_Path.nodes.Count];

        for (int i = 0; i < m_Path.nodes.Count; i++)
        {
            Vector3 checkpointpos = m_Path.nodes[i].transform.position;
            checkPointArray[i] = new Vector3(checkpointpos.x, checkpointpos.y);
            lineRenderer.SetPosition(i, checkPointArray[i]);
        }
        

            targets.Clear();}
     
        
        inputField.textComponent.color = Color.black;
    }


提前谢谢你的帮助!

标签: c#unity3duser-interfacepath-findinginput-field

解决方案


推荐阅读