EditorEventHandler.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. /*
  8. * 相机上必须有发射射线的组件
  9. * UI上直接添加此脚本
  10. * 3D物体必须有碰撞器
  11. */
  12. public class EditorEventHandler : MonoBehaviour, EventObserver
  13. {
  14. [SerializeField, Tooltip("当前素材是否可通过点击进行编辑")]
  15. private bool m_IsClickEditor = true;
  16. [SerializeField]
  17. private MaterialType m_Type;
  18. public MaterialType Type
  19. {
  20. get { return m_Type; }
  21. set { m_Type = value; }
  22. }
  23. public bool IsClickEditor
  24. {
  25. get { return m_IsClickEditor; }
  26. set
  27. {
  28. m_IsClickEditor = value;
  29. if (gameObject.GetComponent<Collider>() != null)
  30. {
  31. gameObject.GetComponent<Collider>().enabled = m_IsClickEditor;
  32. }
  33. }
  34. }
  35. private void Awake()
  36. {
  37. OnAwake();
  38. EventManager.Instance.Register(this, EventID.EVENT_1);
  39. }
  40. public virtual void OnAwake()
  41. {
  42. if (gameObject.GetComponent<Collider>() == null)
  43. {
  44. gameObject.AddComponent<BoxCollider>();
  45. }
  46. gameObject.GetComponent<Collider>().enabled = m_IsClickEditor;
  47. ClickEventTriggerListener.Get(gameObject).onPointerClick = new ClickEventTriggerListener.PointDelegate(OnClick);
  48. }
  49. public virtual void OnClick(PointerEventData eventData)
  50. {
  51. Debug.Log(gameObject.name);
  52. EventData1 data1 = new EventData1(EventID.EVENT_1, gameObject);
  53. data1.Send();
  54. }
  55. public void HandleEvent(EventData data)
  56. {
  57. switch (data.ID)
  58. {
  59. case EventID.EVENT_1:
  60. EventData1 data1 = (EventData1)data;
  61. if (data1.SelectObj == gameObject)
  62. {
  63. //当前物体外轮廓发光
  64. ChangeShow(true);
  65. }
  66. else
  67. {
  68. //当前物体外轮廓隐藏显示
  69. ChangeShow(false);
  70. }
  71. break;
  72. case EventID.EVENT_2:
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. public void ChangeShow(bool isShow)
  79. {
  80. //Debug.Log(gameObject);
  81. }
  82. private void OnDestroy()
  83. {
  84. //EventManager.Instance.Remove(this);
  85. }
  86. }