我绿色按键并没有安装脚本,但是还是会被连接,有什么办法能不让按键被连接。
如果还能提供撤销上一次连线和删除所有连线的方法,我可以适当追加酬金。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class Class1 : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler, IEndDragHandler
{
[Header("线宽度")]
public float lineWidth = 3f;
[Header("线颜色")]
public Color lineColor = Color.white;
[Header("线材质")]
public Material dongtai;
[Header("限制多选")]
bool isUP = true;
public Button ok;
//连线
GameObject lineObj = null;
RectTransform lineObjRT = null;
Image lineObjImg;
//初始点
Vector3 startPos = Vector3.zero;
Vector3 endPos = Vector3.zero;
// 最后落脚的物体
GameObject GetObj;
public void OnDrag(PointerEventData eventData)
{
if (isUP)
{
endPos = Input.mousePosition;
Vector3 durationPos = endPos - startPos;
lineObjRT.sizeDelta = new Vector2(durationPos.magnitude, lineWidth);
float angle = Mathf.Atan2(durationPos.y, durationPos.x) * Mathf.Rad2Deg;
lineObjRT.localRotation = Quaternion.Euler(0, 0, angle);
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (isUP)
{
if (eventData.pointerEnter != null)
{
print(eventData.pointerEnter.name);
endPos = eventData.pointerEnter.transform.position;
Vector3 durationPos = endPos - startPos;
lineObjRT.sizeDelta = new Vector2(durationPos.magnitude, lineWidth);
float angle = Mathf.Atan2(durationPos.y, durationPos.x) * Mathf.Rad2Deg;
lineObjRT.localRotation = Quaternion.Euler(0, 0, angle);
isUP = false;
}
else
{
print("未选中,结果为空");
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
//限制多选
if (isUP)
{
lineObj = new GameObject("LineObj");
lineObj.SetActive(false);
lineObjRT = lineObj.AddComponent<RectTransform>();
lineObjRT.pivot = new Vector2(0, 0.5f);
lineObjRT.localScale = Vector3.one;
lineObjImg = lineObj.AddComponent<Image>();
lineObjImg.color = lineColor;
lineObjImg.raycastTarget = false;
lineObjRT.SetParent(transform);
startPos = transform.position;
lineObjRT.position = startPos;
lineObjRT.sizeDelta = Vector2.zero;
lineObj.SetActive(true);
};
}
public void OnPointerUp(PointerEventData eventData)
{
if (isUP)
{
if (eventData.pointerEnter != null)
{
GetObj = eventData.pointerEnter;
}
if (eventData.pointerEnter == null)
{
DestroyImmediate(lineObjRT.gameObject);
}
}
if (eventData.button == PointerEventData.InputButton.Right)
{
DestoryAll();
isUP = true;
}
}
//销毁自身所有的线
void DestoryAll()
{
for (int i = 0; i < transform.childCount; i++)
{
Destroy(transform.GetChild(i).gameObject);
}
}
}