GQ_Communicate/GQ_TongXin/Assets/Adam/Scripts/DragController.cs

194 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
//============================================================
//支持中文文件使用UTF-8编码
//@author #AUTHOR#
//@create #CREATEDATE#
//@company #COMPANY#
//
//@description:
//============================================================
public class DragController : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
public DragTest1 oriObjectPrefab;
private DragTest1 targetObject;
private UPosItem currentUPosItem;
public Func<bool> DragCondition;
public Action<DragTest1> DragAction;
public Action<DragTest1> BeginDragAction;
public Action<DragTest1> EndDragAction;
//public DragTest1 currentDevice;
public UPosManger uPosManger;
public DeviceManager deviceManager;
public bool isSaveMode = false;
// Start is called before the first frame update
void Start()
{
BeginDragAction = (target) =>
{
target.gameObject.SetActive(true);
};
DragAction = (target) =>
{
//target.transform.position = Camera.main.GetComponent<Camera>().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);
}
//currentDevice = targetObject.GetComponent<DragTest1>();
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();
currentUPosItem = null;
targetObject = null;
}
/// <summary>
/// 计算U位数量
/// </summary>
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);
}
}
}
/// <summary>
/// 自动生成设备
/// </summary>
/// <param name="dt"></param>
/// <param name="index">机位号码从0开始</param>
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<DeviceItem>() == null)
di = dt.gameObject.AddComponent<DeviceItem>();
else
di = dt.GetComponent<DeviceItem>();
if (di.gameObject.GetComponent<HighLight_VR>() == null)
di.gameObject.AddComponent<HighLight_VR>();
di.Init(deviceManager, index, dt.volume, oriObjectPrefab);
deviceManager.SetDeviceItem(di);
return true;
}
else
{
//Destroy(dt.gameObject);
return false;
}
}
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<UPosItem>())
{
currentUPosItem = hitInfo.collider.GetComponent<UPosItem>();
if (!EventSystem.current.IsPointerOverGameObject())
{
if (!currentUPosItem.isOccupied)
{
target.transform.position = hitInfo.point;
}
}
}
else
{
}
}
}
}