196 lines
5.8 KiB
C#
196 lines
5.8 KiB
C#
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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// ¸³Öµ¹¤ÒÕ
|
||
/// </summary>
|
||
private void AssignmentWorkShip()
|
||
{
|
||
//²éÕÒ±¾µØÊý¾Ý
|
||
List<WorkShip> workShips = new List<WorkShip>();
|
||
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<Meshinfo>();
|
||
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<WorkShipBind> workShipBinds = FindObjectsOfType<WorkShipBind>().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;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// ÉäÏß¼ì²âÄ¿±ê
|
||
/// </summary>
|
||
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<WorkmanUIManager>().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<WorkmanUIManager>().ShowLineGo(0,"", null, false);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// ¿ªÊ¼¸ßÁÁ
|
||
/// </summary>
|
||
/// <param name="go"></param>
|
||
private void StartTraverseAllGameObjects(Transform parent)
|
||
{
|
||
if (parent.GetComponent<Renderer>() != null)
|
||
{
|
||
HDRPOutlineMgr.StartHighlight(parent.gameObject);
|
||
return;
|
||
}
|
||
if (parent.childCount > 0)
|
||
{
|
||
foreach (Transform item in parent)
|
||
{
|
||
StartTraverseAllGameObjects(item);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Í£Ö¹¸ßÁÁ
|
||
/// </summary>
|
||
/// <param name="go"></param>
|
||
private void StopTraverseAllGameObjects(Transform parent)
|
||
{
|
||
if (parent.GetComponent<Renderer>() != null)
|
||
{
|
||
HDRPOutlineMgr.StopHighlight(parent.gameObject);
|
||
return;
|
||
}
|
||
if (parent.childCount > 0)
|
||
{
|
||
foreach (Transform item in parent)
|
||
{
|
||
StopTraverseAllGameObjects(item);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// ²éÕÒµÚÒ»¸öÓÐRenderer×é¼þµÄÎïÌå
|
||
/// </summary>
|
||
/// <param name="parent"></param>
|
||
/// <returns></returns>
|
||
Transform FindFirstObjectWithRenderer(Transform parent)
|
||
{
|
||
if (parent.GetComponent<Renderer>() != null)
|
||
{
|
||
return parent;
|
||
}
|
||
|
||
foreach (Transform child in parent)
|
||
{
|
||
Transform result = FindFirstObjectWithRenderer(child);
|
||
if (result != null)
|
||
{
|
||
return result;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|