Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
ViveToolsPanel.cs
Go to the documentation of this file.
1using Spelunx;
2using Spelunx.Vive;
3using UnityEditor;
4using UnityEngine;
5using UnityEngine.UIElements;
6using UnityEditor.SceneManagement;
7using UnityEngine.SceneManagement;
8
9namespace Spelunx
10{
11 public class ViveToolsPanel : EditorWindow
12 {
13 [SerializeField]
14 private VisualTreeAsset m_VisualTreeAsset = default;
15
16 private UnityEngine.Object viveManagerPrefab;
17 private UnityEngine.Object viveTrackerPrefab;
18 private UnityEngine.Object viveControllerPrefab;
19 private GameObject viveManagerInstance;
20 private int trackerCount = 0;
21 private int controllerCount = 0;
22 private Label trackerCountLabel;
23 private Label controllerCountLabel;
24
25 // places tools under CAVERN toolbar with hierarchy ordering
26 [MenuItem("CAVERN/VIVE Tracker Tools", false, 101)]
27 public static void ShowExample()
28 {
29 ViveToolsPanel wnd = GetWindow<ViveToolsPanel>();
30 wnd.titleContent = new GUIContent("Vive Tracker Tools");
31 }
32
33 public void CreateGUI()
34 {
35 // Each editor window contains a root VisualElement object
36 VisualElement root = rootVisualElement;
37
38 // Instantiate UXML, UI setup in UXML document
39 VisualElement panelSetup = m_VisualTreeAsset.Instantiate();
40 root.Add(panelSetup);
41
42 // Add button functionality for vive setup
43 VisualElement viveSetupButton = root.Q("ViveSetupButton");
44 viveSetupButton.RegisterCallback<ClickEvent>(ViveSetup);
45
46 // Add button functionality for vive controller setup
47 VisualElement viveControllerSetupButton = root.Q("ViveControllerSetupButton");
48 viveControllerSetupButton.RegisterCallback<ClickEvent>(ViveControllerSetup);
49
50 trackerCountLabel = root.Q<Label>("TrackerCount");
51 controllerCountLabel = root.Q<Label>("ControllerCount");
52 EditorApplication.hierarchyChanged += OnHierarchyChanged;
53
54
55 // Add button functionality for all of the building block interactions
56 Button followButton = root.Q<Button>("FollowButton");
57 followButton.RegisterCallback<ClickEvent>(AddBuildingBlock<FollowInteraction>);
58
59 Button orbitButton = root.Q<Button>("OrbitButton");
60 orbitButton.RegisterCallback<ClickEvent>(AddBuildingBlock<OrbitCavernInteraction>);
61
62 Button evadeButton = root.Q<Button>("EvadeButton");
63 evadeButton.RegisterCallback<ClickEvent>(AddBuildingBlock<EvadeInteraction>);
64
65 Button lookAtButton = root.Q<Button>("LookAtButton");
66 lookAtButton.RegisterCallback<ClickEvent>(AddBuildingBlock<LookAt>);
67
68 Button zonesButton = root.Q<Button>("ZonesButton");
69 zonesButton.RegisterCallback<ClickEvent>(AddZones);
70
71 }
72
73 private void OnHierarchyChanged()
74 {
75 var trackers = FindObjectsByType<ViveTracker>(FindObjectsSortMode.None);
76 var controllers = FindObjectsByType<ViveController>(FindObjectsSortMode.None);
77 // update information if number of Vive trackers in scene changes
78 if (trackerCount != trackers.Length)
79 {
80 trackerCount = trackers.Length;
81 trackerCountLabel.text = "VIVE Trackers in Scene: " + trackerCount;
82 }
83
84 if (controllerCount != controllers.Length)
85 {
86 controllerCount = controllers.Length;
87 controllerCountLabel.text = "VIVE Controllers in Scene: " + controllerCount;
88 }
89 }
90
91 // adds vive tracker
92 private void ViveSetup(ClickEvent evt)
93 {
94 // load from path
95 viveManagerPrefab = (GameObject)AssetDatabase.LoadAssetAtPath("Packages/com.spelunx.cavern.vive-trackers/Prefabs/ViveTrackerManager.prefab", typeof(GameObject));
96 viveTrackerPrefab = (GameObject)AssetDatabase.LoadAssetAtPath("Packages/com.spelunx.cavern.vive-trackers/Prefabs/ViveTracker.prefab", typeof(GameObject));
97
98 // check if vive tracker manager is already present
99 var manager = FindObjectsByType<Vive_Manager>(FindObjectsSortMode.None);
100
101 // adds manager if not present in scene
102 if (manager.Length == 0)
103 {
104 viveManagerInstance = (GameObject)PrefabUtility.InstantiatePrefab(viveManagerPrefab as GameObject);
105
106 // set vive manager to be in the CAVERN setup folder in the scene hierarchy
107 GameObject cavernSetup = GameObject.Find("CavernSetup");
108 if (cavernSetup != null)
109 {
110 viveManagerInstance.GetComponent<Transform>().parent = cavernSetup.transform;
111 // load in the debug keys
112 cavernSetup.AddComponent<ViveDebugKeys>();
113 }
114 }
115
116 // instantiate a new vive tracker and set its origin to the vive manager
117 GameObject viveTrackerInstance = (GameObject)PrefabUtility.InstantiatePrefab(viveTrackerPrefab as GameObject);
118 viveTrackerInstance.GetComponent<ViveTracker>().SetOrigin(FindObjectsByType<Vive_Manager>(FindObjectsSortMode.None)[0].transform);
119
120 // mark scene as edited to prompt saving
121 EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
122 }
123
124 // Adds vive controller
125 private void ViveControllerSetup(ClickEvent evt)
126 {
127 // load from path
128 viveManagerPrefab = (GameObject)AssetDatabase.LoadAssetAtPath("Packages/com.spelunx.cavern.vive-trackers/Prefabs/ViveTrackerManager.prefab", typeof(GameObject));
129 viveControllerPrefab = (GameObject)AssetDatabase.LoadAssetAtPath("Packages/com.spelunx.cavern.vive-trackers/Prefabs/ViveController.prefab", typeof(GameObject));
130
131 // check if vive tracker manager is already present
132 var manager = FindObjectsByType<Vive_Manager>(FindObjectsSortMode.None);
133
134 // adds manager if not present in scene
135 if (manager.Length == 0)
136 {
137 viveManagerInstance = (GameObject)PrefabUtility.InstantiatePrefab(viveManagerPrefab as GameObject);
138
139 // set vive manager to be in the CAVERN setup folder in the scene hierarchy
140 GameObject cavernSetup = GameObject.Find("CavernSetup");
141 if (cavernSetup != null)
142 {
143 viveManagerInstance.GetComponent<Transform>().parent = cavernSetup.transform;
144 // load in the debug keys
145 cavernSetup.AddComponent<ViveDebugKeys>();
146 }
147 }
148
149 // instantiate a new vive tracker and set its origin to the vive manager
150 GameObject viveController = (GameObject)PrefabUtility.InstantiatePrefab(viveControllerPrefab as GameObject);
151 viveController.GetComponent<ViveController>().SetOrigin(FindObjectsByType<Vive_Manager>(FindObjectsSortMode.None)[0].transform);
152
153 // mark scene as edited to prompt saving
154 EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
155 }
156
157 // adds a building block script to the selected object
158 private void AddBuildingBlock<T>(ClickEvent evt) where T : Interaction
159 {
160 foreach (GameObject go in Selection.gameObjects)
161 {
162 if (go != null)
163 {
164 T interaction = go.AddComponent<T>();
165
166 // Set the target to the first vive tracker found by default.
167 ViveTracker defaultViveTracker = FindFirstObjectByType<ViveTracker>();
168 if (defaultViveTracker != null)
169 {
170 interaction.SetTarget(defaultViveTracker.transform);
171 }
172
173 // CavernInteraction specific
174 if (typeof(T).IsSubclassOf(typeof(CavernInteraction)))
175 {
176 CavernInteraction cavernInteraction = interaction as CavernInteraction;
177 cavernInteraction.SetCavernRenderer(FindFirstObjectByType<CavernRenderer>());
178 }
179 }
180 }
181 }
182
183 // adds zones to the CAVERN
184 private void AddZones(ClickEvent evt)
185 {
186 Zones component = FindObjectsByType<Vive_Manager>(FindObjectsSortMode.None)[0].gameObject.AddComponent(typeof(Zones)) as Zones;
187 component.cavern = FindFirstObjectByType<CavernRenderer>();
188 }
189 }
190}
static void ShowExample()
void SetCavernRenderer(CavernRenderer cavernRenderer)
Helpful debug information (both keyboard shortcuts and GUI) for Vive Trackers. This also adds the abi...