using Hanatric.Unity.MonoComponent; using LitJson; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.SceneManagement; public class WorkShipMouseEvent : MonoBehaviour { public CommonCamera m_FuncCamera; private GameObject m_LastHoverObj; private RaycastHit m_Hit; private Ray m_Ray; private float startMouseTime; // Start is called before the first frame update void Start() { AssignmentWorkShip(); } // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(0)) { startMouseTime = Time.time; } RayCastObject(); } /// /// 赋值工艺 /// private void AssignmentWorkShip() { //查找本地数据 List workShips = new List(); JsonData data = JsonMapper.ToObject(File.ReadAllText(Application.streamingAssetsPath + "/Processing/WorkShipInfo.json")); foreach (JsonData item in data) { WorkShip workShip = new WorkShip(); workShip.SceneName = item["SceneName"].ToString(); workShip.MeshInfos = new List(); foreach (JsonData meshInfos in item["MeshInfos"]) { Meshinfo meshinfo = new Meshinfo(); meshinfo.type = int.Parse(meshInfos["type"].ToString()); meshinfo.key = meshInfos["key"].ToString(); meshinfo.meshName = meshInfos["meshName"].ToString(); meshinfo.content = meshInfos["content"].ToString(); workShip.MeshInfos.Add(meshinfo); } workShips.Add(workShip); } string sceneName = SceneManager.GetActiveScene().name; WorkShip nowWorkShip = workShips.Find(x => x.SceneName == sceneName); if (nowWorkShip == null) { return; } List workShipBinds = FindObjectsOfType().ToList(); foreach (WorkShipBind workShipBind in workShipBinds) { Meshinfo meshinfo = nowWorkShip.MeshInfos.Find(x=>x.key == workShipBind.key); if (meshinfo == null) { continue; } workShipBind.type = meshinfo.type; workShipBind.key = meshinfo.key; workShipBind.meshName = meshinfo.meshName; workShipBind.content = meshinfo.content; } } /// /// 射线检测目标 /// private void RayCastObject() { if (EventSystem.current.IsPointerOverGameObject()) return; m_Ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(m_Ray, out m_Hit, Mathf.Infinity)) { WorkShipBind workShipBind = null; m_Hit.transform.TryGetComponent(out workShipBind); if (workShipBind == null) return; if (m_LastHoverObj != null) { StopTraverseAllGameObjects(m_LastHoverObj.transform); } m_LastHoverObj = m_Hit.transform.gameObject; StartTraverseAllGameObjects(m_LastHoverObj.transform); if (Input.GetMouseButtonUp(0)) { float mouseTime = Time.time - startMouseTime; if (mouseTime >= 0.5f) return; Transform t = workShipBind.target == null ? FindFirstObjectWithRenderer(m_LastHoverObj.transform) : workShipBind.target; m_FuncCamera.SetCenterPosition(t.position,100); FindObjectOfType().ShowLineGo(workShipBind.type, workShipBind.content, t); } } else { if (m_LastHoverObj != null) { StopTraverseAllGameObjects(m_LastHoverObj.transform); m_LastHoverObj = null; } if (Input.GetMouseButtonUp(0)) { float mouseTime = Time.time - startMouseTime; if (mouseTime >= 0.5f) return; FindObjectOfType().ShowLineGo(0,"", null, false); } } } /// /// 开始高亮 /// /// private void StartTraverseAllGameObjects(Transform parent) { if (parent.GetComponent() != null) { HDRPOutlineMgr.StartHighlight(parent.gameObject); return; } if (parent.childCount > 0) { foreach (Transform item in parent) { StartTraverseAllGameObjects(item); } } } /// /// 停止高亮 /// /// private void StopTraverseAllGameObjects(Transform parent) { if (parent.GetComponent() != null) { HDRPOutlineMgr.StopHighlight(parent.gameObject); return; } if (parent.childCount > 0) { foreach (Transform item in parent) { StopTraverseAllGameObjects(item); } } } /// /// 查找第一个有Renderer组件的物体 /// /// /// Transform FindFirstObjectWithRenderer(Transform parent) { if (parent.GetComponent() != null) { return parent; } foreach (Transform child in parent) { Transform result = FindFirstObjectWithRenderer(child); if (result != null) { return result; } } return null; } }