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.

Light2DManager.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. internal static class Light2DManager
  6. {
  7. private static SortingLayer[] s_SortingLayers;
  8. public static List<Light2D> lights { get; } = new List<Light2D>();
  9. // Called during OnEnable
  10. public static void RegisterLight(Light2D light)
  11. {
  12. Debug.Assert(!lights.Contains(light));
  13. lights.Add(light);
  14. ErrorIfDuplicateGlobalLight(light);
  15. }
  16. // Called during OnEnable
  17. public static void DeregisterLight(Light2D light)
  18. {
  19. Debug.Assert(lights.Contains(light));
  20. lights.Remove(light);
  21. }
  22. public static void ErrorIfDuplicateGlobalLight(Light2D light)
  23. {
  24. if (light.lightType != Light2D.LightType.Global)
  25. return;
  26. foreach (var sortingLayer in light.affectedSortingLayers)
  27. {
  28. // should this really trigger at runtime?
  29. if (ContainsDuplicateGlobalLight(sortingLayer, light.blendStyleIndex))
  30. Debug.LogError("More than one global light on layer " + SortingLayer.IDToName(sortingLayer) + " for light blend style index " + light.blendStyleIndex);
  31. }
  32. }
  33. public static bool GetGlobalColor(int sortingLayerIndex, int blendStyleIndex, out Color color)
  34. {
  35. var foundGlobalColor = false;
  36. color = Color.black;
  37. // This should be rewritten to search only global lights
  38. foreach (var light in lights)
  39. {
  40. if (light.lightType != Light2D.LightType.Global ||
  41. light.blendStyleIndex != blendStyleIndex ||
  42. !light.IsLitLayer(sortingLayerIndex))
  43. continue;
  44. var inCurrentPrefabStage = true;
  45. #if UNITY_EDITOR
  46. // If we found the first global light in our prefab stage
  47. inCurrentPrefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage()?.IsPartOfPrefabContents(light.gameObject) ?? true;
  48. #endif
  49. if (inCurrentPrefabStage)
  50. {
  51. color = light.color * light.intensity;
  52. return true;
  53. }
  54. else
  55. {
  56. if (!foundGlobalColor)
  57. {
  58. color = light.color * light.intensity;
  59. foundGlobalColor = true;
  60. }
  61. }
  62. }
  63. return foundGlobalColor;
  64. }
  65. private static bool ContainsDuplicateGlobalLight(int sortingLayerIndex, int blendStyleIndex)
  66. {
  67. var globalLightCount = 0;
  68. // This should be rewritten to search only global lights
  69. foreach (var light in lights)
  70. {
  71. if (light.lightType == Light2D.LightType.Global &&
  72. light.blendStyleIndex == blendStyleIndex &&
  73. light.IsLitLayer(sortingLayerIndex))
  74. {
  75. #if UNITY_EDITOR
  76. // If we found the first global light in our prefab stage
  77. if (UnityEditor.SceneManagement.PrefabStageUtility.GetPrefabStage(light.gameObject) == UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage())
  78. #endif
  79. {
  80. if (globalLightCount > 0)
  81. return true;
  82. globalLightCount++;
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. public static SortingLayer[] GetCachedSortingLayer()
  89. {
  90. if (s_SortingLayers is null)
  91. s_SortingLayers = SortingLayer.layers;
  92. return s_SortingLayers;
  93. }
  94. }
  95. }