MaterialBtnItem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class MaterialBtnItem : MonoBehaviour, EventObserver
  7. {
  8. private Toggle m_Toggle;
  9. private Image m_Image;
  10. private Text m_Text;
  11. private GameObject m_MaterialObj;
  12. private ObjectValue m_ObjectValue;
  13. private MaterialObjValue m_MatValue;
  14. private bool m_IsSelect;
  15. private bool m_IsCollider;
  16. public GameObject MaterialObj
  17. {
  18. get { return m_MaterialObj; }
  19. set { m_MaterialObj = value; }
  20. }
  21. public ObjectValue ObjValue
  22. {
  23. get { return m_ObjectValue; }
  24. set { m_ObjectValue = value; }
  25. }
  26. public MaterialObjValue MatValue
  27. {
  28. get { return m_MatValue; }
  29. set { m_MatValue = value; }
  30. }
  31. public bool IsCollider
  32. {
  33. get { return m_IsCollider; }
  34. set { m_IsCollider = value; }
  35. }
  36. public bool IsSelect
  37. {
  38. get { return m_IsSelect; }
  39. set { m_IsSelect = value; }
  40. }
  41. private void Awake()
  42. {
  43. IsSelect = false;
  44. EventManager.Instance.Register(this, EventID.EVENT_1);
  45. m_Toggle = GetComponent<Toggle>();
  46. m_Image = transform.Find("Image").GetComponent<Image>();
  47. m_Text = transform.Find("Text").GetComponent<Text>(); ;
  48. m_Toggle.onValueChanged.AddListener(OnValueChanged);
  49. }
  50. private void LateUpdate()
  51. {
  52. if (IsCollider && MaterialObj != null)
  53. {
  54. MaterialObj.transform.localScale = new Vector3(MaterialObj.transform.localScale.x, 1, MaterialObj.transform.localScale.z);
  55. }
  56. }
  57. private void OnValueChanged(bool b)
  58. {
  59. if (b)
  60. {
  61. EventData1 data1 = new EventData1(EventID.EVENT_1, MaterialObj);
  62. if (!IsSelect)
  63. {
  64. data1.Send();
  65. }
  66. SelectObj(true);
  67. }
  68. }
  69. public void HandleEvent(EventData data)
  70. {
  71. switch (data.ID)
  72. {
  73. case EventID.EVENT_1:
  74. EventData1 data1 = (EventData1)data;
  75. if (!m_Toggle.isOn && data1.SelectObj == MaterialObj)
  76. {
  77. m_Toggle.isOn = true;
  78. IsSelect = true;
  79. }
  80. else
  81. {
  82. IsSelect = false;
  83. }
  84. break;
  85. case EventID.EVENT_2:
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. public void SetData(MaterialObjValue value, ObjectValue objValue)
  92. {
  93. IsCollider = false;
  94. m_MatValue = value;
  95. m_ObjectValue = objValue;
  96. MaterialObj = objValue.Object;
  97. if(m_Text)
  98. m_Text.text = string.IsNullOrWhiteSpace(value.name) ? MaterialObj.name : value.name;
  99. //MaterialObj.transform.localPosition = m_ObjectValue.nowPos;
  100. //MaterialObj.transform.localEulerAngles = m_ObjectValue.nowRot;
  101. //MaterialObj.transform.localScale = m_ObjectValue.nowScale;
  102. //m_Image.sprite =
  103. }
  104. public void SetData(ObjectValue objValue)
  105. {
  106. IsCollider = true;
  107. m_ObjectValue = objValue;
  108. MaterialObj = objValue.Object;
  109. if (m_Text)
  110. m_Text.text = "BoxCollider";
  111. MaterialObj.transform.localPosition = m_ObjectValue.nowPos;
  112. MaterialObj.transform.localEulerAngles = m_ObjectValue.nowRot;
  113. MaterialObj.transform.localScale = m_ObjectValue.nowScale;
  114. }
  115. public void InitMatObj()
  116. {
  117. if (m_ObjectValue != null)
  118. {
  119. m_ObjectValue.InitObjTransform();
  120. }
  121. if (IsCollider)
  122. {
  123. GameManager.Instance.SaveCurrentColliderValue(m_ObjectValue);
  124. }
  125. }
  126. public void SaveMatObj()
  127. {
  128. if (m_ObjectValue != null)
  129. {
  130. m_ObjectValue.SaveObjTransfoem();
  131. }
  132. if (IsCollider)
  133. {
  134. GameManager.Instance.SaveCurrentColliderValue(m_ObjectValue);
  135. }
  136. }
  137. public void ClearData()
  138. {
  139. if(m_Text)
  140. m_Text.text = null;
  141. if (m_Image)
  142. m_Image.sprite = null;
  143. MaterialObj = null;
  144. m_ObjectValue = null;
  145. IsCollider = false;
  146. IsSelect = false;
  147. }
  148. /// <summary>
  149. /// 选中物体状态改变
  150. /// </summary>
  151. /// <param name="isShow"></param>
  152. public void SelectObj(bool isShow)
  153. {
  154. Debug.Log(MaterialObj);
  155. if (MaterialObj != null)
  156. {
  157. Debug.Log(MaterialObj.GetComponent<EditorEventHandler>());
  158. MaterialObj.GetComponent<EditorEventHandler>().ChangeShow(isShow);
  159. }
  160. }
  161. private void OnDestroy()
  162. {
  163. //EventManager.Instance.Remove(this);
  164. }
  165. }