CinemachineDebug.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #if !UNITY_2019_3_OR_NEWER
  2. #define CINEMACHINE_UNITY_IMGUI
  3. #endif
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. namespace Cinemachine.Utility
  8. {
  9. /// <summary>Manages onscreen positions for Cinemachine debugging output</summary>
  10. public class CinemachineDebug
  11. {
  12. static HashSet<Object> mClients;
  13. #if CINEMACHINE_UNITY_IMGUI
  14. /// <summary>Release a screen rectangle previously obtained through GetScreenPos()</summary>
  15. /// <param name="client">The client caller. Used as a handle.</param>
  16. public static void ReleaseScreenPos(Object client)
  17. {
  18. if (mClients != null && mClients.Contains(client))
  19. mClients.Remove(client);
  20. }
  21. /// <summary>Reserve an on-screen rectangle for debugging output.</summary>
  22. /// <param name="client">The client caller. This is used as a handle.</param>
  23. /// <param name="text">Sample text, for determining rectangle size</param>
  24. /// <param name="style">What style will be used to draw, used here for
  25. /// determining rect size</param>
  26. /// <returns>An area on the game screen large enough to print the text
  27. /// in the style indicated</returns>
  28. public static Rect GetScreenPos(Object client, string text, GUIStyle style)
  29. {
  30. if (mClients == null)
  31. mClients = new HashSet<Object>();
  32. if (!mClients.Contains(client))
  33. mClients.Add(client);
  34. var pos = Vector2.zero;
  35. Vector2 size = style.CalcSize(new GUIContent(text));
  36. if (mClients != null)
  37. {
  38. foreach (var c in mClients)
  39. {
  40. if (c == client)
  41. break;
  42. pos.y += size.y;
  43. }
  44. }
  45. return new Rect(pos, size);
  46. }
  47. #endif
  48. /// <summary>
  49. /// Delegate for OnGUI debugging.
  50. /// This will be called by the CinemachineBrain in its OnGUI (editor only)
  51. /// </summary>
  52. public delegate void OnGUIDelegate();
  53. /// <summary>
  54. /// Delegate for OnGUI debugging.
  55. /// This will be called by the CinemachineBrain in its OnGUI (editor only)
  56. /// </summary>
  57. public static OnGUIDelegate OnGUIHandlers;
  58. private static List<StringBuilder> mAvailableStringBuilders;
  59. /// <summary>Get a preallocated StringBuilder from the pool</summary>
  60. /// <returns>The preallocated StringBuilder from the pool.
  61. /// Client must call ReturnToPool when done</returns>
  62. public static StringBuilder SBFromPool()
  63. {
  64. if (mAvailableStringBuilders == null || mAvailableStringBuilders.Count == 0)
  65. return new StringBuilder();
  66. var sb = mAvailableStringBuilders[mAvailableStringBuilders.Count - 1];
  67. mAvailableStringBuilders.RemoveAt(mAvailableStringBuilders.Count - 1);
  68. sb.Length = 0;
  69. return sb;
  70. }
  71. /// <summary>Return a StringBuilder to the preallocated pool</summary>
  72. /// <param name="sb">The string builder object to return to the pool</param>
  73. public static void ReturnToPool(StringBuilder sb)
  74. {
  75. if (mAvailableStringBuilders == null)
  76. mAvailableStringBuilders = new List<StringBuilder>();
  77. mAvailableStringBuilders.Add(sb);
  78. }
  79. }
  80. }