
Родительский объект имеет сценарий, в котором хранятся данные узла. Он включает в себя:
- GUIDText: отображает GUID узла.
- NodeText : поле ввода для добавления текста в узел.
- Кнопка «Добавить»: создает экземпляр игрового объекта ответа для соединения узлов.
- responsesTransform: Содержит ответы.

Я хочу нарисовать линию от ответчиков к узлам с помощью LineRenderer, но у меня проблемы с позиционированием. Код соединения узлов между собой следующий:
Код: Выделить всё
public class ConnectNodes : MonoBehaviour
{
public GraphicRaycaster raycaster;
public EventSystem eventSystem;
private GameObject currentNode;
private GameObject targetNode;
public GameObject linePrefab;
public Transform canvas;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
GetResponseElement();
}
if (Input.GetMouseButtonUp(0))
{
GetNodeElement();
if (currentNode != null && targetNode != null && currentNode != targetNode)
{
var currentNodeResponse = currentNode.GetComponent();
var targetNodeComponent = targetNode.GetComponent();
if (currentNodeResponse != null && targetNodeComponent != null)
{
if (!currentNodeResponse.nodeData.NextNodes.Contains(targetNodeComponent.nodeData))
{
currentNodeResponse.nodeData.NextNodes.Add(targetNodeComponent.nodeData);
}
Vector3 currentConnectPointPos = currentNode.GetComponentInParent().position;
Vector3 targetConnectPointPos = targetNodeComponent.GetComponent().position;
GameObject lineGameobject = Instantiate(linePrefab,canvas);
lineGameobject.AddComponent();
LineRenderer line = lineGameobject.GetComponent();
line.SetPosition(0, currentConnectPointPos);
line.SetPosition(1,targetConnectPointPos);
Debug.Log($"Connected {currentNodeResponse.nodeData.NodeGUID} to {targetNodeComponent.nodeData.NodeGUID}");
}
else
{
Debug.LogError("Either currentNode or targetNode does not have the expected components!");
}
}
currentNode = null;
targetNode = null;
}
}
void GetResponseElement()
{
PointerEventData pointerEventData = new PointerEventData(eventSystem);
pointerEventData.position = Input.mousePosition;
List results = new List();
raycaster.Raycast(pointerEventData, results);
foreach (RaycastResult result in results)
{
ResponseGameObject response = result.gameObject.GetComponent();
if (response != null)
{
currentNode = response.gameObject;
Debug.Log("response found: " + response.name);
}
}
}
void GetNodeElement()
{
PointerEventData pointerEventData = new PointerEventData(eventSystem);
pointerEventData.position = Input.mousePosition;
List results = new List();
raycaster.Raycast(pointerEventData, results);
foreach (RaycastResult result in results)
{
NodeGameObject node = result.gameObject.GetComponent();
if (node != null)
{
targetNode = node.gameObject;
Debug.Log("node found: " + node.name);
}
}
}
}
Я пробовал использовать локальные позиции, но кажется, что они относятся к родительскому элементу, поэтому я не получил никаких результатов. Я также пытался использовать Transform.position вместо RectTransform, но это тоже не сработало.
Подробнее здесь: https://stackoverflow.com/questions/790 ... ting-nodes
Мобильная версия