| 1 | /************************************************************************************ |
| 2 | Filename : OVRDebugConsole.cs |
| 3 | Content : Write to a text string, used by UI.Text |
| 4 | Created : May 22, 2015 |
| 5 | Copyright : Copyright 2015 Oculus VR, Inc. All Rights reserved. |
| 6 | |
| 7 | Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); |
| 8 | you may not use the Oculus VR Rift SDK except in compliance with the License, |
| 9 | which is provided at the time of installation or download, or which |
| 10 | otherwise accompanies this software in either electronic or hard copy form. |
| 11 | |
| 12 | You may obtain a copy of the License at |
| 13 | |
| 14 | http://www.oculusvr.com/licenses/LICENSE-3.1 |
| 15 | |
| 16 | Unless required by applicable law or agreed to in writing, the Oculus VR SDK |
| 17 | distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | See the License for the specific language governing permissions and |
| 20 | limitations under the License. |
| 21 | ************************************************************************************/ |
| 22 | using UnityEngine; |
| 23 | using UnityEngine.UI; |
| 24 | using System.Collections; |
| 25 | |
| 26 | public class OVRDebugConsole : MonoBehaviour |
| 27 | { |
| 28 | public ArrayList messages = new ArrayList(); |
| 29 | public int maxMessages = 15; // The max number of messages displayed |
| 30 | public Text textMsg; // text string to display |
| 31 | |
| 32 | // Our instance to allow this script to be called without a direct connection. |
| 33 | private static OVRDebugConsole s_Instance = null; |
| 34 | |
| 35 | // Clear timeout |
| 36 | private bool clearTimeoutOn = false; |
| 37 | private float clearTimeout = 0.0f; |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Gets the instance. |
| 41 | /// </summary> |
| 42 | /// <value>The instance.</value> |
| 43 | public static OVRDebugConsole instance |
| 44 | { |
| 45 | get |
| 46 | { |
| 47 | if (s_Instance == null) |
| 48 | { |
| 49 | s_Instance = FindObjectOfType(typeof(OVRDebugConsole)) as OVRDebugConsole; |
| 50 | |
| 51 | if (s_Instance == null) |
| 52 | { |
| 53 | GameObject console = new GameObject(); |
| 54 | console.AddComponent<OVRDebugConsole>(); |
| 55 | console.name = "OVRDebugConsole"; |
| 56 | s_Instance = FindObjectOfType(typeof(OVRDebugConsole)) as OVRDebugConsole; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return s_Instance; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Awake this instance. |
| 66 | /// </summary> |
| 67 | void Awake() |
| 68 | { |
| 69 | s_Instance = this; |
| 70 | Init(); |
| 71 | |
| 72 | } |
| 73 | |
| 74 | /// <summary> |
| 75 | /// Update this instance. |
| 76 | /// </summary> |
| 77 | void Update() |
| 78 | { |
| 79 | if(clearTimeoutOn == true) |
| 80 | { |
| 81 | clearTimeout -= Time.deltaTime; |
| 82 | if(clearTimeout < 0.0f) |
| 83 | { |
| 84 | Clear(); |
| 85 | clearTimeout = 0.0f; |
| 86 | clearTimeoutOn = false; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
| 92 | /// Init this instance. |
| 93 | /// </summary> |
| 94 | public void Init() |
| 95 | { |
| 96 | if(textMsg == null) |
| 97 | { |
| 98 | Debug.LogWarning("DebugConsole Init WARNING::UI text not set. Will not be able to display anything."); |
| 99 | } |
| 100 | |
| 101 | Clear(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | //+++++++++ INTERFACE FUNCTIONS ++++++++++++++++++++++++++++++++ |
| 106 | |
| 107 | /// <summary> |
| 108 | /// Log the specified message. |
| 109 | /// </summary> |
| 110 | /// <param name="message">Message.</param> |
| 111 | public static void Log(string message) |
| 112 | { |
| 113 | OVRDebugConsole.instance.AddMessage(message, Color.white); |
| 114 | } |
| 115 | |
| 116 | /// <summary> |
| 117 | /// Log the specified message and color. |
| 118 | /// </summary> |
| 119 | /// <param name="message">Message.</param> |
| 120 | /// <param name="color">Color.</param> |
| 121 | public static void Log(string message, Color color) |
| 122 | { |
| 123 | OVRDebugConsole.instance.AddMessage(message, color); |
| 124 | } |
| 125 | |
| 126 | /// <summary> |
| 127 | /// Clear this instance. |
| 128 | /// </summary> |
| 129 | public static void Clear() |
| 130 | { |
| 131 | OVRDebugConsole.instance.ClearMessages(); |
| 132 | } |
| 133 | |
| 134 | /// <summary> |
| 135 | /// Calls clear after a certain time. |
| 136 | /// </summary> |
| 137 | /// <param name="timeToClear">Time to clear.</param> |
| 138 | public static void ClearTimeout(float timeToClear) |
| 139 | { |
| 140 | OVRDebugConsole.instance.SetClearTimeout(timeToClear); |
| 141 | } |
| 142 | |
| 143 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 144 | |
| 145 | |
| 146 | /// <summary> |
| 147 | /// Adds the message. |
| 148 | /// </summary> |
| 149 | /// <param name="message">Message.</param> |
| 150 | /// <param name="color">Color.</param> |
| 151 | public void AddMessage(string message, Color color) |
| 152 | { |
| 153 | messages.Add(message); |
| 154 | |
| 155 | if(textMsg != null) |
| 156 | textMsg.color = color; |
| 157 | |
| 158 | Display(); |
| 159 | } |
| 160 | |
| 161 | /// <summary> |
| 162 | /// Clears the messages. |
| 163 | /// </summary> |
| 164 | public void ClearMessages() |
| 165 | { |
| 166 | messages.Clear(); |
| 167 | Display(); |
| 168 | } |
| 169 | |
| 170 | /// <summary> |
| 171 | /// Sets the clear timeout. |
| 172 | /// </summary> |
| 173 | /// <param name="timeout">Timeout.</param> |
| 174 | public void SetClearTimeout(float timeout) |
| 175 | { |
| 176 | clearTimeout = timeout; |
| 177 | clearTimeoutOn = true; |
| 178 | } |
| 179 | |
| 180 | /// <summary> |
| 181 | // Prunes the array to fit within the maxMessages limit |
| 182 | /// </summary> |
| 183 | void Prune() |
| 184 | { |
| 185 | int diff; |
| 186 | if (messages.Count > maxMessages) |
| 187 | { |
| 188 | if (messages.Count <= 0) |
| 189 | { |
| 190 | diff = 0; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | diff = messages.Count - maxMessages; |
| 195 | } |
| 196 | messages.RemoveRange(0, (int)diff); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /// <summary> |
| 201 | /// Display this instance. |
| 202 | /// </summary> |
| 203 | void Display() |
| 204 | { |
| 205 | if (messages.Count > maxMessages) |
| 206 | { |
| 207 | Prune(); |
| 208 | } |
| 209 | |
| 210 | if(textMsg != null) |
| 211 | { |
| 212 | textMsg.text = ""; // Clear text out |
| 213 | int x = 0; |
| 214 | |
| 215 | while (x < messages.Count) |
| 216 | { |
| 217 | textMsg.text += (string)messages[x]; |
| 218 | textMsg.text +='\n'; |
| 219 | x += 1; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |