using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; //============================================================ //支持中文,文件使用UTF-8编码 //@author #AUTHOR# //@create #CREATEDATE# //@company #COMPANY# // //@description: //============================================================ public class DragController : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler { public DragTest1 oriObjectPrefab; private DragTest1 targetObject; public UPosItem currentUPosItem; public Func DragCondition; public Action DragAction; public Action BeginDragAction; public Action EndDragAction; //public DragTest1 currentDevice; public UPosManger uPosManger; public DeviceManager deviceManager; public bool isSaveMode = false; public Image _icon; // Start is called before the first frame update void Start() { BeginDragAction = (target) => { target.gameObject.SetActive(true); }; DragAction = (target) => { //target.transform.position = Camera.main.GetComponent().ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 5)); OnRay(target.gameObject); }; } public void OnBeginDrag(PointerEventData eventData) { if (!isSaveMode) targetObject = Instantiate(oriObjectPrefab); else { targetObject = oriObjectPrefab; targetObject.gameObject.SetActive(true); } targetObject.GetComponent().F1(); //currentDevice = targetObject.GetComponent(); targetObject.transform.eulerAngles = oriObjectPrefab.transform.eulerAngles; targetObject.transform.localScale = oriObjectPrefab.transform.lossyScale; BeginDragAction?.Invoke(targetObject); } public void OnDrag(PointerEventData eventData) { if (DragCondition != null && DragCondition.Invoke()) { DragAction?.Invoke(targetObject); } else if (DragCondition == null) { DragAction?.Invoke(targetObject); } } public void OnEndDrag(PointerEventData eventData) { EndDragAction?.Invoke(targetObject); CountUPos(); //模型错误微调 if (oriObjectPrefab.name == "1" || oriObjectPrefab.name == "14" || oriObjectPrefab.name == "15" || oriObjectPrefab.name == "83") { GameManager.Inst.MoveParentAndChildren(targetObject.transform, new Vector3(-0.25f, 0, 0)); } else if (oriObjectPrefab.name == "18") { GameManager.Inst.MoveParentAndChildren(targetObject.transform, new Vector3(0, 0f, -0.02f)); } targetObject.GetComponent().F2(); currentUPosItem = null; targetObject = null; } /// /// 计算U位数量 /// private void CountUPos() { if (targetObject == null) return; if (currentUPosItem == null) { if (!isSaveMode) Destroy(targetObject.gameObject); else targetObject.gameObject.SetActive(false); return; } if (currentUPosItem.isOccupied) { if (!isSaveMode) Destroy(targetObject.gameObject); else targetObject.gameObject.SetActive(false); return; } int index = uPosManger.CountUPos(targetObject.volume, currentUPosItem.ID); //Debug.Log("index-------------" + index); if (!CountUPos(targetObject, index)) { if (isSaveMode) { targetObject.gameObject.SetActive(false); } else { Destroy(targetObject.gameObject); } } else { if (isSaveMode) { deviceManager.SwitchStagingPanel(this.gameObject); } else { //Debug.LogError(12); } } } /// /// 生成设备 /// /// /// 机位号码,从0开始 public bool CountUPos(DragTest1 dt, int index) { if (index >= 0) { dt.transform.SetParent(uPosManger.uPosItems[index].transform); dt.transform.localPosition = new Vector3(0.25f, 0, -0.045f * dt.volume + 0.0225f); uPosManger.SetCurrentUPosIsOccupied(index, dt.volume, true); DeviceItem di; if (dt.gameObject.GetComponent() == null) di = dt.gameObject.AddComponent(); else di = dt.GetComponent(); //if (di.gameObject.GetComponent() == null) //di.gameObject.AddComponent(); di.Init(deviceManager, index, dt.volume, oriObjectPrefab); deviceManager.SetDeviceItem(di); return true; } else { //Destroy(dt.gameObject); return false; } } /// /// 触碰高亮U位 /// /// public void SetUPosHighLight(bool isOnUPos) { Debug.Log(isOnUPos + "++++++isOnUPos"); if (isOnUPos) { int index = uPosManger.CountUPos(targetObject.volume, currentUPosItem.ID); uPosManger.SetUPosInstruct(index, targetObject.volume, isOnUPos); } else { uPosManger.SetUPosInstruct(0, 0, isOnUPos); } } private void OnRay(GameObject target) { currentUPosItem = null; target.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 2)); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, 100, 1 << 10)) { if (hitInfo.collider != null && hitInfo.collider.GetComponent()) { currentUPosItem = hitInfo.collider.GetComponent(); if (!EventSystem.current.IsPointerOverGameObject()) { SetUPosHighLight(true); if (!currentUPosItem.isOccupied) { target.transform.position = hitInfo.point; } } else { Debug.Log("EventSystem"); } } else { Debug.Log("hitInfo"); } } else { Debug.Log("Physics"); SetUPosHighLight(false); } } }