using QFramework; using QFramework.Example; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using XMLTool; using static XMLTool.Body3D; public class Body3DController : MonoSingleton { public enum Status { Normal = 1 << 0, Active = 1 << 1, Drag = 1 << 2, } Body3DController() { } Dictionary objs = new Dictionary(); bool selectIsGroup = false; public List isOnList = new List(); public bool allowDrag = false; public Status status = Status.Normal; private Vector2 mouseDownPosition; // 记录鼠标按下时的位置 Stack activeObjs = new Stack(); public Stack moveObjs = new Stack(); public override void OnSingletonInit() { base.OnSingletonInit(); TypeEventSystem.Global.Register((arg) => { Refresh(); }).UnRegisterWhenGameObjectDestroyed(gameObject); TypeEventSystem.Global.Register(OnClear); TypeEventSystem.Global.Register(OnToggleSelectType); TypeEventSystem.Global.Register(OnBody3DSelectedHandler); } private void OnBody3DSelectedHandler(OnBody3DSelected selected) { if (selected.isOn) { if (isOnList.Contains(selected.obj) == false) { if (selectIsGroup == false) { ClearObjectToggle(); } isOnList.Add(selected.obj); UIBody3DInfoData data = new UIBody3DInfoData(); data.body = selected.obj.GetComponent().body; UIKit.OpenPanelAsync(UILevel.PopUI, data).ToAction().Start(this); } } else { if (isOnList.Contains(selected.obj)) { isOnList.Remove(selected.obj); selected.obj.GetComponent().Set(false); } if (isOnList.Count <= 0) { UIKit.HidePanel(); } } } public void SetStatus(Status status, bool isAdd) { if (isAdd) { this.status |= status; } else { // 删除状态 this.status &= ~status; } } public bool CheckStatus(Status status) { return (this.status & status) == status; } public void AddActiveObj(GameObject obj) { activeObjs.Push(obj); } public GameObject PopActiveObj() { if (activeObjs.Count > 0) { return activeObjs.Pop(); } else { return null; } } public void AddMoveObj(GameObject obj) { if (moveObjs.Contains(obj)==false) { moveObjs.Push(obj); } } public GameObject PopMoveObj() { if (moveObjs.Count > 0) { return moveObjs.Pop(); } else { return null; } } public void Active(bool isActive) { foreach (var item in objs) { if (isOnList.Contains(item.Key) == false) { item.Key.SetActive(isActive); } } } public void Active(GameObject obj, bool isActive, bool isOther) { if (isOther) { foreach (var item in objs) { if (item.Key != obj) { item.Key.SetActive(isActive); } } } else { obj.SetActive(isActive); } } public void Transparent(GameObject obj, bool isTransparent, bool isOther) { if (isOther) { foreach (var item in objs) { if (item.Key != obj) { Utility.SetSurfaceType(item.Key.GetComponent().material, isTransparent); } } } else { Utility.SetSurfaceType(obj.GetComponent().material, isTransparent); } } private void OnToggleSelectType(OnBody3DGroupTypeChanged type) { selectIsGroup = type.isGroup; } private void OnClear(OnModuleQuit quit) { Clear(); } public void Refresh() { Parser(Global.Instance.cur3DPart); } public void Parser(Body3D.Body body) { if (body.subBody != null && body.subBody.Count > 0) { foreach (var item in body.subBody) { Parser(item.Value); } } else { GameObject obj = Utility.FindObj(body.Path); var bodyObjItem = obj.GetOrAddComponent(); bodyObjItem.Init(body); objs.Add(obj, bodyObjItem); } } private void Update() { if (Input.GetMouseButtonDown(0)) { // 记录鼠标按下时的位置 mouseDownPosition = Input.mousePosition; } if (Input.GetMouseButtonUp(0)) { // 获取鼠标抬起时的位置 Vector2 mouseUpPosition = Input.mousePosition; // 计算鼠标按下和抬起位置之间的距离 float distance = Vector2.Distance(mouseDownPosition, mouseUpPosition); // 判断移动距离是否小于 1 且未点击在 UI 上 if (distance < 1f && EventSystem.current.IsPointerOverGameObject() == false) { // 从相机发射一条射线到当前鼠标位置 if (Camera.main != null) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { //GameObject obj = hit.collider.gameObject; // 这里可以添加处理射线击中物体的逻辑 } else { if (!selectIsGroup) { ClearObjectToggle(); } } } } } } public void ClearObjectToggle() { for (int i = isOnList.Count - (1); i >= 0; i--) { isOnList[i].GetComponent().Set(false); } } public void Clear() { objs.Clear(); } }