subere23

NRSRManager Update

Sep 28th, 2017
677
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. using HoloToolkit.Unity;
  5. using HoloToolkit.Unity.InputModule;
  6.  
  7. public class NRSRManager : Singleton<NRSRManager>
  8. {
  9. public Renderer[] ObjectsInScene;
  10. public List<GameObject> FilterObjectsInScene = new List<GameObject>();
  11. //Used to trigger next object update
  12. public int TotalNumberOfObjects = 0;
  13. public int PreviousFrameObjectCount = 0;
  14.  
  15. public static GameObject FocusedObject;
  16.  
  17. public delegate void OnObjectFocused();
  18. public static event OnObjectFocused ObjectFocused;
  19. public static event OnObjectFocused ObjectUnFocused;
  20.  
  21. //just in case we need - these numbers added together should always = total number of objects
  22. public int numberofVisibleObjects;
  23. public int numberOfFilteredObjects;
  24.  
  25. public Material BoundingBoxMat;
  26.  
  27.  
  28. public RaycastHit hitInfo;
  29. public static bool holdSelectedObject_LookingAtTransformTool;
  30. public static bool holdSelectedObject_UsingTransformTool;
  31. LayerMask layerMask;
  32. public static Vector3 menuPosition;
  33.  
  34.  
  35. private void Start()
  36. {
  37.  
  38. layerMask = 1 << 23;
  39. layerMask = ~layerMask;
  40.  
  41. }
  42.  
  43. private void Update()
  44. {
  45. if (holdSelectedObject_UsingTransformTool)
  46. {
  47. return;
  48. }
  49.  
  50. RayCastToHoldFocusedObject();
  51. menuPosition = hitInfo.point;
  52.  
  53. if (holdSelectedObject_LookingAtTransformTool)
  54. {
  55. if (ObjectFocused != null)
  56. {
  57. ObjectFocused();
  58. }
  59. return;
  60.  
  61. }
  62. else
  63. {
  64.  
  65. FocusedObject = null;
  66. if (ObjectUnFocused != null)
  67. {
  68.  
  69. ObjectUnFocused();
  70. }
  71. }
  72. }
  73.  
  74. public void RayCastToHoldFocusedObject()
  75. {
  76. if (Physics.Raycast(Camera.main.transform.position,
  77. Camera.main.transform.forward,
  78. out hitInfo,
  79. Mathf.Infinity,
  80. layerMask))
  81. {
  82. Debug.Log(hitInfo.transform.root.name);
  83.  
  84. if (hitInfo.transform == null)
  85. {
  86. Debug.Log("hitInfo Transform null");
  87. NRSRManager.holdSelectedObject_LookingAtTransformTool = false;
  88. return;
  89. }
  90.  
  91. if (FocusedObject != null)
  92. {
  93. if (FocusedObject.transform.root.name == hitInfo.transform.root.name)
  94. {
  95. NRSRManager.holdSelectedObject_LookingAtTransformTool = true;
  96. }
  97. else
  98. {
  99. NRSRManager.holdSelectedObject_LookingAtTransformTool = false;
  100. }
  101. }
  102.  
  103. }
  104. else
  105. {
  106. NRSRManager.holdSelectedObject_LookingAtTransformTool = false;
  107. }
  108. }
  109.  
  110. public static void SendFocusedObjectToManager(GameObject go)
  111. {
  112. if (holdSelectedObject_LookingAtTransformTool)
  113. {
  114. return;
  115. }
  116. FocusedObject = go;
  117. Debug.Log(go.name + " to NRSRManager");
  118.  
  119. }
  120. public static void ClearFocusedObjectFromManager()
  121. {
  122. if (holdSelectedObject_LookingAtTransformTool)
  123. {
  124. return;
  125. }
  126. FocusedObject = null;
  127.  
  128. }
  129.  
  130. void FixedUpdate()
  131. {
  132.  
  133. FindObjectsInScene(); //Likely expensive to do every frame, dynamic systems tend to come at a cost.
  134.  
  135. TotalNumberOfObjects = ObjectsInScene.Length;
  136.  
  137. if (TotalNumberOfObjects != PreviousFrameObjectCount)
  138. {
  139. FilterUnneededObjects();
  140. numberofVisibleObjects = FilterObjectsInScene.Count;
  141.  
  142. foreach (GameObject go in FilterObjectsInScene)
  143. {
  144. if (go.transform.root.gameObject.GetComponent<BoundingBox>() == null)
  145. {
  146. BoundingBox box = go.transform.root.gameObject.AddComponent<BoundingBox>();
  147. go.transform.root.gameObject.AddComponent<FadeObjectNotActive>();
  148. box.isRootObject = true;
  149.  
  150.  
  151. }
  152. }
  153. }
  154. PreviousFrameObjectCount = ObjectsInScene.Length;
  155. }
  156.  
  157.  
  158. void FindObjectsInScene()
  159. {
  160. ObjectsInScene = null;
  161. ObjectsInScene = FindObjectsOfType<Renderer>();
  162. }
  163.  
  164. void FilterUnneededObjects()
  165. {
  166. FilterObjectsInScene.Clear();
  167. numberOfFilteredObjects = 0;
  168.  
  169. for (int i = 0; i < ObjectsInScene.Length; i++)
  170. {
  171. if (ObjectsInScene[i].gameObject.tag != "NRSRTools")
  172. {
  173. FilterObjectsInScene.Add(ObjectsInScene[i].gameObject);
  174. }
  175. else
  176. {
  177. numberOfFilteredObjects++;
  178. }
  179. }
  180. }
  181.  
  182.  
  183. }
RAW Paste Data