CustomStateController.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.Serialization;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using Utility;
  8. using Object = UnityEngine.Object;
  9. public class CustomStateController : MonoBehaviour, ISerializationCallbackReceiver
  10. {
  11. [Flags]
  12. [Serializable]
  13. public enum UIStateType
  14. {
  15. Null = 0,
  16. Scale = 2,
  17. Active = 4,
  18. Position = 8,
  19. Rotation = 16,
  20. RBSize = 32,
  21. ImageColor = 64,
  22. TextColor = 128,
  23. Icon = 256,
  24. }
  25. [Serializable]
  26. public class UIStateData
  27. {
  28. public List<StateInfo> StateInfos;
  29. public UIStateData()
  30. {
  31. StateInfos = new List<StateInfo>();
  32. }
  33. }
  34. [Serializable]
  35. public class StateInfo
  36. {
  37. [SerializeField] public GameObject Target;
  38. //v4专用数据源
  39. [SerializeField] public List<UIStateType> UIStateV4Types;
  40. [SerializeField] public List<Vector4> UIStateV4Datas;
  41. //Str 专用数据源
  42. [SerializeField] public List<UIStateType> UIStateStrTypes;
  43. [SerializeField] public List<string> UIStateStrDatas;
  44. }
  45. [SerializeField] public List<UIStateData> data = new List<UIStateData>();
  46. public List<Object> AllComs = new List<Object>();
  47. [SerializeField]
  48. public List<UIStateType> AllComsStateTypes = new List<UIStateType>();
  49. public int CurrIndex;
  50. public void AddData()
  51. {
  52. data.Add(CreatData());
  53. CurrIndex = data.Count - 1;
  54. }
  55. public void RemoveData(int index)
  56. {
  57. data.RemoveAt(index);
  58. CurrIndex = data.Count - 1;
  59. if (data.Count == 0)
  60. {
  61. ChangeState(-1);
  62. }
  63. }
  64. public void SaveData()
  65. {
  66. data[CurrIndex] = CreatData();
  67. }
  68. public UIStateData CreatData()
  69. {
  70. UIStateData uiStateData = new UIStateData();
  71. for (var i = 0; i < AllComs.Count; i++)
  72. {
  73. Object _object = AllComs[i];
  74. if (_object == null)
  75. {
  76. Debug.LogError("保存状态数据的目标为空");
  77. continue;
  78. }
  79. GameObject go = AllComs[i] as GameObject;
  80. if (go == null)
  81. {
  82. Debug.LogError("保存状态数据的目标为空");
  83. continue;
  84. }
  85. StateInfo stateInfo = new StateInfo();
  86. stateInfo.UIStateV4Datas = new List<Vector4>();
  87. stateInfo.UIStateV4Types = new List<UIStateType>();
  88. stateInfo.Target = AllComs[i] as GameObject;
  89. if (AllComsStateTypes[i].HasFlag(UIStateType.Active))
  90. {
  91. Vector4 vector4 = new Vector4();
  92. vector4.x = go.gameObject.activeSelf ? 1 : 0;
  93. stateInfo.UIStateV4Types.Add(UIStateType.Active);
  94. stateInfo.UIStateV4Datas.Add(vector4);
  95. }
  96. if (AllComsStateTypes[i].HasFlag(UIStateType.Position))
  97. {
  98. Vector4 vector4 = go.GetComponent<RectTransform>().anchoredPosition;
  99. stateInfo.UIStateV4Types.Add(UIStateType.Position);
  100. stateInfo.UIStateV4Datas.Add(vector4);
  101. }
  102. if (AllComsStateTypes[i].HasFlag(UIStateType.Scale))
  103. {
  104. Vector4 vector4 = go.GetComponent<RectTransform>().localScale;
  105. stateInfo.UIStateV4Types.Add(UIStateType.Scale);
  106. stateInfo.UIStateV4Datas.Add(vector4);
  107. }
  108. if (AllComsStateTypes[i].HasFlag(UIStateType.Rotation))
  109. {
  110. Vector4 vector4 = go.GetComponent<RectTransform>().localRotation.eulerAngles;
  111. stateInfo.UIStateV4Types.Add(UIStateType.Rotation);
  112. stateInfo.UIStateV4Datas.Add(vector4);
  113. }
  114. if (AllComsStateTypes[i].HasFlag(UIStateType.RBSize))
  115. {
  116. Vector4 vector4 = go.GetComponent<RectTransform>().sizeDelta;
  117. stateInfo.UIStateV4Types.Add(UIStateType.RBSize);
  118. stateInfo.UIStateV4Datas.Add(vector4);
  119. }
  120. if (AllComsStateTypes[i].HasFlag(UIStateType.ImageColor))
  121. {
  122. Vector4 vector4 = go.GetComponent<Image>().color;
  123. stateInfo.UIStateV4Types.Add(UIStateType.ImageColor);
  124. stateInfo.UIStateV4Datas.Add(vector4);
  125. }
  126. if (AllComsStateTypes[i].HasFlag(UIStateType.TextColor))
  127. {
  128. Vector4 vector4 = go.GetComponent<Text>().color;
  129. stateInfo.UIStateV4Types.Add(UIStateType.TextColor);
  130. stateInfo.UIStateV4Datas.Add(vector4);
  131. }
  132. if (AllComsStateTypes[i].HasFlag(UIStateType.Icon))
  133. {
  134. if (stateInfo.UIStateStrDatas == null)
  135. {
  136. stateInfo.UIStateStrDatas = new List<string>();
  137. stateInfo.UIStateStrTypes = new List<UIStateType>();
  138. }
  139. string str = go.GetComponent<Image>().sprite.ToString();
  140. stateInfo.UIStateStrTypes.Add(UIStateType.Icon);
  141. stateInfo.UIStateStrDatas.Add(str);
  142. }
  143. uiStateData.StateInfos.Add(stateInfo);
  144. }
  145. return uiStateData;
  146. }
  147. public void ChangeState(int selectIndex)
  148. {
  149. if (selectIndex < 0)
  150. {
  151. return;
  152. }
  153. CurrIndex = selectIndex;
  154. UIStateData uiStateData = data[selectIndex];
  155. foreach (var keyValuePair in uiStateData.StateInfos)
  156. {
  157. GameObject go = keyValuePair.Target;
  158. if (go == null)
  159. {
  160. Debug.LogError("保存状态数据的目标不存在");
  161. continue;
  162. }
  163. if (keyValuePair.UIStateV4Types != null)
  164. {
  165. for (var i = 0; i < keyValuePair.UIStateV4Types.Count; i++)
  166. {
  167. switch (keyValuePair.UIStateV4Types[i])
  168. {
  169. case UIStateType.Active:
  170. bool active;
  171. active = Math.Abs(keyValuePair.UIStateV4Datas[i].x - 1) < 0.00001;
  172. go.gameObject.SetActive(active);
  173. break;
  174. case UIStateType.Position:
  175. Vector2 pos = keyValuePair.UIStateV4Datas[i];
  176. go.GetComponent<RectTransform>().anchoredPosition = pos;
  177. break;
  178. case UIStateType.Scale:
  179. Vector3 scale = keyValuePair.UIStateV4Datas[i];
  180. go.GetComponent<RectTransform>().localScale = scale;
  181. break;
  182. case UIStateType.Rotation:
  183. Quaternion rotation = Quaternion.Euler(keyValuePair.UIStateV4Datas[i]);
  184. go.GetComponent<RectTransform>().localRotation = rotation;
  185. break;
  186. case UIStateType.RBSize:
  187. Vector2 sizeDelta = keyValuePair.UIStateV4Datas[i];
  188. go.GetComponent<RectTransform>().sizeDelta = sizeDelta;
  189. break;
  190. case UIStateType.ImageColor:
  191. Color imageColorData = keyValuePair.UIStateV4Datas[i];
  192. go.GetComponent<Image>().color = imageColorData;
  193. break;
  194. case UIStateType.TextColor:
  195. Color textColorData = keyValuePair.UIStateV4Datas[i];
  196. go.GetComponent<Text>().color = textColorData;
  197. break;
  198. }
  199. }
  200. }
  201. if (keyValuePair.UIStateStrTypes != null)
  202. {
  203. for (var i = 0; i < keyValuePair.UIStateStrTypes.Count; i++)
  204. {
  205. switch (keyValuePair.UIStateStrTypes[i])
  206. {
  207. case UIStateType.Icon:
  208. string iconName = keyValuePair.UIStateStrDatas[i];
  209. go.GetComponent<Image>().sprite = Resources.Load<Sprite>("Sprite/"+iconName);
  210. break;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. public void OnBeforeSerialize()
  217. {
  218. }
  219. public void OnAfterDeserialize()
  220. {
  221. }
  222. }