ToolPack.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using ChivaXR.VR;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using VRTK;
  6. public class ToolPack : MonoBehaviour
  7. {
  8. public static ToolPack instance;
  9. public VRTK_ControllerEvents leftController;
  10. public List<GameObject> tools;
  11. private List<ToolInfo> toolInfos = new List<ToolInfo>();
  12. private bool showTools;
  13. private int currentGrabToolIndex = -1;
  14. //工具默认状态
  15. private bool toolActive = false;
  16. void OnEnable()
  17. {
  18. if (leftController != null)
  19. {
  20. leftController.ButtonTwoPressed += ButtonTwoClick;
  21. }
  22. }
  23. private void Awake()
  24. {
  25. instance = this;
  26. Init();
  27. }
  28. void Init()
  29. {
  30. SetToolsActives(true);
  31. for (int i = 0; i < tools.Count; i++)
  32. {
  33. CVR_InteractableVRTK_ChildOfController interBase = tools[i].GetComponent<CVR_InteractableVRTK_ChildOfController>();
  34. if (interBase == null) continue;
  35. interBase.InteractableObjectGrabbed += OnToolGrable;
  36. interBase.InteractableObjectUngrabbed += OnToolUnGrable;
  37. ToolInfo info = new ToolInfo
  38. {
  39. tool = tools[i],
  40. position_InPack = tools[i].transform.localPosition,
  41. rotation_InPack = tools[i].transform.localRotation,
  42. scale_InPack = tools[i].transform.localScale
  43. };
  44. toolInfos.Add(info);
  45. }
  46. SetToolsActives(false);
  47. }
  48. /// <summary>
  49. /// 如果工具在背包内,并且背包处于关闭,自动打开背包
  50. /// </summary>
  51. public void CheckToolState(CVR_ToolType toolType)
  52. {
  53. foreach (var item in toolInfos)
  54. {
  55. //工具是否在背包内,并且背包关着
  56. if (item.tool.transform.parent == this.transform && toolActive == true)
  57. {
  58. CVR_ToolTypeFlags flags = item.tool.GetComponent<CVR_ToolTypeFlags>();
  59. //工具类型匹配
  60. if (flags != null && flags.CVR_ToolType == toolType)
  61. {
  62. SetToolsActives(true);
  63. }
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// 抓取后记录Index
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. void OnToolGrable(object sender, InteractableObjectEventArgs e)
  73. {
  74. GameObject go = (sender as CVR_InteractableVRTK_ChildOfController).gameObject;
  75. if (go == null) return;
  76. go.transform.localScale = Vector3.one;
  77. currentGrabToolIndex = tools.IndexOf(go);
  78. }
  79. /// <summary>
  80. /// 放手后还原位置
  81. /// </summary>
  82. /// <param name="sender"></param>
  83. /// <param name="e"></param>
  84. void OnToolUnGrable(object sender, InteractableObjectEventArgs e)
  85. {
  86. if (toolActive)
  87. {
  88. (sender as CVR_InteractableVRTK_ChildOfController).transform.SetParent(null);
  89. return;
  90. }
  91. toolInfos[currentGrabToolIndex].Restore(this.transform);
  92. currentGrabToolIndex = -1;
  93. }
  94. void ButtonTwoClick(object sender, ControllerInteractionEventArgs eventArgs)
  95. {
  96. SetToolsActives(toolActive);
  97. if (toolActive == false) ReIntiPositon();
  98. }
  99. void ReIntiPositon()
  100. {
  101. for (int i = 0; i < toolInfos.Count; i++)
  102. {
  103. if (toolInfos[i].tool.transform.parent != null && toolInfos[i].tool.transform.parent != this.transform)
  104. {
  105. continue;
  106. }
  107. toolInfos[i].Restore(this.transform);
  108. }
  109. }
  110. void SetToolsActives(bool active)
  111. {
  112. foreach (GameObject item in tools)
  113. {
  114. if (tools.IndexOf(item) == currentGrabToolIndex) continue;
  115. foreach (var render in item.GetComponentsInChildren<MeshRenderer>())
  116. {
  117. render.enabled = active;
  118. SetColliderActive(render.transform,active);
  119. }
  120. }
  121. toolActive = !active;
  122. }
  123. private void SetColliderActive(Transform tran, bool active)
  124. {
  125. foreach (var collider in tran.GetComponentsInChildren<Collider>())
  126. {
  127. collider.enabled = active;
  128. }
  129. }
  130. private void OnDisable()
  131. {
  132. if (leftController != null)
  133. {
  134. leftController.ButtonTwoPressed -= ButtonTwoClick;
  135. }
  136. }
  137. }
  138. public class ToolInfo
  139. {
  140. public GameObject tool;
  141. public Vector3 position_InPack;
  142. public Quaternion rotation_InPack;
  143. public Vector3 scale_InPack;
  144. public Vector3 scale_Real;
  145. public void Restore(Transform parent)
  146. {
  147. tool.transform.parent = parent;
  148. tool.transform.localPosition = position_InPack;
  149. tool.transform.localRotation = rotation_InPack;
  150. tool.transform.localScale = scale_InPack;
  151. }
  152. }