430 lines
13 KiB
C#
430 lines
13 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class ToolHolder : MonoBehaviour
|
||
{
|
||
/// <summary>
|
||
/// 手中是否有工器具
|
||
/// </summary>
|
||
private bool isHoldTool = false;
|
||
/// <summary>
|
||
/// 当前选择的工器具
|
||
/// </summary>
|
||
private ToolBase currentTb;
|
||
/// <summary>
|
||
/// temp的工器具
|
||
/// </summary>
|
||
ToolBase db;
|
||
|
||
/// <summary>
|
||
/// tool放置父物体
|
||
/// </summary>
|
||
public Transform gripper;
|
||
/// <summary>
|
||
/// 手放置父物体
|
||
/// </summary>
|
||
private Transform handGripper;
|
||
public static ToolHolder instance;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public Transform gripperParent;
|
||
/// <summary>
|
||
/// 射线检测距离
|
||
/// </summary>
|
||
float rayDiatance = 2f;
|
||
private void Start()
|
||
{
|
||
instance = this;
|
||
gripperParent = Camera.main.transform;
|
||
gripper = Camera.main.transform.Find("Gripper");
|
||
handGripper = Camera.main.transform.Find("handGripper");
|
||
//非工器具间 不可触发射线检测
|
||
if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Contains("Tool"))
|
||
{
|
||
isHoldTool = false;
|
||
}
|
||
else
|
||
{
|
||
isHoldTool = true;
|
||
}
|
||
}
|
||
private void Update()
|
||
{
|
||
if (isHoldTool)
|
||
{
|
||
return;
|
||
}
|
||
RayToInteractableObject();
|
||
|
||
}
|
||
/// <summary>
|
||
/// 射线检测可交互物体
|
||
/// </summary>
|
||
private void RayToInteractableObject()
|
||
{
|
||
|
||
if (EventSystem.current.IsPointerOverGameObject())
|
||
{
|
||
return;
|
||
}
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit raycast;
|
||
if (Physics.Raycast(ray, out raycast, rayDiatance))
|
||
{
|
||
Debug.DrawLine(ray.origin, raycast.point);
|
||
db = raycast.transform.GetComponent<ToolBase>();
|
||
if (db)
|
||
{
|
||
if (!db.highlightOnHover) return;
|
||
if (currentTb != db)
|
||
{
|
||
|
||
HighLight_VR.instance.SetHight(db.transform);
|
||
currentTb = db;
|
||
//Debug.Log("Hit"+db.gameObject);
|
||
}
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
//if (EventSystem.current.IsPointerOverGameObject())
|
||
//{
|
||
// return;
|
||
//}
|
||
if (PackagePanel.instacne == null)
|
||
{
|
||
XFrame.Core.UI.XUIPanel.ShowPanel<PackagePanel>();
|
||
XFrame.Core.UI.XUIPanel.ClosePanel<PackagePanel>();
|
||
}
|
||
if (PackagePanel.instacne.content == null)
|
||
{
|
||
XFrame.Core.UI.XUIPanel.ShowPanel<PackagePanel>();
|
||
XFrame.Core.UI.XUIPanel.ClosePanel<PackagePanel>();
|
||
}
|
||
if (!db.isHold)
|
||
{
|
||
PackagePanel.instacne.AddToolIntoPackageFromShelf(db);
|
||
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (currentTb != null)
|
||
{
|
||
HighLight_VR.instance.CloseHight();
|
||
currentTb = null;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (currentTb != null)
|
||
{
|
||
HighLight_VR.instance.CloseHight();
|
||
}
|
||
currentTb = null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 拿到手中
|
||
/// </summary>
|
||
/// <param name="deviceBase"></param>
|
||
public void Show(ToolBase deviceBase, Texture _tex = null)
|
||
{
|
||
// 移除现有tool
|
||
int count = gripper.childCount;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
ToolBase tb = gripper.GetChild(i).GetComponent<ToolBase>();
|
||
if (tb)
|
||
{
|
||
if (tb != deviceBase)
|
||
{
|
||
if (tb.gameObject.activeInHierarchy)
|
||
tb.OnFinishUse();
|
||
}
|
||
}
|
||
// 移除手
|
||
RemoveHand();
|
||
gripper.GetChild(i).gameObject.SetActive(false);
|
||
}
|
||
// 添加目标tool
|
||
deviceBase.gameObject.SetActive(true);
|
||
|
||
|
||
// 1:放置到 摄像机下面
|
||
if (deviceBase.operationType.Equals(ToolOperationType.InHand))
|
||
{
|
||
//生成手 ,如果有
|
||
SetHand(deviceBase);
|
||
|
||
deviceBase.transform.SetParent(gripper);
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
//2:直接放置到现场不放入手中 比如梯子
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.DirectUse))
|
||
{
|
||
deviceBase.transform.SetParent(null);
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
// 3:不使用, 比如各种材料 不需要点击
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.DonOperation))
|
||
{
|
||
deviceBase.transform.SetParent(gripper);
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
Debug.Log(deviceBase.toolName);
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.ChooseChild))
|
||
{
|
||
deviceBase.transform.SetParent(gripper);
|
||
if (_tex)
|
||
{
|
||
ToolModel_Material tmat = deviceBase.gameObject.GetComponent<ToolModel_Material>();
|
||
tmat.replaceTex = _tex;
|
||
deviceBase.gameObject.SetMeshRendererActive(true, _tex);
|
||
}
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
Debug.Log(deviceBase.toolName);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 拿到手中
|
||
/// </summary>
|
||
/// <param name="deviceBase"></param>
|
||
public void ShowMat(ToolBase deviceBase,Texture _tex =null)
|
||
{
|
||
// 移除现有tool
|
||
int count = gripper.childCount;
|
||
for (int i = 0; i <count; i++)
|
||
{
|
||
ToolBase tb = gripper.GetChild(i).GetComponent<ToolBase>();
|
||
if (tb)
|
||
{
|
||
if (tb != deviceBase)
|
||
{
|
||
if (tb.gameObject.activeInHierarchy)
|
||
tb.OnFinishUse();
|
||
}
|
||
}
|
||
// 移除手
|
||
RemoveHand();
|
||
gripper.GetChild(i).gameObject.SetActive(false);
|
||
}
|
||
// 添加目标tool
|
||
deviceBase.gameObject.SetActive(true);
|
||
|
||
|
||
// 1:放置到 摄像机下面
|
||
if (deviceBase.operationType.Equals(ToolOperationType.InHand))
|
||
{
|
||
//生成手 ,如果有
|
||
SetHand(deviceBase);
|
||
|
||
deviceBase.transform.SetParent(gripper);
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
//2:直接放置到现场不放入手中 比如梯子
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.DirectUse))
|
||
{
|
||
deviceBase.transform.SetParent(null);
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
// 3:不使用, 比如各种材料 不需要点击
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.DonOperation))
|
||
{
|
||
deviceBase.transform.SetParent(gripper);
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
Debug.Log(deviceBase.toolName);
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.ChooseChild))
|
||
{
|
||
deviceBase.transform.SetParent(gripper);
|
||
if (_tex)
|
||
{
|
||
ToolModel_Material tmat = deviceBase.gameObject.GetComponent<ToolModel_Material>();
|
||
tmat.replaceTex = _tex;
|
||
deviceBase.gameObject.SetMeshRendererActive(true, _tex);
|
||
}
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
Debug.Log(deviceBase.toolName);
|
||
}
|
||
}
|
||
|
||
public void ShowMat(ToolBase deviceBase, string _name ="")
|
||
{
|
||
// 移除现有tool
|
||
int count = gripper.childCount;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
ToolBase tb = gripper.GetChild(i).GetComponent<ToolBase>();
|
||
if (tb)
|
||
{
|
||
if (tb != deviceBase)
|
||
{
|
||
if (tb.gameObject.activeInHierarchy)
|
||
tb.OnFinishUse();
|
||
}
|
||
}
|
||
// 移除手
|
||
RemoveHand();
|
||
gripper.GetChild(i).gameObject.SetActive(false);
|
||
}
|
||
// 添加目标tool
|
||
deviceBase.gameObject.SetActive(true);
|
||
|
||
|
||
// 1:放置到 摄像机下面
|
||
if (deviceBase.operationType.Equals(ToolOperationType.InHand))
|
||
{
|
||
//生成手 ,如果有
|
||
SetHand(deviceBase);
|
||
|
||
deviceBase.transform.SetParent(gripper);
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
//2:直接放置到现场不放入手中 比如梯子
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.DirectUse))
|
||
{
|
||
deviceBase.transform.SetParent(null);
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
// 3:不使用, 比如各种材料 不需要点击
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.DonOperation))
|
||
{
|
||
deviceBase.transform.SetParent(gripper);
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
Debug.Log(deviceBase.toolName);
|
||
deviceBase.OnBeginUse();
|
||
}
|
||
else if (deviceBase.operationType.Equals(ToolOperationType.ChooseChild))
|
||
{
|
||
deviceBase.transform.SetParent(gripper);
|
||
if (!string.IsNullOrEmpty(_name))
|
||
{
|
||
deviceBase.transform.ChildDo((child) => { child.SetMeshRendererActive(false); });
|
||
deviceBase.transform.Find(_name).gameObject.SetMeshRendererActive(true);
|
||
}
|
||
deviceBase.transform.localPosition = deviceBase.inHandPos;
|
||
deviceBase.transform.localEulerAngles = deviceBase.inHandRot;
|
||
Debug.Log(deviceBase.toolName);
|
||
}
|
||
}
|
||
GameObject hand;
|
||
/// <summary>
|
||
/// 生成手
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
void SetHand(ToolBase data)
|
||
{
|
||
for (int i = 0; i < handGripper.childCount; i++)
|
||
{
|
||
GameObject.DestroyImmediate(handGripper.GetChild(i).gameObject);
|
||
}
|
||
hand = Resources.Load<GameObject>("Prefabs/handPrefabs/hand_" + data.toolName);
|
||
if (hand)
|
||
{
|
||
hand =GameObject.Instantiate<GameObject>(hand,handGripper);
|
||
hand.GetComponent<HandModel>().Init() ;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 移除所有手
|
||
/// </summary>
|
||
public void RemoveHand()
|
||
{
|
||
// 移除手
|
||
for (int n = 0; n < handGripper.childCount; n++)
|
||
{
|
||
GameObject.Destroy(handGripper.GetChild(n).gameObject);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 获取当前手组件
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public HandModel GetCurrentHand()
|
||
{
|
||
HandModel hm=null;
|
||
|
||
if (handGripper.childCount > 0)
|
||
{
|
||
hm = handGripper.GetChild(0).GetComponent<HandModel>();
|
||
}
|
||
return hm;
|
||
}
|
||
/// <summary>
|
||
/// 获取当前工器具
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public ToolBase GetCurrentTool()
|
||
{
|
||
ToolBase tb=null;
|
||
for (int i = 0; i < gripper.childCount; i++)
|
||
{
|
||
if (gripper.GetChild(i).gameObject.activeInHierarchy)
|
||
{
|
||
tb = gripper.GetChild(i).GetComponent<ToolBase>();
|
||
break;
|
||
}
|
||
}
|
||
return tb;
|
||
}
|
||
/// <summary>
|
||
/// 获取当前工器具
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <returns></returns>
|
||
public T GetCurrentTool<T>()
|
||
{
|
||
T tb =default(T);
|
||
for (int i = 0; i < gripper.childCount; i++)
|
||
{
|
||
if (gripper.GetChild(i).gameObject.activeInHierarchy)
|
||
{
|
||
tb = gripper.GetChild(i).GetComponent<T>();
|
||
break;
|
||
}
|
||
}
|
||
return tb;
|
||
}
|
||
public void SetToolParentToHand(ToolBase tool)
|
||
{
|
||
tool.transform.SetParent(gripper);
|
||
}
|
||
}
|
||
/*
|
||
*
|
||
*
|
||
/// <summary>
|
||
/// 确认放入
|
||
/// </summary>
|
||
/// <param name="tempDb"></param>
|
||
void AddInToHand(ToolBase tempDb)
|
||
{
|
||
//doldTip.Show(db);
|
||
//放入背包
|
||
XFrame.Core.UI.XUIPanel.ShowPanel<TipsCheckPanel>((a) => {
|
||
if ((bool)a)
|
||
{
|
||
|
||
}
|
||
}, " 是否将 <color=#00FF5D>" + tempDb.toolName + "</color> 放入工具包", false);
|
||
}
|
||
* */ |