123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.Serialization;
- using Fort23.Core;
- using Fort23.Mono;
- using Fort23.UTool;
- using TMPro;
- 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,
- TextValue = 256,
- Icon = 512,
- OutLineColor = 1024,
- Sizedata = 2048,
- CustomState = 4096,
- TmpColor = 8192
- }
- [Serializable]
- public class UIStateData
- {
- public List<StateInfo> StateInfos;
- public UIStateData()
- {
- StateInfos = new List<StateInfo>();
- }
- }
- [Serializable]
- public class StateInfo
- {
- [SerializeField] public GameObject Target;
- //v4专用数据源
- [SerializeField] public List<UIStateType> UIStateV4Types;
- [SerializeField] public List<Vector4> UIStateV4Datas;
- //Str 专用数据源
- [SerializeField] public List<UIStateType> UIStateStrTypes;
- [SerializeField] public List<string> UIStateStrDatas;
- }
- [SerializeField] public List<UIStateData> data = new List<UIStateData>();
- public List<Object> AllComs = new List<Object>();
- [SerializeField] public List<UIStateType> AllComsStateTypes = new List<UIStateType>();
- 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)
- {
- LogTool.Error("保存状态数据的目标为空");
- continue;
- }
- GameObject go = AllComs[i] as GameObject;
- if (go == null)
- {
- LogTool.Error("保存状态数据的目标为空");
- continue;
- }
- StateInfo stateInfo = new StateInfo();
- stateInfo.UIStateV4Datas = new List<Vector4>();
- stateInfo.UIStateV4Types = new List<UIStateType>();
- 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<RectTransform>().anchoredPosition;
- stateInfo.UIStateV4Types.Add(UIStateType.Position);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.Scale))
- {
- Vector4 vector4 = go.GetComponent<RectTransform>().localScale;
- stateInfo.UIStateV4Types.Add(UIStateType.Scale);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.Rotation))
- {
- Vector4 vector4 = go.GetComponent<RectTransform>().localRotation.eulerAngles;
- stateInfo.UIStateV4Types.Add(UIStateType.Rotation);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.Sizedata))
- {
- Vector4 vector4 = go.GetComponent<RectTransform>().sizeDelta;
- stateInfo.UIStateV4Types.Add(UIStateType.Sizedata);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.RBSize))
- {
- Vector4 vector4 = go.GetComponent<RectTransform>().sizeDelta;
- stateInfo.UIStateV4Types.Add(UIStateType.RBSize);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.ImageColor))
- {
- Vector4 vector4 = go.GetComponent<Image>().color;
- stateInfo.UIStateV4Types.Add(UIStateType.ImageColor);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.TextColor))
- {
- Vector4 vector4 = go.GetComponent<Text>().color;
- stateInfo.UIStateV4Types.Add(UIStateType.TextColor);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
-
- if (AllComsStateTypes[i].HasFlag(UIStateType.TmpColor))
- {
- Vector4 vector4 = go.GetComponent<TextMeshProUGUI>().color;
- stateInfo.UIStateV4Types.Add(UIStateType.TmpColor);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.OutLineColor))
- {
- Vector4 vector4 = go.GetComponent<Outline>().effectColor;
- stateInfo.UIStateV4Types.Add(UIStateType.OutLineColor);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.TextValue))
- {
- if (stateInfo.UIStateStrDatas == null)
- {
- stateInfo.UIStateStrDatas = new List<string>();
- stateInfo.UIStateStrTypes = new List<UIStateType>();
- }
- string str = String.Empty;
- if (go.GetComponent<TextLanguageMono>().id != 0)
- {
- str = go.GetComponent<TextLanguageMono>().id.ToString();
- }
- else
- {
- str = go.GetComponent<Text>().text;
- }
- stateInfo.UIStateStrTypes.Add(UIStateType.TextValue);
- stateInfo.UIStateStrDatas.Add(str);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.Icon))
- {
- if (stateInfo.UIStateStrDatas == null)
- {
- stateInfo.UIStateStrDatas = new List<string>();
- stateInfo.UIStateStrTypes = new List<UIStateType>();
- }
- string str = go.GetComponent<MyImage>().icon_name.ToString();
- stateInfo.UIStateStrTypes.Add(UIStateType.Icon);
- stateInfo.UIStateStrDatas.Add(str);
- }
- if (AllComsStateTypes[i].HasFlag(UIStateType.CustomState))
- {
- Vector4 vector4 = new Vector4();
- vector4.x = go.GetComponent<CustomStateController>().CurrIndex;
- stateInfo.UIStateV4Types.Add(UIStateType.CustomState);
- stateInfo.UIStateV4Datas.Add(vector4);
- }
- 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)
- {
- LogTool.Error("保存状态数据的目标不存在");
- 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<RectTransform>().anchoredPosition = pos;
- break;
- case UIStateType.Scale:
- Vector3 scale = keyValuePair.UIStateV4Datas[i];
- go.GetComponent<RectTransform>().localScale = scale;
- break;
- case UIStateType.Rotation:
- Quaternion rotation = Quaternion.Euler(keyValuePair.UIStateV4Datas[i]);
- go.GetComponent<RectTransform>().localRotation = rotation;
- break;
- case UIStateType.Sizedata:
- Vector3 sizedata = keyValuePair.UIStateV4Datas[i];
- go.GetComponent<RectTransform>().sizeDelta = sizedata;
- break;
- case UIStateType.RBSize:
- Vector2 sizeDelta = keyValuePair.UIStateV4Datas[i];
- go.GetComponent<RectTransform>().sizeDelta = sizeDelta;
- break;
- case UIStateType.ImageColor:
- Color imageColorData = keyValuePair.UIStateV4Datas[i];
- go.GetComponent<Image>().color = imageColorData;
- break;
- case UIStateType.TextColor:
- Color textColorData = keyValuePair.UIStateV4Datas[i];
- go.GetComponent<Text>().color = textColorData;
- break;
- case UIStateType.TmpColor:
- Color textColorData1 = keyValuePair.UIStateV4Datas[i];
- go.GetComponent<TextMeshProUGUI>().color = textColorData1;
- break;
- case UIStateType.OutLineColor:
- Color outLineColorData = keyValuePair.UIStateV4Datas[i];
- go.GetComponent<Outline>().effectColor = outLineColorData;
- break;
- case UIStateType.CustomState:
- int index = (int)keyValuePair.UIStateV4Datas[i].x;
- go.GetComponent<CustomStateController>().ChangeState(index);
- break;
- }
- }
- }
- if (keyValuePair.UIStateStrTypes != null)
- {
- for (var i = 0; i < keyValuePair.UIStateStrTypes.Count; i++)
- {
- switch (keyValuePair.UIStateStrTypes[i])
- {
- case UIStateType.TextValue:
- string str = keyValuePair.UIStateStrDatas[i];
- int id = 0;
- if (int.TryParse(str, out id))
- {
- // go.GetComponent<TextLanguageMono>().SetLanguage(str.ToInt());
- }
- else
- {
- go.GetComponent<Text>().text = str;
- }
- break;
- case UIStateType.Icon:
- string iconName = keyValuePair.UIStateStrDatas[i];
- go.GetComponent<MyImage>().icon_name = iconName;
- break;
- }
- }
- }
- }
- }
- public void OnBeforeSerialize()
- {
- }
- public void OnAfterDeserialize()
- {
- }
- }
|