UIEffectHelper.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace QFramework
  6. {
  7. public class UIEffectHelper
  8. {
  9. public static void AddUIEffect(Transform parent, GameObject effectRoot, int offsetOrder = 1)
  10. {
  11. if (parent == null || effectRoot == null)
  12. {
  13. return;
  14. }
  15. int sortingOrder = offsetOrder;
  16. Canvas parentCanvas = parent.GetComponentInParent<Canvas>();
  17. if (parentCanvas != null)
  18. {
  19. sortingOrder += parentCanvas.sortingOrder;
  20. }
  21. effectRoot.transform.SetParent(parent, true);
  22. Renderer[] childs = effectRoot.GetComponentsInChildren<Renderer>(true);
  23. if (childs != null)
  24. {
  25. for (int i = 0; i < childs.Length; ++i)
  26. {
  27. childs[i].sortingOrder = sortingOrder;
  28. }
  29. }
  30. }
  31. public static int GetLayerOffset(Transform parent, int offsetOrder = -1)
  32. {
  33. if (parent == null)
  34. {
  35. return offsetOrder;
  36. }
  37. int sortingOrder = offsetOrder;
  38. Canvas parentCanvas = parent.GetComponentInParent<Canvas>();
  39. if (parentCanvas != null)
  40. {
  41. sortingOrder += parentCanvas.sortingOrder;
  42. }
  43. return sortingOrder;
  44. }
  45. }
  46. }