using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ToolHolder : MonoBehaviour
{
///
/// 手中是否有工器具
///
private bool isHoldTool = false;
///
/// 当前选择的工器具
///
private ToolBase currentTb;
///
/// temp的工器具
///
ToolBase db;
///
/// tool放置父物体
///
public Transform gripper;
///
/// 手放置父物体
///
private Transform handGripper;
public static ToolHolder instance;
///
///
///
public Transform gripperParent;
///
/// 射线检测距离
///
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();
}
///
/// 射线检测可交互物体
///
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();
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();
XFrame.Core.UI.XUIPanel.ClosePanel();
}
if (PackagePanel.instacne.content == null)
{
XFrame.Core.UI.XUIPanel.ShowPanel();
XFrame.Core.UI.XUIPanel.ClosePanel();
}
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;
}
}
///
/// 拿到手中
///
///
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();
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();
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,Texture _tex =null)
{
// 移除现有tool
int count = gripper.childCount;
for (int i = 0; i ();
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();
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();
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;
///
/// 生成手
///
///
void SetHand(ToolBase data)
{
for (int i = 0; i < handGripper.childCount; i++)
{
GameObject.DestroyImmediate(handGripper.GetChild(i).gameObject);
}
hand = Resources.Load("Prefabs/handPrefabs/hand_" + data.toolName);
if (hand)
{
hand =GameObject.Instantiate(hand,handGripper);
hand.GetComponent().Init() ;
}
}
///
/// 移除所有手
///
public void RemoveHand()
{
// 移除手
for (int n = 0; n < handGripper.childCount; n++)
{
GameObject.Destroy(handGripper.GetChild(n).gameObject);
}
}
///
/// 获取当前手组件
///
///
public HandModel GetCurrentHand()
{
HandModel hm=null;
if (handGripper.childCount > 0)
{
hm = handGripper.GetChild(0).GetComponent();
}
return hm;
}
///
/// 获取当前工器具
///
///
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();
break;
}
}
return tb;
}
///
/// 获取当前工器具
///
///
///
public T GetCurrentTool()
{
T tb =default(T);
for (int i = 0; i < gripper.childCount; i++)
{
if (gripper.GetChild(i).gameObject.activeInHierarchy)
{
tb = gripper.GetChild(i).GetComponent();
break;
}
}
return tb;
}
public void SetToolParentToHand(ToolBase tool)
{
tool.transform.SetParent(gripper);
}
}
/*
*
*
///
/// 确认放入
///
///
void AddInToHand(ToolBase tempDb)
{
//doldTip.Show(db);
//放入背包
XFrame.Core.UI.XUIPanel.ShowPanel((a) => {
if ((bool)a)
{
}
}, " 是否将 " + tempDb.toolName + " 放入工具包", false);
}
* */