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 tools; private List toolInfos = new List(); 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(); 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); } /// /// 如果工具在背包内,并且背包处于关闭,自动打开背包 /// 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(); //工具类型匹配 if (flags != null && flags.CVR_ToolType == toolType) { SetToolsActives(true); } } } } /// /// 抓取后记录Index /// /// /// 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); } /// /// 放手后还原位置 /// /// /// 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()) { render.enabled = active; SetColliderActive(render.transform,active); } } toolActive = !active; } private void SetColliderActive(Transform tran, bool active) { foreach (var collider in tran.GetComponentsInChildren()) { 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; } }