#if !UNITY_2019_3_OR_NEWER #define CINEMACHINE_UNITY_IMGUI #endif using UnityEngine; using System.Collections.Generic; using System.Text; namespace Cinemachine.Utility { /// Manages onscreen positions for Cinemachine debugging output public class CinemachineDebug { static HashSet mClients; #if CINEMACHINE_UNITY_IMGUI /// Release a screen rectangle previously obtained through GetScreenPos() /// The client caller. Used as a handle. public static void ReleaseScreenPos(Object client) { if (mClients != null && mClients.Contains(client)) mClients.Remove(client); } /// Reserve an on-screen rectangle for debugging output. /// The client caller. This is used as a handle. /// Sample text, for determining rectangle size /// What style will be used to draw, used here for /// determining rect size /// An area on the game screen large enough to print the text /// in the style indicated public static Rect GetScreenPos(Object client, string text, GUIStyle style) { if (mClients == null) mClients = new HashSet(); if (!mClients.Contains(client)) mClients.Add(client); var pos = Vector2.zero; Vector2 size = style.CalcSize(new GUIContent(text)); if (mClients != null) { foreach (var c in mClients) { if (c == client) break; pos.y += size.y; } } return new Rect(pos, size); } #endif /// /// Delegate for OnGUI debugging. /// This will be called by the CinemachineBrain in its OnGUI (editor only) /// public delegate void OnGUIDelegate(); /// /// Delegate for OnGUI debugging. /// This will be called by the CinemachineBrain in its OnGUI (editor only) /// public static OnGUIDelegate OnGUIHandlers; private static List mAvailableStringBuilders; /// Get a preallocated StringBuilder from the pool /// The preallocated StringBuilder from the pool. /// Client must call ReturnToPool when done public static StringBuilder SBFromPool() { if (mAvailableStringBuilders == null || mAvailableStringBuilders.Count == 0) return new StringBuilder(); var sb = mAvailableStringBuilders[mAvailableStringBuilders.Count - 1]; mAvailableStringBuilders.RemoveAt(mAvailableStringBuilders.Count - 1); sb.Length = 0; return sb; } /// Return a StringBuilder to the preallocated pool /// The string builder object to return to the pool public static void ReturnToPool(StringBuilder sb) { if (mAvailableStringBuilders == null) mAvailableStringBuilders = new List(); mAvailableStringBuilders.Add(sb); } } }