Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ShadowCasterGroup2DManager.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. namespace UnityEngine.Rendering.Universal
  8. {
  9. #if UNITY_EDITOR
  10. [InitializeOnLoadAttribute]
  11. #endif
  12. internal class ShadowCasterGroup2DManager
  13. {
  14. static List<ShadowCasterGroup2D> s_ShadowCasterGroups = null;
  15. public static List<ShadowCasterGroup2D> shadowCasterGroups { get { return s_ShadowCasterGroups; } }
  16. #if UNITY_EDITOR
  17. static ShadowCasterGroup2DManager()
  18. {
  19. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  20. }
  21. private static void OnPlayModeStateChanged(PlayModeStateChange state)
  22. {
  23. if (s_ShadowCasterGroups != null && (state == PlayModeStateChange.ExitingEditMode || state == PlayModeStateChange.ExitingPlayMode))
  24. s_ShadowCasterGroups.Clear();
  25. }
  26. #endif
  27. public static void CacheValues()
  28. {
  29. if (shadowCasterGroups != null)
  30. {
  31. for (int i = 0; i < shadowCasterGroups.Count; i++)
  32. {
  33. if (shadowCasterGroups[i] != null)
  34. shadowCasterGroups[i].CacheValues();
  35. }
  36. }
  37. }
  38. public static void AddShadowCasterGroupToList(ShadowCasterGroup2D shadowCaster, List<ShadowCasterGroup2D> list)
  39. {
  40. if (list.Contains(shadowCaster))
  41. return;
  42. int positionToInsert = 0;
  43. for (positionToInsert = 0; positionToInsert < list.Count; positionToInsert++)
  44. {
  45. if (shadowCaster.m_Priority < list[positionToInsert].m_Priority)
  46. break;
  47. }
  48. list.Insert(positionToInsert, shadowCaster);
  49. }
  50. public static void RemoveShadowCasterGroupFromList(ShadowCasterGroup2D shadowCaster, List<ShadowCasterGroup2D> list)
  51. {
  52. list.Remove(shadowCaster);
  53. }
  54. static CompositeShadowCaster2D FindTopMostCompositeShadowCaster(ShadowCaster2D shadowCaster)
  55. {
  56. CompositeShadowCaster2D retGroup = null;
  57. Transform transformToCheck = shadowCaster.transform.parent;
  58. while (transformToCheck != null)
  59. {
  60. CompositeShadowCaster2D currentGroup;
  61. if (transformToCheck.TryGetComponent<CompositeShadowCaster2D>(out currentGroup))
  62. retGroup = currentGroup;
  63. transformToCheck = transformToCheck.parent;
  64. }
  65. return retGroup;
  66. }
  67. public static int GetRendereringPriority(ShadowCaster2D shadowCaster)
  68. {
  69. int sortingOrder = 0;
  70. // This should take sorting groups into account, but doesn't as there isn't a way to do this at the moment.
  71. Renderer renderer;
  72. if (shadowCaster.TryGetComponent<Renderer>(out renderer))
  73. sortingOrder = renderer.sortingOrder;
  74. return sortingOrder;
  75. }
  76. public static bool AddToShadowCasterGroup(ShadowCaster2D shadowCaster, ref ShadowCasterGroup2D shadowCasterGroup, ref int priority)
  77. {
  78. ShadowCasterGroup2D newShadowCasterGroup = FindTopMostCompositeShadowCaster(shadowCaster) as ShadowCasterGroup2D;
  79. int newPriority = 0;
  80. if (newShadowCasterGroup == null)
  81. {
  82. newPriority = GetRendereringPriority(shadowCaster);
  83. shadowCaster.TryGetComponent<ShadowCasterGroup2D>(out newShadowCasterGroup);
  84. }
  85. if (newShadowCasterGroup != null && (shadowCasterGroup != newShadowCasterGroup || priority != newPriority))
  86. {
  87. newShadowCasterGroup.RegisterShadowCaster2D(shadowCaster);
  88. shadowCasterGroup = newShadowCasterGroup;
  89. priority = newPriority;
  90. return true;
  91. }
  92. return false;
  93. }
  94. public static void RemoveFromShadowCasterGroup(ShadowCaster2D shadowCaster, ShadowCasterGroup2D shadowCasterGroup)
  95. {
  96. if (shadowCasterGroup != null)
  97. shadowCasterGroup.UnregisterShadowCaster2D(shadowCaster);
  98. if (shadowCasterGroup == shadowCaster)
  99. RemoveGroup(shadowCasterGroup);
  100. }
  101. public static void AddGroup(ShadowCasterGroup2D group)
  102. {
  103. if (group == null)
  104. return;
  105. if (s_ShadowCasterGroups == null)
  106. s_ShadowCasterGroups = new List<ShadowCasterGroup2D>();
  107. AddShadowCasterGroupToList(group, s_ShadowCasterGroups);
  108. }
  109. public static void RemoveGroup(ShadowCasterGroup2D group)
  110. {
  111. if (group != null && s_ShadowCasterGroups != null)
  112. RemoveShadowCasterGroupFromList(group, s_ShadowCasterGroups);
  113. }
  114. }
  115. }