VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_Core_ConsoleCommands.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEditor;
5 using System.Linq;
6 
7 using AVR.Core;
8 using UnityEngine.XR.Management;
9 
10 namespace AVR.Core {
11  [ExecuteInEditMode]
12  /// <summary>
13  /// Declares a set of base DevConsole commands.
14  /// </summary>
16  {
17  #if UNITY_EDITOR
18  [InitializeOnLoadMethod]
19  #endif
20  [RuntimeInitializeOnLoadMethod]
21  static void InitCommands()
22  {
23  AVR_DevConsole.register_command("echo", (s) => AVR_DevConsole.print(string.Join(" ", s)), 1, "Prints a given message back to the user.");
24 
25  AVR_DevConsole.register_command("packages", (s) => print_packages(), "Prints a list of all installed arc-vr packages");
26 
27  AVR_DevConsole.register_command("clear", (s) => AVR_DevConsole.clear(), "Clears console output");
28  AVR_DevConsole.register_command("clr", (s) => AVR_DevConsole.clear(), "Clears console output");
29 
30  AVR_DevConsole.register_command("toggle", (s) => toggle_obj(s), 1, "Toggles a scene object with a given name.");
31 
32  AVR_DevConsole.register_command("prefabs", (s) => prefabSearch(s), 2, "Searches within the UnityEditor for prefabs. args[0] should be \"ui\" or \"props\" with the remainder being the search term.");
33 
34  AVR_DevConsole.register_command("spam", (s) => { for(int i=0; i<100; i++) AVR_DevConsole.print("output"+i); }, "Outputs a bunch of console messages. Used for console testing/debugging.");
35 
36  AVR_DevConsole.register_command("reconfigure", (s) => { AVR_Settings.reconfigure(); }, "Re-initializes AVR_Settings.");
37 
38  AVR_DevConsole.register_command("get", (s) => AVR_DevConsole.print(AVR_Settings.get_string(s[0])), 1, "Prints a setting with the given settings-token");
39 
40  AVR_DevConsole.register_command("set", (s) => AVR_Settings.settings[s[0]]=s[1], 2, "Sets a setting with settings-token arg[0] to the value of arg[1]");
41 
42  AVR_DevConsole.register_command("start_xr", (s) => AVR_PlayerRig.Instance.StartCoroutine(StartXR()));
43 
44  AVR_DevConsole.register_command("stop_xr", (s) => StopXR());
45  }
46 
47  private static IEnumerator StartXR()
48  {
49  Debug.Log("Initializing XR...");
50  yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
51 
52  if (XRGeneralSettings.Instance.Manager.activeLoader == null)
53  {
54  Debug.LogError("Initializing XR Failed. Check Editor or Player log for details.");
55  }
56  else
57  {
58  Debug.Log("Starting XR...");
59  XRGeneralSettings.Instance.Manager.StartSubsystems();
60  }
61  }
62 
63  private static void StopXR()
64  {
65  Debug.Log("Stopping XR...");
66 
67  XRGeneralSettings.Instance.Manager.StopSubsystems();
68  XRGeneralSettings.Instance.Manager.DeinitializeLoader();
69  Debug.Log("XR stopped completely.");
70  }
71 
72  private static void toggle_obj(string[] args) {
73  string remainder = string.Join(" ", args);
74  GameObject o = (GameObject)AVR.Core.Utils.Misc.GlobalFind(remainder, typeof(GameObject));
75  if (o == null)
76  {
77  AVR_DevConsole.error("Could not find object with name " + remainder);
78  }
79  else
80  {
81  o.SetActive(!o.activeSelf);
82  AVR_DevConsole.success("Toggled object " + o.name);
83  }
84  }
85 
86  private static void print_packages() {
87  AVR_DevConsole.success("Installed packages:");
88  #if AVR_CORE
89  AVR_DevConsole.success("> AVR_CORE");
90  #endif
91  #if AVR_AVATAR
92  AVR_DevConsole.success("> AVR_AVATAR");
93  #endif
94  #if AVR_MOTION
95  AVR_DevConsole.success("> AVR_MOTION");
96  #endif
97  #if AVR_PHYS
98  AVR_DevConsole.success("> AVR_PHYS");
99  #endif
100  #if AVR_UI
101  AVR_DevConsole.success("> AVR_UI");
102  #endif
103  #if AVR_NET
104  AVR_DevConsole.success("> AVR_NET");
105  #endif
106  }
107 
108  private static void prefabSearch(string[] args) {
109  string remainder = string.Join(" ", args.Skip(1));
110  if (args[0] == "props")
111  {
112  AVR_DevConsole.print("Displaying Avr_Prop labeled prefabs with query: " + remainder);
113  SetSearch("l:Avr_Prop " + remainder);
114  }
115  if (args[0] == "ui")
116  {
117  AVR_DevConsole.print("Displaying Avr_Ui labeled prefabs with query: " + remainder);
118  SetSearch("l:Avr_Ui " + remainder);
119  }
120  }
121 
122  // To check how to set certain searchfilters, check: https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/Mono/ProjectWindow/SearchFilter.cs#L278
123  // or alternatively: SearchUtility.cs
124  // At the time of writing it is:
125  // t:<query> for classnames
126  // l:<query> for assetlabels
127  // v:<query> for versioncontrolstates
128  // b:<query> for assetbundles
129  // glob:<query> for ???
130  private static void SetSearch(string searchstring)
131  {
132  try
133  {
134  // Get type UnityEditor.ProjectBrowser from the UnityEditor Assembly
135  // Check https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ProjectBrowser.cs
136  // for source code on this class
137  System.Type upb_t = System.Type.GetType("UnityEditor.ProjectBrowser, UnityEditor");
138 
139  // Get the first object of this type
140  Object projectBrowser = Resources.FindObjectsOfTypeAll(upb_t)[0];
141  if (projectBrowser == null)
142  {
143  AVR_DevConsole.error("Could not get an active ProjectBrowser.");
144  return;
145  }
146 
147  //// Set Searchstring
148  // get method SeatSearch(string) from UnityEditor.ProjectBrowser object
149  System.Reflection.MethodInfo setSearchInfo = upb_t.GetMethod("SetSearch", new[] { typeof(string) });
150 
151  setSearchInfo.Invoke(projectBrowser, new object[] { searchstring });
152  }
153  catch (System.Exception e)
154  {
155  AVR_DevConsole.error("Encountered an exception while setting search-string. Do you not have a ProjectBrowser open, or are you using a different Unity version?");
156  AVR_DevConsole.error("Stacktrace: " + e.StackTrace);
157  }
158  }
159  }
160 }
Represents the players VR setup. Only one instance at a time is allowed.
static void SetSearch(string searchstring)
Class for the arc-vr developer's console. Functions as a singleton with only static members...
Contains constan values, settings etc. which can all be set through *.avr files. Duplicate settings w...
Definition: AVR_Settings.cs:14
static Dictionary< string, string > settings
Dictionary containing all settings by their key. A setting declared as domain = { setting = true } wi...
Definition: AVR_Settings.cs:42
static void print(string s)
Print a given string on the console.
Declares a set of base DevConsole commands.
static string get_string(string token)
Get a registered setting of the type string.
Definition: AVR_Settings.cs:88
static void clear()
Clear console output entirely.