GuideHeightLightComponent.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class GuideHeightLightComponent : MonoBehaviour {
  6. public int order=1;
  7. private GuideChangeData status=new GuideChangeData();
  8. private void OnEnable()
  9. {
  10. RunGuide = true;
  11. }
  12. public void ClearGuide()
  13. {
  14. Canvas canvas = gameObject.GetComponent<Canvas>();
  15. GraphicRaycaster graphic = gameObject.GetComponent<GraphicRaycaster>();
  16. if (graphic != null && status.isCreateGraphic)
  17. {
  18. Destroy(graphic);
  19. }
  20. if (canvas != null && status.isCreateCanvas)
  21. {
  22. Destroy(canvas);
  23. }
  24. else
  25. {
  26. if (canvas != null)
  27. {
  28. canvas.overrideSorting = status.OldOverrideSorting;
  29. canvas.sortingOrder = status.OldSortingOrder;
  30. canvas.sortingLayerName = status.oldSortingLayerName;
  31. }
  32. }
  33. }
  34. public void SetHeightLight()
  35. {
  36. Canvas canvas = GetComponent<Canvas>();
  37. GraphicRaycaster graphic = GetComponent<GraphicRaycaster>();
  38. if (canvas == null)
  39. {
  40. canvas = gameObject. AddComponent<Canvas>();
  41. status.isCreateCanvas = true;
  42. }
  43. if (graphic == null)
  44. {
  45. graphic =gameObject. AddComponent<GraphicRaycaster>();
  46. status.isCreateGraphic = true;
  47. }
  48. status.OldOverrideSorting = canvas.overrideSorting;
  49. status.OldSortingOrder = canvas.sortingOrder;
  50. status.oldSortingLayerName = canvas.sortingLayerName;
  51. ////如果检测到目标对象
  52. //bool oldActive = gameObject.activeSelf;
  53. //if (!oldActive)
  54. //{
  55. // gameObject.SetActive(true);
  56. //}
  57. canvas.overrideSorting = true;
  58. canvas.sortingOrder = order;
  59. canvas.sortingLayerName = "Guide";
  60. //if (!oldActive)
  61. //{
  62. // gameObject.SetActive(false);
  63. //}
  64. }
  65. bool RunGuide = false;
  66. private void Update()
  67. {
  68. if(RunGuide)
  69. {
  70. RunGuide = false;
  71. SetHeightLight();
  72. }
  73. }
  74. protected struct GuideChangeData
  75. {
  76. public bool isCreateCanvas;
  77. public bool isCreateGraphic;
  78. public string oldSortingLayerName;
  79. public int OldSortingOrder;
  80. public bool OldOverrideSorting;
  81. }
  82. }