Hue Preserving Color Blending
TMP_UpdateManager.cs
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections.Generic;
4 
5 #if UNITY_2018_1_OR_NEWER
6 using UnityEngine.Experimental.Rendering;
7 #endif
8 
9 
10 namespace TMPro
11 {
12 
13  public class TMP_UpdateManager
14  {
15  private static TMP_UpdateManager s_Instance;
16 
17  private readonly List<TMP_Text> m_LayoutRebuildQueue = new List<TMP_Text>();
18  private Dictionary<int, int> m_LayoutQueueLookup = new Dictionary<int, int>();
19 
20  private readonly List<TMP_Text> m_GraphicRebuildQueue = new List<TMP_Text>();
21  private Dictionary<int, int> m_GraphicQueueLookup = new Dictionary<int, int>();
22 
23  //private bool m_PerformingGraphicRebuild;
24  //private bool m_PerformingLayoutRebuild;
25 
29  public static TMP_UpdateManager instance
30  {
31  get
32  {
33  if (TMP_UpdateManager.s_Instance == null)
34  TMP_UpdateManager.s_Instance = new TMP_UpdateManager();
35  return TMP_UpdateManager.s_Instance;
36  }
37  }
38 
39 
43  protected TMP_UpdateManager()
44  {
45  Camera.onPreCull += OnCameraPreCull;
46 
47  #if UNITY_2018_1_OR_NEWER
48  RenderPipeline.beginFrameRendering += OnBeginFrameRendering;
49  #endif
50  }
51 
52 
57  public static void RegisterTextElementForLayoutRebuild(TMP_Text element)
58  {
59  TMP_UpdateManager.instance.InternalRegisterTextElementForLayoutRebuild(element);
60  }
61 
62  private bool InternalRegisterTextElementForLayoutRebuild(TMP_Text element)
63  {
64  int id = element.GetInstanceID();
65 
66  if (this.m_LayoutQueueLookup.ContainsKey(id))
67  return false;
68 
69  m_LayoutQueueLookup[id] = id;
70  this.m_LayoutRebuildQueue.Add(element);
71 
72  return true;
73  }
74 
75 
80  public static void RegisterTextElementForGraphicRebuild(TMP_Text element)
81  {
82  TMP_UpdateManager.instance.InternalRegisterTextElementForGraphicRebuild(element);
83  }
84 
85  private bool InternalRegisterTextElementForGraphicRebuild(TMP_Text element)
86  {
87  int id = element.GetInstanceID();
88 
89  if (this.m_GraphicQueueLookup.ContainsKey(id))
90  return false;
91 
92  m_GraphicQueueLookup[id] = id;
93  this.m_GraphicRebuildQueue.Add(element);
94 
95  return true;
96  }
97 
102  void OnBeginFrameRendering(Camera[] cameras)
103  {
104  // Exclude the PreRenderCamera
105  #if UNITY_EDITOR
106  if (cameras.Length == 1 && cameras[0].cameraType == CameraType.Preview)
107  return;
108  #endif
109  DoRebuilds();
110  }
111 
116  void OnCameraPreCull(Camera cam)
117  {
118  // Exclude the PreRenderCamera
119  #if UNITY_EDITOR
120  if (cam.cameraType == CameraType.Preview)
121  return;
122  #endif
123  DoRebuilds();
124  }
125 
129  void DoRebuilds()
130  {
131  // Handle Layout Rebuild Phase
132  for (int i = 0; i < m_LayoutRebuildQueue.Count; i++)
133  {
134  m_LayoutRebuildQueue[i].Rebuild(CanvasUpdate.Prelayout);
135  }
136 
137  if (m_LayoutRebuildQueue.Count > 0)
138  {
139  m_LayoutRebuildQueue.Clear();
140  m_LayoutQueueLookup.Clear();
141  }
142 
143  // Handle Graphic Rebuild Phase
144  for (int i = 0; i < m_GraphicRebuildQueue.Count; i++)
145  {
146  m_GraphicRebuildQueue[i].Rebuild(CanvasUpdate.PreRender);
147  }
148 
149  // If there are no objects in the queue, we don't need to clear the lists again.
150  if (m_GraphicRebuildQueue.Count > 0)
151  {
152  m_GraphicRebuildQueue.Clear();
153  m_GraphicQueueLookup.Clear();
154  }
155  }
156 
157 
162  public static void UnRegisterTextElementForRebuild(TMP_Text element)
163  {
164  TMP_UpdateManager.instance.InternalUnRegisterTextElementForGraphicRebuild(element);
165  TMP_UpdateManager.instance.InternalUnRegisterTextElementForLayoutRebuild(element);
166  }
167 
168  private void InternalUnRegisterTextElementForGraphicRebuild(TMP_Text element)
169  {
170  //if (this.m_PerformingGraphicRebuild)
171  //{
172  // Debug.LogError((object)string.Format("Trying to remove {0} from rebuild list while we are already inside a rebuild loop. This is not supported.", (object)element));
173  //}
174  //else
175  //{
176  int id = element.GetInstanceID();
177 
178  //element.LayoutComplete();
179  TMP_UpdateManager.instance.m_GraphicRebuildQueue.Remove(element);
180  m_GraphicQueueLookup.Remove(id);
181  //}
182  }
183 
184  private void InternalUnRegisterTextElementForLayoutRebuild(TMP_Text element)
185  {
186  //if (this.m_PerformingLayoutRebuild)
187  //{
188  // Debug.LogError((object)string.Format("Trying to remove {0} from rebuild list while we are already inside a rebuild loop. This is not supported.", (object)element));
189  //}
190  //else
191  //{
192  int id = element.GetInstanceID();
193 
194  //element.LayoutComplete();
195  TMP_UpdateManager.instance.m_LayoutRebuildQueue.Remove(element);
196  m_LayoutQueueLookup.Remove(id);
197  //}
198  }
199 
200 
201  }
202 }
static void RegisterTextElementForLayoutRebuild(TMP_Text element)
Function to register elements which require a layout rebuild.
static void RegisterTextElementForGraphicRebuild(TMP_Text element)
Function to register elements which require a layout rebuild.
Base class which contains common properties and functions shared between the TextMeshPro and TextMesh...
Definition: TMP_Text.cs:110
static void UnRegisterTextElementForRebuild(TMP_Text element)
Function to unregister elements which no longer require a rebuild.
TMP_UpdateManager()
Register to receive rendering callbacks.
void OnCameraPreCull(Camera cam)
Callback which occurs just before the cam is rendered.
static TMP_UpdateManager instance
Get a singleton instance of the registry
void DoRebuilds()
Process the rebuild requests in the rebuild queues.
void OnBeginFrameRendering(Camera[] cameras)
Callback which occurs just before the Scriptable Render Pipeline (SRP) begins rendering.