You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
66 lines
1.8 KiB
3 years ago
|
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>();
|
||
|
rectTransform = GetComponent<RectTransform>();
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|