源战役客户端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

234 wiersze
9.4 KiB

1 miesiąc temu
  1. #if UNITY_EDITOR
  2. using System.IO;
  3. using System.Text;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace U3DExtends
  7. {
  8. [RequireComponent(typeof(Canvas))]
  9. [ExecuteInEditMode]
  10. public class LayoutInfo : MonoBehaviour
  11. {
  12. //[HideInInspector]
  13. [SerializeField]
  14. private string _layoutPath = string.Empty;
  15. public static bool IsShowLayoutName = false;
  16. Vector3 _lastRealLayoutPos = new Vector3(-1, -1);
  17. Vector2 _lastRealLayoutSize = Vector2.zero;
  18. UnityEngine.UI.Text _viewNameLabel = null;
  19. const string RealPosStartStr = "RealLayoutPosStart ";
  20. const string RealPosEndStr = " RealLayoutPosEnd\n";
  21. static string configPath = string.Empty;
  22. static string ConfigPath
  23. {
  24. get
  25. {
  26. if (configPath == string.Empty)
  27. configPath = Application.temporaryCachePath + "/Decorates";
  28. return configPath;
  29. }
  30. }
  31. public string LayoutPath
  32. {
  33. get
  34. {
  35. return _layoutPath;
  36. }
  37. set
  38. {
  39. _layoutPath = value;
  40. }
  41. }
  42. public GameObject EditingView
  43. {
  44. get
  45. {
  46. for (int i = 0; i < transform.childCount; i++)
  47. {
  48. Transform child = transform.GetChild(i);
  49. if (child.GetComponent<Decorate>() != null || (_viewNameLabel!=null && _viewNameLabel.transform==child))
  50. continue;
  51. return child.gameObject;
  52. }
  53. return null;
  54. }
  55. }
  56. private void Start() {
  57. Transform name_trans = transform.Find("ViewName");
  58. if (name_trans!=null)
  59. _viewNameLabel = name_trans.GetComponent<UnityEngine.UI.Text>();
  60. }
  61. //������������С����ʱҲ��Ҫ�ٱ���һ��,������Ҫ����pos size�ȱ�����Ҫio����,��̫������ͼʱӰ������,���Ի��ǽ��汣��ʱ��һ�𱣴���
  62. public bool SaveToConfigFile()
  63. {
  64. string select_path = FileUtil.GetProjectRelativePath(LayoutPath);
  65. string layout_path_md5 = UIEditorHelper.GenMD5String(select_path);
  66. RectTransform real_layout = UIEditorHelper.GetRealLayout(gameObject) as RectTransform;//���õ���ʵ�Ľ���prefab
  67. if (select_path == "" || real_layout == null)
  68. {
  69. //���滹δ����,�ȱ���ʱ�ٵ��ñ�����
  70. return false;
  71. }
  72. RectTransform curTrans = transform as RectTransform;
  73. bool hadDecorateTransChanged = false;
  74. bool hadTransChanged = true;
  75. if (real_layout.localPosition == _lastRealLayoutPos && real_layout.sizeDelta == _lastRealLayoutSize)
  76. hadTransChanged = false;
  77. _lastRealLayoutPos = real_layout.localPosition;
  78. _lastRealLayoutSize = real_layout.sizeDelta;
  79. if (!Directory.Exists(ConfigPath))
  80. Directory.CreateDirectory(ConfigPath);
  81. string savePath = ConfigPath + "/" + layout_path_md5 + ".txt";
  82. StringBuilder content = new StringBuilder();
  83. content.Append(RealPosStartStr);
  84. content.Append(real_layout.localPosition.x.ToString());
  85. content.Append(' ');
  86. content.Append(real_layout.localPosition.y.ToString());
  87. content.Append(RealPosEndStr);
  88. Decorate[] decorates = transform.GetComponentsInChildren<Decorate>();
  89. for (int i = 0; i < decorates.Length; i++)
  90. {
  91. RectTransform rectTrans = decorates[i].GetComponent<RectTransform>();
  92. if (rectTrans != null)
  93. {
  94. content.Append(decorates[i].SprPath);
  95. content.Append('#');
  96. content.Append(rectTrans.localPosition.x.ToString());
  97. content.Append(' ');
  98. content.Append(rectTrans.localPosition.y.ToString());
  99. content.Append('#');
  100. content.Append(rectTrans.sizeDelta.x.ToString());
  101. content.Append(' ');
  102. content.Append(rectTrans.sizeDelta.y.ToString());
  103. content.Append('*');//�ָ���ͬ�IJ���ͼ
  104. if (decorates[i].IsChangedTrans())
  105. {
  106. decorates[i].SaveTrans();
  107. hadDecorateTransChanged = true;
  108. }
  109. }
  110. }
  111. if (hadTransChanged || hadDecorateTransChanged)
  112. {
  113. if (content[content.Length - 1] == '*')
  114. content.Remove(content.Length - 1, 1);//ɾ������һ���ָ���
  115. File.WriteAllText(savePath, content.ToString());
  116. return true;
  117. }
  118. //����ʵ�����������Ͳ���ͼ�ı任û���Ļ��Ͳ���Ҫ������
  119. return false;
  120. }
  121. public Decorate GetDecorateChild(string picPath)
  122. {
  123. for (int i = 0; i < transform.childCount; i++)
  124. {
  125. Transform child = transform.GetChild(i);
  126. Decorate decor = child.GetComponent<Decorate>();
  127. if (decor != null && decor.SprPath == picPath)
  128. {
  129. return decor;
  130. }
  131. }
  132. return null;
  133. }
  134. //�򿪽���ʱ,����Ŀ��ʱ�ļ����ҵ���Ӧ�����IJ���ͼ����,Ȼ�����ɲ���ͼ
  135. public void ApplyConfig(string view_path)
  136. {
  137. string layout_path_md5 = UIEditorHelper.GenMD5String(view_path);
  138. string confighFilePath = ConfigPath + "/" + layout_path_md5 + ".txt";
  139. if (!File.Exists(confighFilePath))
  140. return;
  141. string content = File.ReadAllText(confighFilePath);
  142. int pos_end_index = content.IndexOf(RealPosEndStr);
  143. if (pos_end_index == -1)
  144. {
  145. Debug.Log("cannot find real layout pos config on ApplyConfig : " + view_path);
  146. return;
  147. }
  148. string real_layout_pos_str = content.Substring(RealPosStartStr.Length, pos_end_index - RealPosStartStr.Length);
  149. string[] pos_cfg = real_layout_pos_str.Split(' ');
  150. if (pos_cfg.Length == 2)
  151. {
  152. RectTransform real_layout = UIEditorHelper.GetRealLayout(gameObject) as RectTransform;//���õ���ʵ�Ľ���prefab
  153. if (real_layout == null)
  154. {
  155. Debug.Log("cannot find real layout on ApplyConfig : " + view_path);
  156. return;
  157. }
  158. real_layout.localPosition = new Vector3(float.Parse(pos_cfg[0]), float.Parse(pos_cfg[1]), real_layout.localPosition.z);
  159. }
  160. else
  161. {
  162. Debug.Log("cannot find real layout pos xy config on ApplyConfig : " + view_path);
  163. return;
  164. }
  165. content = content.Substring(pos_end_index + RealPosEndStr.Length);
  166. if (content == "")
  167. return;//��Щ����û�ο�ͼҲ��������,ֱ�ӷ���
  168. string[] decorate_cfgs = content.Split('*');
  169. for (int i = 0; i < decorate_cfgs.Length; i++)
  170. {
  171. string[] cfgs = decorate_cfgs[i].Split('#');
  172. if (cfgs.Length == 3)
  173. {
  174. string decorate_img_path = cfgs[0];
  175. if (!File.Exists(decorate_img_path))
  176. {
  177. Debug.Log("LayoutInfo:ApplyConfig() cannot find decorate img file : " + decorate_img_path);
  178. continue;
  179. }
  180. Decorate decor = GetDecorateChild(decorate_img_path);
  181. if (decor == null)
  182. decor = UIEditorHelper.CreateEmptyDecorate(transform);
  183. decor.SprPath = decorate_img_path;
  184. RectTransform rectTrans = decor.GetComponent<RectTransform>();
  185. if (rectTrans != null)
  186. {
  187. //IFormatter formatter = new BinaryFormatter();//ʹ�����л����ߵĻ��Ϳ��Ա���������Ϣ,��ʵ�ָ�����,���ü򵥵İ�
  188. string[] pos = cfgs[1].Split(' ');
  189. if (pos.Length == 2)
  190. rectTrans.localPosition = new Vector2(float.Parse(pos[0]), float.Parse(pos[1]));
  191. string[] size = cfgs[2].Split(' ');
  192. if (size.Length == 2)
  193. rectTrans.sizeDelta = new Vector2(float.Parse(size[0]), float.Parse(size[1]));
  194. }
  195. }
  196. else
  197. {
  198. Debug.Log("warning : detect a wrong decorate config file!");
  199. return;
  200. }
  201. }
  202. }
  203. private void OnDrawGizmos() {
  204. if (_viewNameLabel==null)
  205. return;
  206. // bool is_show_name = Event.current!=null && (Event.current.control) && !Event.current.alt && !Event.current.shift;
  207. if (IsShowLayoutName)
  208. {
  209. string show_name = transform.name.Substring(0, transform.name.Length-("_Canvas").Length);
  210. _viewNameLabel.text = show_name;
  211. _viewNameLabel.transform.SetAsLastSibling();
  212. _viewNameLabel.gameObject.SetActive(true);
  213. }
  214. else
  215. {
  216. _viewNameLabel.gameObject.SetActive(false);
  217. }
  218. }
  219. }
  220. }
  221. #endif