CinemachineDebug.cs 3.1 KB

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