123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using ChivaXR.VR;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using VRTK;
- public class ToolPack : MonoBehaviour
- {
- public static ToolPack instance;
- public VRTK_ControllerEvents leftController;
- public List<GameObject> tools;
- private List<ToolInfo> toolInfos = new List<ToolInfo>();
- private bool showTools;
- private int currentGrabToolIndex = -1;
- //工具默认状态
- private bool toolActive = false;
- void OnEnable()
- {
- if (leftController != null)
- {
- leftController.ButtonTwoPressed += ButtonTwoClick;
- }
- }
- private void Awake()
- {
- instance = this;
- Init();
- }
- void Init()
- {
- SetToolsActives(true);
- for (int i = 0; i < tools.Count; i++)
- {
- CVR_InteractableVRTK_ChildOfController interBase = tools[i].GetComponent<CVR_InteractableVRTK_ChildOfController>();
- if (interBase == null) continue;
- interBase.InteractableObjectGrabbed += OnToolGrable;
- interBase.InteractableObjectUngrabbed += OnToolUnGrable;
- ToolInfo info = new ToolInfo
- {
- tool = tools[i],
- position_InPack = tools[i].transform.localPosition,
- rotation_InPack = tools[i].transform.localRotation,
- scale_InPack = tools[i].transform.localScale
- };
- toolInfos.Add(info);
- }
- SetToolsActives(false);
- }
-
- /// <summary>
- /// 如果工具在背包内,并且背包处于关闭,自动打开背包
- /// </summary>
- public void CheckToolState(CVR_ToolType toolType)
- {
- foreach (var item in toolInfos)
- {
- //工具是否在背包内,并且背包关着
- if (item.tool.transform.parent == this.transform && toolActive == true)
- {
- CVR_ToolTypeFlags flags = item.tool.GetComponent<CVR_ToolTypeFlags>();
- //工具类型匹配
- if (flags != null && flags.CVR_ToolType == toolType)
- {
- SetToolsActives(true);
- }
- }
- }
- }
- /// <summary>
- /// 抓取后记录Index
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void OnToolGrable(object sender, InteractableObjectEventArgs e)
- {
- GameObject go = (sender as CVR_InteractableVRTK_ChildOfController).gameObject;
- if (go == null) return;
- go.transform.localScale = Vector3.one;
- currentGrabToolIndex = tools.IndexOf(go);
- }
- /// <summary>
- /// 放手后还原位置
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void OnToolUnGrable(object sender, InteractableObjectEventArgs e)
- {
- if (toolActive)
- {
- (sender as CVR_InteractableVRTK_ChildOfController).transform.SetParent(null);
- return;
- }
-
- toolInfos[currentGrabToolIndex].Restore(this.transform);
- currentGrabToolIndex = -1;
- }
- void ButtonTwoClick(object sender, ControllerInteractionEventArgs eventArgs)
- {
- SetToolsActives(toolActive);
- if (toolActive == false) ReIntiPositon();
- }
- void ReIntiPositon()
- {
- for (int i = 0; i < toolInfos.Count; i++)
- {
- if (toolInfos[i].tool.transform.parent != null && toolInfos[i].tool.transform.parent != this.transform)
- {
- continue;
- }
- toolInfos[i].Restore(this.transform);
- }
- }
- void SetToolsActives(bool active)
- {
- foreach (GameObject item in tools)
- {
- if (tools.IndexOf(item) == currentGrabToolIndex) continue;
- foreach (var render in item.GetComponentsInChildren<MeshRenderer>())
- {
- render.enabled = active;
- SetColliderActive(render.transform,active);
- }
- }
- toolActive = !active;
- }
- private void SetColliderActive(Transform tran, bool active)
- {
- foreach (var collider in tran.GetComponentsInChildren<Collider>())
- {
- collider.enabled = active;
- }
- }
- private void OnDisable()
- {
- if (leftController != null)
- {
- leftController.ButtonTwoPressed -= ButtonTwoClick;
- }
-
- }
- }
- public class ToolInfo
- {
- public GameObject tool;
- public Vector3 position_InPack;
- public Quaternion rotation_InPack;
- public Vector3 scale_InPack;
- public Vector3 scale_Real;
- public void Restore(Transform parent)
- {
- tool.transform.parent = parent;
- tool.transform.localPosition = position_InPack;
- tool.transform.localRotation = rotation_InPack;
- tool.transform.localScale = scale_InPack;
- }
- }
|