No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMP_MaterialManager.cs 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. //#define TMP_DEBUG_MODE
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. namespace TMPro
  6. {
  7. public static class TMP_MaterialManager
  8. {
  9. private static List<MaskingMaterial> m_materialList = new List<MaskingMaterial>();
  10. private static Dictionary<long, FallbackMaterial> m_fallbackMaterials = new Dictionary<long, FallbackMaterial>();
  11. private static Dictionary<int, long> m_fallbackMaterialLookup = new Dictionary<int, long>();
  12. private static List<FallbackMaterial> m_fallbackCleanupList = new List<FallbackMaterial>();
  13. private static bool isFallbackListDirty;
  14. static TMP_MaterialManager()
  15. {
  16. Canvas.willRenderCanvases += OnPreRender;
  17. }
  18. static void OnPreRender()
  19. {
  20. if (isFallbackListDirty)
  21. {
  22. //Debug.Log("2 - Cleaning up Fallback Materials.");
  23. CleanupFallbackMaterials();
  24. isFallbackListDirty = false;
  25. }
  26. }
  27. /// <summary>
  28. /// Create a Masking Material Instance for the given ID
  29. /// </summary>
  30. /// <param name="baseMaterial"></param>
  31. /// <param name="stencilID"></param>
  32. /// <returns></returns>
  33. public static Material GetStencilMaterial(Material baseMaterial, int stencilID)
  34. {
  35. // Check if Material supports masking
  36. if (!baseMaterial.HasProperty(ShaderUtilities.ID_StencilID))
  37. {
  38. Debug.LogWarning("Selected Shader does not support Stencil Masking. Please select the Distance Field or Mobile Distance Field Shader.");
  39. return baseMaterial;
  40. }
  41. int baseMaterialID = baseMaterial.GetInstanceID();
  42. // If baseMaterial already has a corresponding masking material, return it.
  43. for (int i = 0; i < m_materialList.Count; i++)
  44. {
  45. if (m_materialList[i].baseMaterial.GetInstanceID() == baseMaterialID && m_materialList[i].stencilID == stencilID)
  46. {
  47. m_materialList[i].count += 1;
  48. #if TMP_DEBUG_MODE
  49. ListMaterials();
  50. #endif
  51. return m_materialList[i].stencilMaterial;
  52. }
  53. }
  54. // No matching masking material found. Create and return a new one.
  55. Material stencilMaterial;
  56. //Create new Masking Material Instance for this Base Material
  57. stencilMaterial = new Material(baseMaterial);
  58. stencilMaterial.hideFlags = HideFlags.HideAndDontSave;
  59. #if UNITY_EDITOR
  60. stencilMaterial.name += " Masking ID:" + stencilID;
  61. #endif
  62. stencilMaterial.shaderKeywords = baseMaterial.shaderKeywords;
  63. // Set Stencil Properties
  64. ShaderUtilities.GetShaderPropertyIDs();
  65. stencilMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
  66. //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilOp, 0);
  67. stencilMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 4);
  68. //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, stencilID);
  69. //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 0);
  70. MaskingMaterial temp = new MaskingMaterial();
  71. temp.baseMaterial = baseMaterial;
  72. temp.stencilMaterial = stencilMaterial;
  73. temp.stencilID = stencilID;
  74. temp.count = 1;
  75. m_materialList.Add(temp);
  76. #if TMP_DEBUG_MODE
  77. ListMaterials();
  78. #endif
  79. return stencilMaterial;
  80. }
  81. /// <summary>
  82. /// Function to release the stencil material.
  83. /// </summary>
  84. /// <param name="stencilMaterial"></param>
  85. public static void ReleaseStencilMaterial(Material stencilMaterial)
  86. {
  87. int stencilMaterialID = stencilMaterial.GetInstanceID();
  88. for (int i = 0; i < m_materialList.Count; i++)
  89. {
  90. if (m_materialList[i].stencilMaterial.GetInstanceID() == stencilMaterialID)
  91. {
  92. if (m_materialList[i].count > 1)
  93. m_materialList[i].count -= 1;
  94. else
  95. {
  96. Object.DestroyImmediate(m_materialList[i].stencilMaterial);
  97. m_materialList.RemoveAt(i);
  98. stencilMaterial = null;
  99. }
  100. break;
  101. }
  102. }
  103. #if TMP_DEBUG_MODE
  104. ListMaterials();
  105. #endif
  106. }
  107. // Function which returns the base material associated with a Masking Material
  108. public static Material GetBaseMaterial(Material stencilMaterial)
  109. {
  110. // Check if maskingMaterial already has a base material associated with it.
  111. int index = m_materialList.FindIndex(item => item.stencilMaterial == stencilMaterial);
  112. if (index == -1)
  113. return null;
  114. else
  115. return m_materialList[index].baseMaterial;
  116. }
  117. /// <summary>
  118. /// Function to set the Material Stencil ID
  119. /// </summary>
  120. /// <param name="material"></param>
  121. /// <param name="stencilID"></param>
  122. /// <returns></returns>
  123. public static Material SetStencil(Material material, int stencilID)
  124. {
  125. material.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
  126. if (stencilID == 0)
  127. material.SetFloat(ShaderUtilities.ID_StencilComp, 8);
  128. else
  129. material.SetFloat(ShaderUtilities.ID_StencilComp, 4);
  130. return material;
  131. }
  132. public static void AddMaskingMaterial(Material baseMaterial, Material stencilMaterial, int stencilID)
  133. {
  134. // Check if maskingMaterial already has a base material associated with it.
  135. int index = m_materialList.FindIndex(item => item.stencilMaterial == stencilMaterial);
  136. if (index == -1)
  137. {
  138. MaskingMaterial temp = new MaskingMaterial();
  139. temp.baseMaterial = baseMaterial;
  140. temp.stencilMaterial = stencilMaterial;
  141. temp.stencilID = stencilID;
  142. temp.count = 1;
  143. m_materialList.Add(temp);
  144. }
  145. else
  146. {
  147. stencilMaterial = m_materialList[index].stencilMaterial;
  148. m_materialList[index].count += 1;
  149. }
  150. }
  151. public static void RemoveStencilMaterial(Material stencilMaterial)
  152. {
  153. // Check if maskingMaterial is already on the list.
  154. int index = m_materialList.FindIndex(item => item.stencilMaterial == stencilMaterial);
  155. if (index != -1)
  156. {
  157. m_materialList.RemoveAt(index);
  158. }
  159. #if TMP_DEBUG_MODE
  160. ListMaterials();
  161. #endif
  162. }
  163. public static void ReleaseBaseMaterial(Material baseMaterial)
  164. {
  165. // Check if baseMaterial already has a masking material associated with it.
  166. int index = m_materialList.FindIndex(item => item.baseMaterial == baseMaterial);
  167. if (index == -1)
  168. {
  169. Debug.Log("No Masking Material exists for " + baseMaterial.name);
  170. }
  171. else
  172. {
  173. if (m_materialList[index].count > 1)
  174. {
  175. m_materialList[index].count -= 1;
  176. Debug.Log("Removed (1) reference to " + m_materialList[index].stencilMaterial.name + ". There are " + m_materialList[index].count + " references left.");
  177. }
  178. else
  179. {
  180. Debug.Log("Removed last reference to " + m_materialList[index].stencilMaterial.name + " with ID " + m_materialList[index].stencilMaterial.GetInstanceID());
  181. Object.DestroyImmediate(m_materialList[index].stencilMaterial);
  182. m_materialList.RemoveAt(index);
  183. }
  184. }
  185. #if TMP_DEBUG_MODE
  186. ListMaterials();
  187. #endif
  188. }
  189. public static void ClearMaterials()
  190. {
  191. if (m_materialList.Count == 0)
  192. {
  193. Debug.Log("Material List has already been cleared.");
  194. return;
  195. }
  196. for (int i = 0; i < m_materialList.Count; i++)
  197. {
  198. //Material baseMaterial = m_materialList[i].baseMaterial;
  199. Material stencilMaterial = m_materialList[i].stencilMaterial;
  200. Object.DestroyImmediate(stencilMaterial);
  201. }
  202. m_materialList.Clear();
  203. }
  204. /// <summary>
  205. /// Function to get the Stencil ID
  206. /// </summary>
  207. /// <param name="obj"></param>
  208. /// <returns></returns>
  209. public static int GetStencilID(GameObject obj)
  210. {
  211. // Implementation is almost copied from Unity UI
  212. var count = 0;
  213. var transform = obj.transform;
  214. var stopAfter = FindRootSortOverrideCanvas(transform);
  215. if (transform == stopAfter)
  216. return count;
  217. var t = transform.parent;
  218. var components = TMP_ListPool<Mask>.Get();
  219. while (t != null)
  220. {
  221. t.GetComponents<Mask>(components);
  222. for (var i = 0; i < components.Count; ++i)
  223. {
  224. var mask = components[i];
  225. if (mask != null && mask.MaskEnabled() && mask.graphic.IsActive())
  226. {
  227. ++count;
  228. break;
  229. }
  230. }
  231. if (t == stopAfter)
  232. break;
  233. t = t.parent;
  234. }
  235. TMP_ListPool<Mask>.Release(components);
  236. return Mathf.Min((1 << count) - 1, 255);
  237. }
  238. public static Material GetMaterialForRendering(MaskableGraphic graphic, Material baseMaterial)
  239. {
  240. if (baseMaterial == null)
  241. return null;
  242. var modifiers = TMP_ListPool<IMaterialModifier>.Get();
  243. graphic.GetComponents(modifiers);
  244. var result = baseMaterial;
  245. for (int i = 0; i < modifiers.Count; i++)
  246. result = modifiers[i].GetModifiedMaterial(result);
  247. TMP_ListPool<IMaterialModifier>.Release(modifiers);
  248. return result;
  249. }
  250. private static Transform FindRootSortOverrideCanvas(Transform start)
  251. {
  252. // Implementation is copied from Unity UI
  253. var canvasList = TMP_ListPool<Canvas>.Get();
  254. start.GetComponentsInParent(false, canvasList);
  255. Canvas canvas = null;
  256. for (int i = 0; i < canvasList.Count; ++i)
  257. {
  258. canvas = canvasList[i];
  259. // We found the canvas we want to use break
  260. if (canvas.overrideSorting)
  261. break;
  262. }
  263. TMP_ListPool<Canvas>.Release(canvasList);
  264. return canvas != null ? canvas.transform : null;
  265. }
  266. internal static Material GetFallbackMaterial(TMP_FontAsset fontAsset, Material sourceMaterial, int atlasIndex)
  267. {
  268. int sourceMaterialID = sourceMaterial.GetInstanceID();
  269. Texture tex = fontAsset.atlasTextures[atlasIndex];
  270. int texID = tex.GetInstanceID();
  271. long key = (long)sourceMaterialID << 32 | (long)(uint)texID;
  272. FallbackMaterial fallback;
  273. if (m_fallbackMaterials.TryGetValue(key, out fallback))
  274. {
  275. // Check if source material properties have changed.
  276. int sourceMaterialCRC = sourceMaterial.ComputeCRC();
  277. if (sourceMaterialCRC == fallback.sourceMaterialCRC)
  278. return fallback.fallbackMaterial;
  279. CopyMaterialPresetProperties(sourceMaterial, fallback.fallbackMaterial);
  280. fallback.sourceMaterialCRC = sourceMaterialCRC;
  281. return fallback.fallbackMaterial;
  282. }
  283. // Create new material from the source material and assign relevant atlas texture
  284. Material fallbackMaterial = new Material(sourceMaterial);
  285. fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, tex);
  286. fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;
  287. #if UNITY_EDITOR
  288. fallbackMaterial.name += " + " + tex.name;
  289. #endif
  290. fallback = new FallbackMaterial();
  291. fallback.fallbackID = key;
  292. fallback.sourceMaterial = fontAsset.material;
  293. fallback.sourceMaterialCRC = sourceMaterial.ComputeCRC();
  294. fallback.fallbackMaterial = fallbackMaterial;
  295. fallback.count = 0;
  296. m_fallbackMaterials.Add(key, fallback);
  297. m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);
  298. #if TMP_DEBUG_MODE
  299. ListFallbackMaterials();
  300. #endif
  301. return fallbackMaterial;
  302. }
  303. /// <summary>
  304. /// This function returns a material instance using the material properties of a previous material but using the font atlas texture of the new font asset.
  305. /// </summary>
  306. /// <param name="sourceMaterial">The material containing the source material properties to be copied to the new material.</param>
  307. /// <param name="targetMaterial">The font atlas texture that should be assigned to the new material.</param>
  308. /// <returns></returns>
  309. public static Material GetFallbackMaterial (Material sourceMaterial, Material targetMaterial)
  310. {
  311. int sourceID = sourceMaterial.GetInstanceID();
  312. Texture tex = targetMaterial.GetTexture(ShaderUtilities.ID_MainTex);
  313. int texID = tex.GetInstanceID();
  314. long key = (long)sourceID << 32 | (long)(uint)texID;
  315. FallbackMaterial fallback;
  316. if (m_fallbackMaterials.TryGetValue(key, out fallback))
  317. {
  318. // Check if source material properties have changed.
  319. int sourceMaterialCRC = sourceMaterial.ComputeCRC();
  320. if (sourceMaterialCRC == fallback.sourceMaterialCRC)
  321. return fallback.fallbackMaterial;
  322. CopyMaterialPresetProperties(sourceMaterial, fallback.fallbackMaterial);
  323. fallback.sourceMaterialCRC = sourceMaterialCRC;
  324. return fallback.fallbackMaterial;
  325. }
  326. // Create new material from the source material and copy properties if using distance field shaders.
  327. Material fallbackMaterial;
  328. if (sourceMaterial.HasProperty(ShaderUtilities.ID_GradientScale) && targetMaterial.HasProperty(ShaderUtilities.ID_GradientScale))
  329. {
  330. fallbackMaterial = new Material(sourceMaterial);
  331. fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;
  332. #if UNITY_EDITOR
  333. fallbackMaterial.name += " + " + tex.name;
  334. //Debug.Log("Creating new fallback material for " + fallbackMaterial.name);
  335. #endif
  336. fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, tex);
  337. // Retain material properties unique to target material.
  338. fallbackMaterial.SetFloat(ShaderUtilities.ID_GradientScale, targetMaterial.GetFloat(ShaderUtilities.ID_GradientScale));
  339. fallbackMaterial.SetFloat(ShaderUtilities.ID_TextureWidth, targetMaterial.GetFloat(ShaderUtilities.ID_TextureWidth));
  340. fallbackMaterial.SetFloat(ShaderUtilities.ID_TextureHeight, targetMaterial.GetFloat(ShaderUtilities.ID_TextureHeight));
  341. fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightNormal, targetMaterial.GetFloat(ShaderUtilities.ID_WeightNormal));
  342. fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightBold, targetMaterial.GetFloat(ShaderUtilities.ID_WeightBold));
  343. }
  344. else
  345. {
  346. // TODO: Need to add material property copy for bitmap materials as well
  347. fallbackMaterial = new Material(targetMaterial);
  348. fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;
  349. #if UNITY_EDITOR
  350. fallbackMaterial.name += " + " + tex.name;
  351. #endif
  352. }
  353. fallback = new FallbackMaterial();
  354. fallback.fallbackID = key;
  355. fallback.sourceMaterial = sourceMaterial;
  356. fallback.sourceMaterialCRC = sourceMaterial.ComputeCRC();
  357. fallback.fallbackMaterial = fallbackMaterial;
  358. fallback.count = 0;
  359. m_fallbackMaterials.Add(key, fallback);
  360. m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);
  361. #if TMP_DEBUG_MODE
  362. ListFallbackMaterials();
  363. #endif
  364. return fallbackMaterial;
  365. }
  366. /// <summary>
  367. ///
  368. /// </summary>
  369. /// <param name="targetMaterial"></param>
  370. public static void AddFallbackMaterialReference(Material targetMaterial)
  371. {
  372. if (targetMaterial == null) return;
  373. int sourceID = targetMaterial.GetInstanceID();
  374. long key;
  375. // Lookup key to retrieve
  376. if (m_fallbackMaterialLookup.TryGetValue(sourceID, out key))
  377. {
  378. FallbackMaterial fallback;
  379. if (m_fallbackMaterials.TryGetValue(key, out fallback))
  380. {
  381. //Debug.Log("Adding Fallback material " + fallback.fallbackMaterial.name + " with reference count of " + (fallback.count + 1));
  382. fallback.count += 1;
  383. }
  384. }
  385. }
  386. /// <summary>
  387. ///
  388. /// </summary>
  389. /// <param name="targetMaterial"></param>
  390. public static void RemoveFallbackMaterialReference(Material targetMaterial)
  391. {
  392. if (targetMaterial == null) return;
  393. int sourceID = targetMaterial.GetInstanceID();
  394. long key;
  395. // Lookup key to retrieve
  396. if (m_fallbackMaterialLookup.TryGetValue(sourceID, out key))
  397. {
  398. FallbackMaterial fallback;
  399. if (m_fallbackMaterials.TryGetValue(key, out fallback))
  400. {
  401. fallback.count -= 1;
  402. if (fallback.count < 1)
  403. m_fallbackCleanupList.Add(fallback);
  404. }
  405. }
  406. }
  407. /// <summary>
  408. ///
  409. /// </summary>
  410. public static void CleanupFallbackMaterials()
  411. {
  412. // Return if the list is empty.
  413. if (m_fallbackCleanupList.Count == 0) return;
  414. for (int i = 0; i < m_fallbackCleanupList.Count; i++)
  415. {
  416. FallbackMaterial fallback = m_fallbackCleanupList[i];
  417. if (fallback.count < 1)
  418. {
  419. //Debug.Log("Cleaning up " + fallback.fallbackMaterial.name);
  420. Material mat = fallback.fallbackMaterial;
  421. m_fallbackMaterials.Remove(fallback.fallbackID);
  422. m_fallbackMaterialLookup.Remove(mat.GetInstanceID());
  423. Object.DestroyImmediate(mat);
  424. mat = null;
  425. }
  426. }
  427. m_fallbackCleanupList.Clear();
  428. }
  429. /// <summary>
  430. /// Function to release the fallback material.
  431. /// </summary>
  432. /// <param name="fallbackMaterial">Material to be released.</param>
  433. public static void ReleaseFallbackMaterial(Material fallbackMaterial)
  434. {
  435. if (fallbackMaterial == null) return;
  436. int materialID = fallbackMaterial.GetInstanceID();
  437. long key;
  438. if (m_fallbackMaterialLookup.TryGetValue(materialID, out key))
  439. {
  440. FallbackMaterial fallback;
  441. if (m_fallbackMaterials.TryGetValue(key, out fallback))
  442. {
  443. //Debug.Log("Releasing Fallback material " + fallback.fallbackMaterial.name + " with remaining reference count of " + (fallback.count - 1));
  444. fallback.count -= 1;
  445. if (fallback.count < 1)
  446. m_fallbackCleanupList.Add(fallback);
  447. }
  448. }
  449. isFallbackListDirty = true;
  450. #if TMP_DEBUG_MODE
  451. ListFallbackMaterials();
  452. #endif
  453. }
  454. private class FallbackMaterial
  455. {
  456. public long fallbackID;
  457. public Material sourceMaterial;
  458. internal int sourceMaterialCRC;
  459. public Material fallbackMaterial;
  460. public int count;
  461. }
  462. private class MaskingMaterial
  463. {
  464. public Material baseMaterial;
  465. public Material stencilMaterial;
  466. public int count;
  467. public int stencilID;
  468. }
  469. /// <summary>
  470. /// Function to copy the properties of a source material preset to another while preserving the unique font asset properties of the destination material.
  471. /// </summary>
  472. /// <param name="source"></param>
  473. /// <param name="destination"></param>
  474. public static void CopyMaterialPresetProperties(Material source, Material destination)
  475. {
  476. if (!source.HasProperty(ShaderUtilities.ID_GradientScale) || !destination.HasProperty(ShaderUtilities.ID_GradientScale))
  477. return;
  478. // Save unique material properties
  479. Texture dst_texture = destination.GetTexture(ShaderUtilities.ID_MainTex);
  480. float dst_gradientScale = destination.GetFloat(ShaderUtilities.ID_GradientScale);
  481. float dst_texWidth = destination.GetFloat(ShaderUtilities.ID_TextureWidth);
  482. float dst_texHeight = destination.GetFloat(ShaderUtilities.ID_TextureHeight);
  483. float dst_weightNormal = destination.GetFloat(ShaderUtilities.ID_WeightNormal);
  484. float dst_weightBold = destination.GetFloat(ShaderUtilities.ID_WeightBold);
  485. // Make sure the same shader is used
  486. destination.shader = source.shader;
  487. // Copy all material properties
  488. destination.CopyPropertiesFromMaterial(source);
  489. // Copy shader keywords
  490. destination.shaderKeywords = source.shaderKeywords;
  491. // Restore unique material properties
  492. destination.SetTexture(ShaderUtilities.ID_MainTex, dst_texture);
  493. destination.SetFloat(ShaderUtilities.ID_GradientScale, dst_gradientScale);
  494. destination.SetFloat(ShaderUtilities.ID_TextureWidth, dst_texWidth);
  495. destination.SetFloat(ShaderUtilities.ID_TextureHeight, dst_texHeight);
  496. destination.SetFloat(ShaderUtilities.ID_WeightNormal, dst_weightNormal);
  497. destination.SetFloat(ShaderUtilities.ID_WeightBold, dst_weightBold);
  498. }
  499. #if TMP_DEBUG_MODE
  500. /// <summary>
  501. ///
  502. /// </summary>
  503. public static void ListMaterials()
  504. {
  505. if (m_materialList.Count == 0)
  506. {
  507. Debug.Log("Material List is empty.");
  508. return;
  509. }
  510. //Debug.Log("List contains " + m_materialList.Count() + " items.");
  511. for (int i = 0; i < m_materialList.Count; i++)
  512. {
  513. Material baseMaterial = m_materialList[i].baseMaterial;
  514. Material stencilMaterial = m_materialList[i].stencilMaterial;
  515. Debug.Log("Item #" + (i + 1) + " - Base Material is [" + baseMaterial.name + "] with ID " + baseMaterial.GetInstanceID() + " is associated with [" + (stencilMaterial != null ? stencilMaterial.name : "Null") + "] Stencil ID " + m_materialList[i].stencilID + " with ID " + (stencilMaterial != null ? stencilMaterial.GetInstanceID() : 0) + " and is referenced " + m_materialList[i].count + " time(s).");
  516. }
  517. }
  518. /// <summary>
  519. ///
  520. /// </summary>
  521. public static void ListFallbackMaterials()
  522. {
  523. if (m_fallbackMaterials.Count == 0)
  524. {
  525. Debug.Log("Material List is empty.");
  526. return;
  527. }
  528. Debug.Log("List contains " + m_fallbackMaterials.Count + " items.");
  529. int count = 0;
  530. foreach (var fallback in m_fallbackMaterials)
  531. {
  532. Material baseMaterial = fallback.Value.baseMaterial;
  533. Material fallbackMaterial = fallback.Value.fallbackMaterial;
  534. string output = "Item #" + (count++);
  535. if (baseMaterial != null)
  536. output += " - Base Material is [" + baseMaterial.name + "] with ID " + baseMaterial.GetInstanceID();
  537. if (fallbackMaterial != null)
  538. output += " is associated with [" + fallbackMaterial.name + "] with ID " + fallbackMaterial.GetInstanceID() + " and is referenced " + fallback.Value.count + " time(s).";
  539. Debug.Log(output);
  540. }
  541. }
  542. #endif
  543. }
  544. }