using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MaterialBtnItem : MonoBehaviour, EventObserver { private Toggle m_Toggle; private Image m_Image; private Text m_Text; private GameObject m_MaterialObj; private ObjectValue m_ObjectValue; private MaterialObjValue m_MatValue; private bool m_IsSelect; private bool m_IsCollider; public GameObject MaterialObj { get { return m_MaterialObj; } set { m_MaterialObj = value; } } public ObjectValue ObjValue { get { return m_ObjectValue; } set { m_ObjectValue = value; } } public MaterialObjValue MatValue { get { return m_MatValue; } set { m_MatValue = value; } } public bool IsCollider { get { return m_IsCollider; } set { m_IsCollider = value; } } public bool IsSelect { get { return m_IsSelect; } set { m_IsSelect = value; } } private void Awake() { IsSelect = false; EventManager.Instance.Register(this, EventID.EVENT_1); m_Toggle = GetComponent(); m_Image = transform.Find("Image").GetComponent(); m_Text = transform.Find("Text").GetComponent(); ; m_Toggle.onValueChanged.AddListener(OnValueChanged); } private void LateUpdate() { if (IsCollider && MaterialObj != null) { MaterialObj.transform.localScale = new Vector3(MaterialObj.transform.localScale.x, 1, MaterialObj.transform.localScale.z); } } private void OnValueChanged(bool b) { if (b) { EventData1 data1 = new EventData1(EventID.EVENT_1, MaterialObj); if (!IsSelect) { data1.Send(); } SelectObj(true); } } public void HandleEvent(EventData data) { switch (data.ID) { case EventID.EVENT_1: EventData1 data1 = (EventData1)data; if (!m_Toggle.isOn && data1.SelectObj == MaterialObj) { m_Toggle.isOn = true; IsSelect = true; } else { IsSelect = false; } break; case EventID.EVENT_2: break; default: break; } } public void SetData(MaterialObjValue value, ObjectValue objValue) { IsCollider = false; m_MatValue = value; m_ObjectValue = objValue; MaterialObj = objValue.Object; if(m_Text) m_Text.text = string.IsNullOrWhiteSpace(value.name) ? MaterialObj.name : value.name; //MaterialObj.transform.localPosition = m_ObjectValue.nowPos; //MaterialObj.transform.localEulerAngles = m_ObjectValue.nowRot; //MaterialObj.transform.localScale = m_ObjectValue.nowScale; //m_Image.sprite = } public void SetData(ObjectValue objValue) { IsCollider = true; m_ObjectValue = objValue; MaterialObj = objValue.Object; if (m_Text) m_Text.text = "BoxCollider"; MaterialObj.transform.localPosition = m_ObjectValue.nowPos; MaterialObj.transform.localEulerAngles = m_ObjectValue.nowRot; MaterialObj.transform.localScale = m_ObjectValue.nowScale; } public void InitMatObj() { if (m_ObjectValue != null) { m_ObjectValue.InitObjTransform(); } if (IsCollider) { GameManager.Instance.SaveCurrentColliderValue(m_ObjectValue); } } public void SaveMatObj() { if (m_ObjectValue != null) { m_ObjectValue.SaveObjTransfoem(); } if (IsCollider) { GameManager.Instance.SaveCurrentColliderValue(m_ObjectValue); } } public void ClearData() { if(m_Text) m_Text.text = null; if (m_Image) m_Image.sprite = null; MaterialObj = null; m_ObjectValue = null; IsCollider = false; IsSelect = false; } /// /// 选中物体状态改变 /// /// public void SelectObj(bool isShow) { Debug.Log(MaterialObj); if (MaterialObj != null) { Debug.Log(MaterialObj.GetComponent()); MaterialObj.GetComponent().ChangeShow(isShow); } } private void OnDestroy() { //EventManager.Instance.Remove(this); } }