Unity5を使い自作ゲームを作っているのですが、Debug.logを表示について聞きたいことがあります。
現在複数のステージを作り1つ1つをつなげて1つのゲームを作成しようとしています。
ステージ1(Main)からステージ2(Stage2)に移動する際に画像にありますように左下にルール等を表示しています。
スタート直後のステージ1では、きれいに表示がされるのですが、ステージ1からステージ2に移る際に同じ文章が重複してしまいます。
初心者なものでして、なぜこのようになってしまうのかがわかりません。
重複をなくすにはどうしたらよいか、教えてください。お願いします。
なおこの表示に関しては、Assets Storeより"ScreenLogor"というものをインポートして使っています。
それぞれのスクリプトを下記に貼っていきます。※使用言語はC#です。
C#
1<枠と仕組み? ScreenLogger.cs> 2 3using UnityEngine; 4using System.Collections.Generic; 5 6public class ScreenLogger : MonoBehaviour 7{ 8 public enum LogAnchor 9 { 10 TopLeft, 11 TopRight, 12 BottomLeft, 13 BottomRight 14 } 15 16 public bool IsPersistent = true; 17 public bool ShowInEditor = true; 18 19 [Tooltip("Height of the log area as a percentage of the screen height")] 20 [Range(0.3f, 1.0f)] 21 public float Height = 0.5f; 22 23 [Tooltip("Width of the log area as a percentage of the screen width")] 24 [Range(0.3f, 1.0f)] 25 public float Width = 0.5f; 26 27 public int Margin = 20; 28 29 public LogAnchor AnchorPosition = LogAnchor.BottomLeft; 30 31 public int FontSize = 14; 32 33 [Range(0f, 01f)] 34 public float BackgroundOpacity = 0.5f; 35 public Color BackgroundColor = Color.black; 36 37 public bool LogMessages = true; 38 public bool LogWarnings = true; 39 public bool LogErrors = true; 40 41 public Color MessageColor = Color.white; 42 public Color WarningColor = Color.yellow; 43 public Color ErrorColor = new Color(1, 0.5f, 0.5f); 44 45 public bool StackTraceMessages = false; 46 public bool StackTraceWarnings = false; 47 public bool StackTraceErrors = true; 48 49 static Queue<LogMessage> queue = new Queue<LogMessage>(); 50 51 GUIStyle styleContainer, styleText; 52 int padding = 5; 53 54 public void Awake() 55 { 56 Texture2D back = new Texture2D(1, 1); 57 BackgroundColor.a = BackgroundOpacity; 58 back.SetPixel(0, 0, BackgroundColor); 59 back.Apply(); 60 61 styleContainer = new GUIStyle(); 62 styleContainer.normal.background = back; 63 styleContainer.wordWrap = true; 64 styleContainer.padding = new RectOffset(padding, padding, padding, padding); 65 66 styleText = new GUIStyle(); 67 styleText.fontSize = FontSize; 68 69 if (IsPersistent) 70 DontDestroyOnLoad(this); 71 } 72 73 void OnEnable() 74 { 75 if (!ShowInEditor && Application.isEditor) return; 76 77 queue = new Queue<LogMessage>(); 78 79#if UNITY_4_5 || UNITY_4_6 80 Application.RegisterLogCallback(HandleLog); 81#else 82 Application.logMessageReceived += HandleLog; 83#endif 84 } 85 86 void OnDisable() 87 { 88 if (!ShowInEditor && Application.isEditor) return; 89 90#if UNITY_4_5 || UNITY_4_6 91 Application.RegisterLogCallback(null); 92#else 93 Application.logMessageReceived -= HandleLog; 94#endif 95 } 96 97 void Update() 98 { 99 if (!ShowInEditor && Application.isEditor) return; 100 101 while (queue.Count > ((Screen.height - 2 * Margin) * Height - 2 * padding) / styleText.lineHeight) 102 queue.Dequeue(); 103 } 104 105 void OnGUI() 106 { 107 if (!ShowInEditor && Application.isEditor) return; 108 109 float w = (Screen.width - 2 * Margin) * Width; 110 float h = (Screen.height - 2 * Margin) * Height; 111 float x = 1, y = 1; 112 113 switch (AnchorPosition) 114 { 115 case LogAnchor.BottomLeft: 116 x = Margin; 117 y = Margin + (Screen.height - 2 * Margin) * (1 - Height); 118 break; 119 120 case LogAnchor.BottomRight: 121 x = Margin + (Screen.width - 2 * Margin) * (1 - Width); 122 y = Margin + (Screen.height - 2 * Margin) * (1 - Height); 123 break; 124 125 case LogAnchor.TopLeft: 126 x = Margin; 127 y = Margin; 128 break; 129 130 case LogAnchor.TopRight: 131 x = Margin + (Screen.width - 2 * Margin) * (1 - Width); 132 y = Margin; 133 break; 134 } 135 136 GUILayout.BeginArea(new Rect(x, y, w, h), styleContainer); 137 138 foreach (LogMessage m in queue) 139 { 140 switch (m.Type) 141 { 142 case LogType.Warning: 143 styleText.normal.textColor = WarningColor; 144 break; 145 146 case LogType.Log: 147 styleText.normal.textColor = MessageColor; 148 break; 149 150 case LogType.Assert: 151 case LogType.Exception: 152 case LogType.Error: 153 styleText.normal.textColor = ErrorColor; 154 break; 155 156 default: 157 styleText.normal.textColor = MessageColor; 158 break; 159 } 160 161 GUILayout.Label(m.Message, styleText); 162 } 163 164 GUILayout.EndArea(); 165 } 166 167 void HandleLog(string message, string stackTrace, LogType type) 168 { 169 if (type == LogType.Assert && !LogErrors) return; 170 if (type == LogType.Error && !LogErrors) return; 171 if (type == LogType.Exception && !LogErrors) return; 172 if (type == LogType.Log && !LogMessages) return; 173 if (type == LogType.Warning && !LogWarnings) return; 174 175 queue.Enqueue(new LogMessage(message, type)); 176 177 if (type == LogType.Assert && !StackTraceErrors) return; 178 if (type == LogType.Error && !StackTraceErrors) return; 179 if (type == LogType.Exception && !StackTraceErrors) return; 180 if (type == LogType.Log && !StackTraceMessages) return; 181 if (type == LogType.Warning && !StackTraceWarnings) return; 182 183 string[] trace = stackTrace.Split(new char[] { '\n' }); 184 185 foreach (string t in trace) 186 if (t.Length != 0) queue.Enqueue(new LogMessage(" " + t, type)); 187 } 188} 189 190class LogMessage 191{ 192 public string Message; 193 public LogType Type; 194 195 public LogMessage(string msg, LogType type) 196 { 197 Message = msg; 198 Type = type; 199 } 200} 201
C#
1<ステージ1用 Test.cs> 2using UnityEngine; 3using System.Collections; 4 5public class Test : MonoBehaviour 6{ 7 float timeout = 5, lasttime = -1; 8 int i = 0; 9 10 void Start() 11 { 12 TestMessageTypes(); 13 } 14 15 void TestMessageTypes() 16 { 17 Debug.Log("スタート地点に立ちました。"); 18 Debug.Log("ゴールを目指して頑張ってください"); 19 Debug.LogWarning("十字キーで移動できます。"); 20 } 21} 22
C#
1<ステージ2用 Stage2.cs> 2using UnityEngine; 3using System.Collections; 4 5public class Stage2 : MonoBehaviour 6{ 7 float timeout = 5, lasttime = -1; 8 int i = 0; 9 10 void Start() 11 { 12 TestMessageTypes(); 13 } 14 void TestMessageTypes() 15 { 16 Debug.Log("第2ステージです。"); 17 Debug.LogWarning("Spaceでジャンプできます。"); 18 Debug.LogWarning("助走が必要です。"); 19 ClearConsole(); 20 } 21 22 static void ClearConsole() { 23 var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll"); 24 var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); 25 clearMethod.Invoke(null,null); 26 } 27}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/23 04:30