c# - 如何在 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;
}
提前谢谢你的帮助!
解决方案
推荐阅读
- spring - Spring Bean 不可写或具有无效的 setter 方法
- google-sheets - 记分卡显示每月的总平均值
- typescript - Typescript 扩展相同接口的不兼容泛型类型
- perl - 如何获取用户可配置的打印缓冲区?
- ios - ITMS-90809:已弃用的 API 使用 — 当上传我的 react native 应用时,Apple 将停止接受使用 UIWebView API 的应用提交
- angular - Angular 组件属性未在 HTTPRequest 订阅中更新
- python - 拦截python类中的魔术方法调用
- c++ - 单目总捕获编译错误:致命错误:glm/glm.hpp:找不到文件或目录
- php - 如何使用“n Y”格式创建 DateTime 并使用 DateTime::createFromFormat 控制错误日期
- ios - 根据标签文本自定义 UICollectionViewCell 自身大小