SortingLayer.cs 981 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [RequireComponent(typeof(Renderer))]
  5. public class SortingLayer : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. this.LayerName = this.layerName;
  10. this.OrderInLayer = this.orderInLayer;
  11. }
  12. private void OnValidate()
  13. {
  14. this.LayerName = this.layerName;
  15. this.OrderInLayer = this.orderInLayer;
  16. }
  17. public string LayerName
  18. {
  19. get
  20. {
  21. return this.layerName;
  22. }
  23. set
  24. {
  25. this.layerName = value;
  26. foreach (Renderer renderer in base.GetComponents<Renderer>())
  27. {
  28. renderer.sortingLayerName = this.layerName;
  29. }
  30. }
  31. }
  32. public int OrderInLayer
  33. {
  34. get
  35. {
  36. return this.orderInLayer;
  37. }
  38. set
  39. {
  40. this.orderInLayer = value;
  41. foreach (Renderer renderer in base.GetComponents<Renderer>())
  42. {
  43. renderer.sortingOrder = this.orderInLayer;
  44. }
  45. }
  46. }
  47. [SerializeField]
  48. [SortingLayer]
  49. private string layerName = "Default";
  50. [SerializeField]
  51. private int orderInLayer;
  52. private Renderer _renderer;
  53. }