using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class DragIcon : IconBase { protected Vector2 offsetPos; //临时记录点击点与UI的相对位置 protected bool isOnMap;//放置在固定位置 protected bool keepLocalScale;//是否保持自身缩放 true随父物体缩放 false父物体缩放时始终保持当前的大小 protected bool isAdded;//已经添加到记录中 protected List results = new List();//点击时触发的所有UI层级 // Start is called before the first frame update protected virtual void Start() { if (!isOnMap) { transform.position = (Vector2)Input.mousePosition; offsetPos = (Vector2)Input.mousePosition - (Vector2)transform.position; } float parentScale = this.transform.parent.localScale.x; this.transform.localScale = new Vector3(1.0f / parentScale, 1.0f / parentScale, 1); } // Update is called once per frame protected virtual void Update() { if (!isOnMap) { transform.position = (Vector2)Input.mousePosition - offsetPos; } if (!keepLocalScale) { float parentScale = this.transform.parent.localScale.x; this.transform.localScale = new Vector3(1.0f / parentScale, 1.0f / parentScale, 1); } } protected void GetMouseRaycastAll() { PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); eventDataCurrentPosition.position = Input.mousePosition; EventSystem.current.RaycastAll(eventDataCurrentPosition, results); } //设置初始化位置 并固定在该位置 关闭拖拽逻辑 public void SetFixedAnchoredPos(Vector2 anchorPos) { this.GetComponent().anchoredPosition = anchorPos; isOnMap = true; isAdded = true; } /// /// 被对象池收回时初始化参数 /// protected virtual void OnDisable() { isOnMap = false; isAdded = false; } }