CustomStateController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.Serialization;
  5. using Fort23.Core;
  6. using Fort23.Mono;
  7. using Fort23.UTool;
  8. using TMPro;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using Utility;
  12. using Object = UnityEngine.Object;
  13. public class CustomStateController : MonoBehaviour, ISerializationCallbackReceiver
  14. {
  15. [Flags]
  16. [Serializable]
  17. public enum UIStateType
  18. {
  19. Null = 0,
  20. Scale = 2,
  21. Active = 4,
  22. Position = 8,
  23. Rotation = 16,
  24. RBSize = 32,
  25. ImageColor = 64,
  26. TextColor = 128,
  27. TextValue = 256,
  28. Icon = 512,
  29. OutLineColor = 1024,
  30. Sizedata = 2048,
  31. CustomState = 4096,
  32. TmpColor = 8192
  33. }
  34. [Serializable]
  35. public class UIStateData
  36. {
  37. public List<StateInfo> StateInfos;
  38. public UIStateData()
  39. {
  40. StateInfos = new List<StateInfo>();
  41. }
  42. }
  43. [Serializable]
  44. public class StateInfo
  45. {
  46. [SerializeField] public GameObject Target;
  47. //v4专用数据源
  48. [SerializeField] public List<UIStateType> UIStateV4Types;
  49. [SerializeField] public List<Vector4> UIStateV4Datas;
  50. //Str 专用数据源
  51. [SerializeField] public List<UIStateType> UIStateStrTypes;
  52. [SerializeField] public List<string> UIStateStrDatas;
  53. }
  54. [SerializeField] public List<UIStateData> data = new List<UIStateData>();
  55. public List<Object> AllComs = new List<Object>();
  56. [SerializeField] public List<UIStateType> AllComsStateTypes = new List<UIStateType>();
  57. public int CurrIndex;
  58. public void AddData()
  59. {
  60. data.Add(CreatData());
  61. CurrIndex = data.Count - 1;
  62. }
  63. public void RemoveData(int index)
  64. {
  65. data.RemoveAt(index);
  66. CurrIndex = data.Count - 1;
  67. if (data.Count == 0)
  68. {
  69. ChangeState(-1);
  70. }
  71. }
  72. public void SaveData()
  73. {
  74. data[CurrIndex] = CreatData();
  75. }
  76. public UIStateData CreatData()
  77. {
  78. UIStateData uiStateData = new UIStateData();
  79. for (var i = 0; i < AllComs.Count; i++)
  80. {
  81. Object _object = AllComs[i];
  82. if (_object == null)
  83. {
  84. LogTool.Error("保存状态数据的目标为空");
  85. continue;
  86. }
  87. GameObject go = AllComs[i] as GameObject;
  88. if (go == null)
  89. {
  90. LogTool.Error("保存状态数据的目标为空");
  91. continue;
  92. }
  93. StateInfo stateInfo = new StateInfo();
  94. stateInfo.UIStateV4Datas = new List<Vector4>();
  95. stateInfo.UIStateV4Types = new List<UIStateType>();
  96. stateInfo.Target = AllComs[i] as GameObject;
  97. if (AllComsStateTypes[i].HasFlag(UIStateType.Active))
  98. {
  99. Vector4 vector4 = new Vector4();
  100. vector4.x = go.gameObject.activeSelf ? 1 : 0;
  101. stateInfo.UIStateV4Types.Add(UIStateType.Active);
  102. stateInfo.UIStateV4Datas.Add(vector4);
  103. }
  104. if (AllComsStateTypes[i].HasFlag(UIStateType.Position))
  105. {
  106. Vector4 vector4 = go.GetComponent<RectTransform>().anchoredPosition;
  107. stateInfo.UIStateV4Types.Add(UIStateType.Position);
  108. stateInfo.UIStateV4Datas.Add(vector4);
  109. }
  110. if (AllComsStateTypes[i].HasFlag(UIStateType.Scale))
  111. {
  112. Vector4 vector4 = go.GetComponent<RectTransform>().localScale;
  113. stateInfo.UIStateV4Types.Add(UIStateType.Scale);
  114. stateInfo.UIStateV4Datas.Add(vector4);
  115. }
  116. if (AllComsStateTypes[i].HasFlag(UIStateType.Rotation))
  117. {
  118. Vector4 vector4 = go.GetComponent<RectTransform>().localRotation.eulerAngles;
  119. stateInfo.UIStateV4Types.Add(UIStateType.Rotation);
  120. stateInfo.UIStateV4Datas.Add(vector4);
  121. }
  122. if (AllComsStateTypes[i].HasFlag(UIStateType.Sizedata))
  123. {
  124. Vector4 vector4 = go.GetComponent<RectTransform>().sizeDelta;
  125. stateInfo.UIStateV4Types.Add(UIStateType.Sizedata);
  126. stateInfo.UIStateV4Datas.Add(vector4);
  127. }
  128. if (AllComsStateTypes[i].HasFlag(UIStateType.RBSize))
  129. {
  130. Vector4 vector4 = go.GetComponent<RectTransform>().sizeDelta;
  131. stateInfo.UIStateV4Types.Add(UIStateType.RBSize);
  132. stateInfo.UIStateV4Datas.Add(vector4);
  133. }
  134. if (AllComsStateTypes[i].HasFlag(UIStateType.ImageColor))
  135. {
  136. Vector4 vector4 = go.GetComponent<Image>().color;
  137. stateInfo.UIStateV4Types.Add(UIStateType.ImageColor);
  138. stateInfo.UIStateV4Datas.Add(vector4);
  139. }
  140. if (AllComsStateTypes[i].HasFlag(UIStateType.TextColor))
  141. {
  142. Vector4 vector4 = go.GetComponent<Text>().color;
  143. stateInfo.UIStateV4Types.Add(UIStateType.TextColor);
  144. stateInfo.UIStateV4Datas.Add(vector4);
  145. }
  146. if (AllComsStateTypes[i].HasFlag(UIStateType.TmpColor))
  147. {
  148. Vector4 vector4 = go.GetComponent<TextMeshProUGUI>().color;
  149. stateInfo.UIStateV4Types.Add(UIStateType.TmpColor);
  150. stateInfo.UIStateV4Datas.Add(vector4);
  151. }
  152. if (AllComsStateTypes[i].HasFlag(UIStateType.OutLineColor))
  153. {
  154. Vector4 vector4 = go.GetComponent<Outline>().effectColor;
  155. stateInfo.UIStateV4Types.Add(UIStateType.OutLineColor);
  156. stateInfo.UIStateV4Datas.Add(vector4);
  157. }
  158. if (AllComsStateTypes[i].HasFlag(UIStateType.TextValue))
  159. {
  160. if (stateInfo.UIStateStrDatas == null)
  161. {
  162. stateInfo.UIStateStrDatas = new List<string>();
  163. stateInfo.UIStateStrTypes = new List<UIStateType>();
  164. }
  165. string str = String.Empty;
  166. if (go.GetComponent<TextLanguageMono>().id != 0)
  167. {
  168. str = go.GetComponent<TextLanguageMono>().id.ToString();
  169. }
  170. else
  171. {
  172. str = go.GetComponent<Text>().text;
  173. }
  174. stateInfo.UIStateStrTypes.Add(UIStateType.TextValue);
  175. stateInfo.UIStateStrDatas.Add(str);
  176. }
  177. if (AllComsStateTypes[i].HasFlag(UIStateType.Icon))
  178. {
  179. if (stateInfo.UIStateStrDatas == null)
  180. {
  181. stateInfo.UIStateStrDatas = new List<string>();
  182. stateInfo.UIStateStrTypes = new List<UIStateType>();
  183. }
  184. string str = go.GetComponent<MyImage>().icon_name.ToString();
  185. stateInfo.UIStateStrTypes.Add(UIStateType.Icon);
  186. stateInfo.UIStateStrDatas.Add(str);
  187. }
  188. if (AllComsStateTypes[i].HasFlag(UIStateType.CustomState))
  189. {
  190. Vector4 vector4 = new Vector4();
  191. vector4.x = go.GetComponent<CustomStateController>().CurrIndex;
  192. stateInfo.UIStateV4Types.Add(UIStateType.CustomState);
  193. stateInfo.UIStateV4Datas.Add(vector4);
  194. }
  195. uiStateData.StateInfos.Add(stateInfo);
  196. }
  197. return uiStateData;
  198. }
  199. public void ChangeState(int selectIndex)
  200. {
  201. if (selectIndex < 0)
  202. {
  203. return;
  204. }
  205. CurrIndex = selectIndex;
  206. UIStateData uiStateData = data[selectIndex];
  207. foreach (var keyValuePair in uiStateData.StateInfos)
  208. {
  209. GameObject go = keyValuePair.Target;
  210. if (go == null)
  211. {
  212. LogTool.Error("保存状态数据的目标不存在");
  213. continue;
  214. }
  215. if (keyValuePair.UIStateV4Types != null)
  216. {
  217. for (var i = 0; i < keyValuePair.UIStateV4Types.Count; i++)
  218. {
  219. switch (keyValuePair.UIStateV4Types[i])
  220. {
  221. case UIStateType.Active:
  222. bool active;
  223. active = Math.Abs(keyValuePair.UIStateV4Datas[i].x - 1) < 0.00001;
  224. go.gameObject.SetActive(active);
  225. break;
  226. case UIStateType.Position:
  227. Vector2 pos = keyValuePair.UIStateV4Datas[i];
  228. go.GetComponent<RectTransform>().anchoredPosition = pos;
  229. break;
  230. case UIStateType.Scale:
  231. Vector3 scale = keyValuePair.UIStateV4Datas[i];
  232. go.GetComponent<RectTransform>().localScale = scale;
  233. break;
  234. case UIStateType.Rotation:
  235. Quaternion rotation = Quaternion.Euler(keyValuePair.UIStateV4Datas[i]);
  236. go.GetComponent<RectTransform>().localRotation = rotation;
  237. break;
  238. case UIStateType.Sizedata:
  239. Vector3 sizedata = keyValuePair.UIStateV4Datas[i];
  240. go.GetComponent<RectTransform>().sizeDelta = sizedata;
  241. break;
  242. case UIStateType.RBSize:
  243. Vector2 sizeDelta = keyValuePair.UIStateV4Datas[i];
  244. go.GetComponent<RectTransform>().sizeDelta = sizeDelta;
  245. break;
  246. case UIStateType.ImageColor:
  247. Color imageColorData = keyValuePair.UIStateV4Datas[i];
  248. go.GetComponent<Image>().color = imageColorData;
  249. break;
  250. case UIStateType.TextColor:
  251. Color textColorData = keyValuePair.UIStateV4Datas[i];
  252. go.GetComponent<Text>().color = textColorData;
  253. break;
  254. case UIStateType.TmpColor:
  255. Color textColorData1 = keyValuePair.UIStateV4Datas[i];
  256. go.GetComponent<TextMeshProUGUI>().color = textColorData1;
  257. break;
  258. case UIStateType.OutLineColor:
  259. Color outLineColorData = keyValuePair.UIStateV4Datas[i];
  260. go.GetComponent<Outline>().effectColor = outLineColorData;
  261. break;
  262. case UIStateType.CustomState:
  263. int index = (int)keyValuePair.UIStateV4Datas[i].x;
  264. go.GetComponent<CustomStateController>().ChangeState(index);
  265. break;
  266. }
  267. }
  268. }
  269. if (keyValuePair.UIStateStrTypes != null)
  270. {
  271. for (var i = 0; i < keyValuePair.UIStateStrTypes.Count; i++)
  272. {
  273. switch (keyValuePair.UIStateStrTypes[i])
  274. {
  275. case UIStateType.TextValue:
  276. string str = keyValuePair.UIStateStrDatas[i];
  277. int id = 0;
  278. if (int.TryParse(str, out id))
  279. {
  280. // go.GetComponent<TextLanguageMono>().SetLanguage(str.ToInt());
  281. }
  282. else
  283. {
  284. go.GetComponent<Text>().text = str;
  285. }
  286. break;
  287. case UIStateType.Icon:
  288. string iconName = keyValuePair.UIStateStrDatas[i];
  289. go.GetComponent<MyImage>().icon_name = iconName;
  290. break;
  291. }
  292. }
  293. }
  294. }
  295. }
  296. public void OnBeforeSerialize()
  297. {
  298. }
  299. public void OnAfterDeserialize()
  300. {
  301. }
  302. }