CustomStateController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 = default;
  155. if (go.GetComponent<Outline>() != null)
  156. {
  157. vector4 = go.GetComponent<Outline>().effectColor;
  158. }
  159. else if (go.GetComponent<NicerOutline>() != null)
  160. {
  161. vector4 = go.GetComponent<NicerOutline>().effectColor;
  162. }
  163. stateInfo.UIStateV4Types.Add(UIStateType.OutLineColor);
  164. stateInfo.UIStateV4Datas.Add(vector4);
  165. }
  166. if (AllComsStateTypes[i].HasFlag(UIStateType.TextValue))
  167. {
  168. if (stateInfo.UIStateStrDatas == null)
  169. {
  170. stateInfo.UIStateStrDatas = new List<string>();
  171. stateInfo.UIStateStrTypes = new List<UIStateType>();
  172. }
  173. string str = String.Empty;
  174. if (go.GetComponent<TextLanguageMono>().id != 0)
  175. {
  176. str = go.GetComponent<TextLanguageMono>().id.ToString();
  177. }
  178. else
  179. {
  180. str = go.GetComponent<Text>().text;
  181. }
  182. stateInfo.UIStateStrTypes.Add(UIStateType.TextValue);
  183. stateInfo.UIStateStrDatas.Add(str);
  184. }
  185. if (AllComsStateTypes[i].HasFlag(UIStateType.Icon))
  186. {
  187. if (stateInfo.UIStateStrDatas == null)
  188. {
  189. stateInfo.UIStateStrDatas = new List<string>();
  190. stateInfo.UIStateStrTypes = new List<UIStateType>();
  191. }
  192. string str = go.GetComponent<MyImage>().icon_name.ToString();
  193. stateInfo.UIStateStrTypes.Add(UIStateType.Icon);
  194. stateInfo.UIStateStrDatas.Add(str);
  195. }
  196. if (AllComsStateTypes[i].HasFlag(UIStateType.CustomState))
  197. {
  198. Vector4 vector4 = new Vector4();
  199. vector4.x = go.GetComponent<CustomStateController>().CurrIndex;
  200. stateInfo.UIStateV4Types.Add(UIStateType.CustomState);
  201. stateInfo.UIStateV4Datas.Add(vector4);
  202. }
  203. uiStateData.StateInfos.Add(stateInfo);
  204. }
  205. return uiStateData;
  206. }
  207. public void ChangeState(int selectIndex)
  208. {
  209. if (selectIndex < 0)
  210. {
  211. return;
  212. }
  213. CurrIndex = selectIndex;
  214. UIStateData uiStateData = data[selectIndex];
  215. foreach (var keyValuePair in uiStateData.StateInfos)
  216. {
  217. GameObject go = keyValuePair.Target;
  218. if (go == null)
  219. {
  220. LogTool.Error("保存状态数据的目标不存在");
  221. continue;
  222. }
  223. if (keyValuePair.UIStateV4Types != null)
  224. {
  225. for (var i = 0; i < keyValuePair.UIStateV4Types.Count; i++)
  226. {
  227. switch (keyValuePair.UIStateV4Types[i])
  228. {
  229. case UIStateType.Active:
  230. bool active;
  231. active = Math.Abs(keyValuePair.UIStateV4Datas[i].x - 1) < 0.00001;
  232. go.gameObject.SetActive(active);
  233. break;
  234. case UIStateType.Position:
  235. Vector2 pos = keyValuePair.UIStateV4Datas[i];
  236. go.GetComponent<RectTransform>().anchoredPosition = pos;
  237. break;
  238. case UIStateType.Scale:
  239. Vector3 scale = keyValuePair.UIStateV4Datas[i];
  240. go.GetComponent<RectTransform>().localScale = scale;
  241. break;
  242. case UIStateType.Rotation:
  243. Quaternion rotation = Quaternion.Euler(keyValuePair.UIStateV4Datas[i]);
  244. go.GetComponent<RectTransform>().localRotation = rotation;
  245. break;
  246. case UIStateType.Sizedata:
  247. Vector3 sizedata = keyValuePair.UIStateV4Datas[i];
  248. go.GetComponent<RectTransform>().sizeDelta = sizedata;
  249. break;
  250. case UIStateType.RBSize:
  251. Vector2 sizeDelta = keyValuePair.UIStateV4Datas[i];
  252. go.GetComponent<RectTransform>().sizeDelta = sizeDelta;
  253. break;
  254. case UIStateType.ImageColor:
  255. Color imageColorData = keyValuePair.UIStateV4Datas[i];
  256. go.GetComponent<Image>().color = imageColorData;
  257. break;
  258. case UIStateType.TextColor:
  259. Color textColorData = keyValuePair.UIStateV4Datas[i];
  260. go.GetComponent<Text>().color = textColorData;
  261. break;
  262. case UIStateType.TmpColor:
  263. Color textColorData1 = keyValuePair.UIStateV4Datas[i];
  264. go.GetComponent<TextMeshProUGUI>().color = textColorData1;
  265. break;
  266. case UIStateType.OutLineColor:
  267. Color outLineColorData = keyValuePair.UIStateV4Datas[i];
  268. if (go.GetComponent<Outline>() != null)
  269. {
  270. go.GetComponent<Outline>().effectColor = outLineColorData;
  271. }
  272. else if (go.GetComponent<NicerOutline>() != null)
  273. {
  274. go.GetComponent<NicerOutline>().effectColor = outLineColorData;
  275. }
  276. break;
  277. case UIStateType.CustomState:
  278. int index = (int)keyValuePair.UIStateV4Datas[i].x;
  279. go.GetComponent<CustomStateController>().ChangeState(index);
  280. break;
  281. }
  282. }
  283. }
  284. if (keyValuePair.UIStateStrTypes != null)
  285. {
  286. for (var i = 0; i < keyValuePair.UIStateStrTypes.Count; i++)
  287. {
  288. switch (keyValuePair.UIStateStrTypes[i])
  289. {
  290. case UIStateType.TextValue:
  291. string str = keyValuePair.UIStateStrDatas[i];
  292. int id = 0;
  293. if (int.TryParse(str, out id))
  294. {
  295. // go.GetComponent<TextLanguageMono>().SetLanguage(str.ToInt());
  296. }
  297. else
  298. {
  299. go.GetComponent<Text>().text = str;
  300. }
  301. break;
  302. case UIStateType.Icon:
  303. string iconName = keyValuePair.UIStateStrDatas[i];
  304. go.GetComponent<MyImage>().icon_name = iconName;
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. public void OnBeforeSerialize()
  312. {
  313. }
  314. public void OnAfterDeserialize()
  315. {
  316. }
  317. }