using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class DragNode : MonoBehaviour { public Text uiName; private RectTransform rectTransform; public float border; public float minWidth; private RectTransform canvasRect; private Vector2 localPos; private Vector2 offSet; public SecondNodeObject SecondNodeObjectObj; private void Awake() { canvasRect = transform.parent.GetComponent(); rectTransform = GetComponent(); offSet = new Vector2(20f, -20f); } // Start is called before the first frame update void Start() { } public void SetinfoWhileBeginDrag(SecondNodeObject info) { SecondNodeObjectObj = (SecondNodeObject)info.DeepCopy(); } // Update is called once per frame void Update() { } public void SetUI(string text) { uiName.text = text; if (uiName.preferredWidth < minWidth) { rectTransform.sizeDelta = new Vector2(minWidth, rectTransform.sizeDelta.y); } else { rectTransform.sizeDelta = new Vector2(uiName.preferredWidth + border, rectTransform.sizeDelta.y); } } public void BeginDrag(PointerEventData eventData) { if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, eventData.enterEventCamera, out localPos)) { rectTransform.localPosition = localPos + offSet; } } public void Drag(PointerEventData eventData) { if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, eventData.position, eventData.enterEventCamera, out localPos)) { rectTransform.localPosition = localPos + offSet; } } }