using System; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization; using UnityEngine; using UnityEngine.UI; using Utility; using Object = UnityEngine.Object; public class CustomStateController : MonoBehaviour, ISerializationCallbackReceiver { [Flags] [Serializable] public enum UIStateType { Null = 0, Scale = 2, Active = 4, Position = 8, Rotation = 16, RBSize = 32, ImageColor = 64, TextColor = 128, Icon = 256, } [Serializable] public class UIStateData { public List StateInfos; public UIStateData() { StateInfos = new List(); } } [Serializable] public class StateInfo { [SerializeField] public GameObject Target; //v4专用数据源 [SerializeField] public List UIStateV4Types; [SerializeField] public List UIStateV4Datas; //Str 专用数据源 [SerializeField] public List UIStateStrTypes; [SerializeField] public List UIStateStrDatas; } [SerializeField] public List data = new List(); public List AllComs = new List(); [SerializeField] public List AllComsStateTypes = new List(); public int CurrIndex; public void AddData() { data.Add(CreatData()); CurrIndex = data.Count - 1; } public void RemoveData(int index) { data.RemoveAt(index); CurrIndex = data.Count - 1; if (data.Count == 0) { ChangeState(-1); } } public void SaveData() { data[CurrIndex] = CreatData(); } public UIStateData CreatData() { UIStateData uiStateData = new UIStateData(); for (var i = 0; i < AllComs.Count; i++) { Object _object = AllComs[i]; if (_object == null) { Debug.LogError("保存状态数据的目标为空"); continue; } GameObject go = AllComs[i] as GameObject; if (go == null) { Debug.LogError("保存状态数据的目标为空"); continue; } StateInfo stateInfo = new StateInfo(); stateInfo.UIStateV4Datas = new List(); stateInfo.UIStateV4Types = new List(); stateInfo.Target = AllComs[i] as GameObject; if (AllComsStateTypes[i].HasFlag(UIStateType.Active)) { Vector4 vector4 = new Vector4(); vector4.x = go.gameObject.activeSelf ? 1 : 0; stateInfo.UIStateV4Types.Add(UIStateType.Active); stateInfo.UIStateV4Datas.Add(vector4); } if (AllComsStateTypes[i].HasFlag(UIStateType.Position)) { Vector4 vector4 = go.GetComponent().anchoredPosition; stateInfo.UIStateV4Types.Add(UIStateType.Position); stateInfo.UIStateV4Datas.Add(vector4); } if (AllComsStateTypes[i].HasFlag(UIStateType.Scale)) { Vector4 vector4 = go.GetComponent().localScale; stateInfo.UIStateV4Types.Add(UIStateType.Scale); stateInfo.UIStateV4Datas.Add(vector4); } if (AllComsStateTypes[i].HasFlag(UIStateType.Rotation)) { Vector4 vector4 = go.GetComponent().localRotation.eulerAngles; stateInfo.UIStateV4Types.Add(UIStateType.Rotation); stateInfo.UIStateV4Datas.Add(vector4); } if (AllComsStateTypes[i].HasFlag(UIStateType.RBSize)) { Vector4 vector4 = go.GetComponent().sizeDelta; stateInfo.UIStateV4Types.Add(UIStateType.RBSize); stateInfo.UIStateV4Datas.Add(vector4); } if (AllComsStateTypes[i].HasFlag(UIStateType.ImageColor)) { Vector4 vector4 = go.GetComponent().color; stateInfo.UIStateV4Types.Add(UIStateType.ImageColor); stateInfo.UIStateV4Datas.Add(vector4); } if (AllComsStateTypes[i].HasFlag(UIStateType.TextColor)) { Vector4 vector4 = go.GetComponent().color; stateInfo.UIStateV4Types.Add(UIStateType.TextColor); stateInfo.UIStateV4Datas.Add(vector4); } if (AllComsStateTypes[i].HasFlag(UIStateType.Icon)) { if (stateInfo.UIStateStrDatas == null) { stateInfo.UIStateStrDatas = new List(); stateInfo.UIStateStrTypes = new List(); } string str = go.GetComponent().sprite.ToString(); stateInfo.UIStateStrTypes.Add(UIStateType.Icon); stateInfo.UIStateStrDatas.Add(str); } uiStateData.StateInfos.Add(stateInfo); } return uiStateData; } public void ChangeState(int selectIndex) { if (selectIndex < 0) { return; } CurrIndex = selectIndex; UIStateData uiStateData = data[selectIndex]; foreach (var keyValuePair in uiStateData.StateInfos) { GameObject go = keyValuePair.Target; if (go == null) { Debug.LogError("保存状态数据的目标不存在"); continue; } if (keyValuePair.UIStateV4Types != null) { for (var i = 0; i < keyValuePair.UIStateV4Types.Count; i++) { switch (keyValuePair.UIStateV4Types[i]) { case UIStateType.Active: bool active; active = Math.Abs(keyValuePair.UIStateV4Datas[i].x - 1) < 0.00001; go.gameObject.SetActive(active); break; case UIStateType.Position: Vector2 pos = keyValuePair.UIStateV4Datas[i]; go.GetComponent().anchoredPosition = pos; break; case UIStateType.Scale: Vector3 scale = keyValuePair.UIStateV4Datas[i]; go.GetComponent().localScale = scale; break; case UIStateType.Rotation: Quaternion rotation = Quaternion.Euler(keyValuePair.UIStateV4Datas[i]); go.GetComponent().localRotation = rotation; break; case UIStateType.RBSize: Vector2 sizeDelta = keyValuePair.UIStateV4Datas[i]; go.GetComponent().sizeDelta = sizeDelta; break; case UIStateType.ImageColor: Color imageColorData = keyValuePair.UIStateV4Datas[i]; go.GetComponent().color = imageColorData; break; case UIStateType.TextColor: Color textColorData = keyValuePair.UIStateV4Datas[i]; go.GetComponent().color = textColorData; break; } } } if (keyValuePair.UIStateStrTypes != null) { for (var i = 0; i < keyValuePair.UIStateStrTypes.Count; i++) { switch (keyValuePair.UIStateStrTypes[i]) { case UIStateType.Icon: string iconName = keyValuePair.UIStateStrDatas[i]; go.GetComponent().sprite = Resources.Load("Sprite/"+iconName); break; } } } } } public void OnBeforeSerialize() { } public void OnAfterDeserialize() { } }