源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

208 行
7.1 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.IO;
  8. using System.Text.RegularExpressions;
  9. public class AddOutLineTool : EditorWindow
  10. {
  11. private static AddOutLineTool instance;
  12. static int _rate = 100;
  13. static string _targetUid;
  14. static string _pathRoot;
  15. static Color _outLineColor = Color.white;
  16. static Vector2 _outLineDistance = new Vector2(1,1);
  17. static GameObject go;
  18. static Sprite _sourceSprite;
  19. static int _ratePrefab = 10;
  20. static int _successCount = 0;
  21. [MenuItem("Tools/为使用指定纹理的按钮文字添加OutLine")]
  22. static void ShowExcelTools()
  23. {
  24. Init();
  25. instance.Show();
  26. }
  27. void OnGUI()
  28. {
  29. DrawOptions();
  30. }
  31. private void DrawOptions()
  32. {
  33. var cur_guid = EditorGUILayout.TextField("GUID", _targetUid);
  34. var cru_sp = EditorGUILayout.ObjectField("目标纹理", _sourceSprite, typeof(Sprite),false) as Sprite;
  35. var tempSouce = AssetDatabase.LoadAssetAtPath<Sprite>(AssetDatabase.GUIDToAssetPath(cur_guid));
  36. var tempGuid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(cru_sp));
  37. if (tempSouce != null && cur_guid != _targetUid)
  38. {
  39. _sourceSprite = tempSouce;
  40. _targetUid = cur_guid;
  41. }
  42. else if (!string.IsNullOrEmpty(tempGuid))
  43. {
  44. _targetUid = tempGuid;
  45. _sourceSprite = cru_sp;
  46. }
  47. EditorGUILayout.HelpBox("填写 GUID 或者 拖入纹理自动查找GUID 计算以 GUID 为准", MessageType.Info);
  48. EditorGUILayout.Space();
  49. _pathRoot = EditorGUILayout.TextField("查找目录", _pathRoot);
  50. EditorGUILayout.Space();
  51. _outLineColor = EditorGUILayout.ColorField("描边颜色", _outLineColor);
  52. EditorGUILayout.Space();
  53. _outLineDistance = EditorGUILayout.Vector2Field("描边扩展距离", _outLineDistance);
  54. EditorGUILayout.Space();
  55. if (GUILayout.Button("转换"))
  56. {
  57. EditorCoroutineRunner.StartEditorCoroutine(Conver());
  58. }
  59. EditorGUILayout.Space();
  60. EditorGUILayout.Space();
  61. EditorGUILayout.Space();
  62. _rate = EditorGUILayout.IntField("每帧处理 文件 数",_rate);
  63. _ratePrefab = EditorGUILayout.IntField("每帧处理 预设 数", _ratePrefab);
  64. EditorGUILayout.HelpBox("每帧处理文件数,如果处理过程太卡,可以减少,当然也可以增加。\n目的是为了不让进程出现(未响应)",MessageType.Info);
  65. }
  66. IEnumerator Conver()
  67. {
  68. _successCount = 0;
  69. var files = Directory.GetFiles(_pathRoot, "*prefab*", SearchOption.AllDirectories);
  70. List<string> prefabsPath = new List<string>();
  71. var path = AssetDatabase.GUIDToAssetPath(_targetUid);
  72. if(string.IsNullOrEmpty(path))
  73. {
  74. Debug.LogErrorFormat("根据GUID,没有找到对应 地址 {0}",_targetUid);
  75. yield break;
  76. }
  77. _sourceSprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
  78. if(_sourceSprite == null)
  79. {
  80. Debug.LogErrorFormat("根据GUID,没有找到对应 文件 {0}",path);
  81. yield break;
  82. }
  83. //Debug.Log(AssetDatabase.GUIDToAssetPath(_targetUid));
  84. //找到所有预设
  85. float count = 0;
  86. foreach (var file in files)
  87. {
  88. count++;
  89. if (file.EndsWith(".prefab"))
  90. {
  91. var clearPaht = file.Replace(Application.dataPath,"Assets");
  92. prefabsPath.Add(clearPaht);
  93. //Debug.Log(clearPaht);
  94. if (count % _rate == 0)
  95. {
  96. yield return 1;
  97. }
  98. EditorUtility.DisplayProgressBar("查找文件中的预设···", "查找中 " + file, count / files.Length);
  99. }
  100. }
  101. count = 0;
  102. foreach (var singlePaht in prefabsPath)
  103. {
  104. count++;
  105. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(singlePaht);
  106. if (prefab)
  107. {
  108. CheckSingPrefab(prefab);
  109. }
  110. if (count % _ratePrefab == 0)
  111. {
  112. yield return 1;
  113. }
  114. EditorUtility.DisplayProgressBar("添加或者修改Outline···", "当前 " + singlePaht, count / prefabsPath.Count);
  115. }
  116. yield return 1;
  117. AssetDatabase.SaveAssets();
  118. AssetDatabase.Refresh();
  119. EditorUtility.ClearProgressBar();
  120. EditorUtility.DisplayDialog("提示", "查找完成,一共替换 "+ _successCount + " 处", "好的呢", "");
  121. }
  122. /// <summary>
  123. /// 循环遍历对象的每一个子对象,执行检查操作
  124. /// </summary>
  125. /// <param name="go"></param>
  126. void CheckSingPrefab(GameObject go)
  127. {
  128. CheckSingGameObject(go);
  129. foreach (Transform child in go.transform)
  130. {
  131. if(child.childCount > 0)
  132. {
  133. CheckSingPrefab(child.gameObject);
  134. }
  135. CheckSingGameObject(child.gameObject);
  136. }
  137. }
  138. /// <summary>
  139. /// 检查当前对象上是否有Image组件,如果有检查是否和指定的纹理相同,如果相同检查他的子对象的Text组件,尝试添加OutLine
  140. /// </summary>
  141. /// <param name="go"></param>
  142. void CheckSingGameObject(GameObject go)
  143. {
  144. var img = go.GetComponent<Image>();
  145. if(img)
  146. {
  147. if(img.sprite == _sourceSprite)
  148. {
  149. var t = go.GetComponentInChildren<Text>();
  150. if(t)
  151. {
  152. Outline line = t.gameObject.GetComponent<Outline>();
  153. if(line == null)
  154. {
  155. line = t.gameObject.AddComponent<Outline>();
  156. }
  157. line.effectColor = _outLineColor;
  158. line.effectDistance = _outLineDistance;
  159. line.enabled = true;
  160. _successCount++;
  161. }
  162. }
  163. }
  164. }
  165. void OnSelectionChange()
  166. {
  167. //object[] selection = (object[])Selection.objects;
  168. //Debug.Log(selection .Length);
  169. ////判断是否有对象被选中
  170. //if (selection.Length != 1)
  171. // return;
  172. ////遍历每一个对象判断不是文件夹
  173. //string objPath = AssetDatabase.GetAssetPath((Object)selection[0]);
  174. //Debug.Log(objPath);
  175. //if( Directory.Exists(objPath) )
  176. //{
  177. // _pathRoot = objPath;
  178. // Repaint();
  179. //}
  180. //else
  181. //{
  182. //}
  183. }
  184. private static void Init()
  185. {
  186. instance=EditorWindow.GetWindow<AddOutLineTool>("添加描边");
  187. _targetUid = "";
  188. _pathRoot = Application.dataPath + "/LuaFramework/AssetBundleRes/ui";
  189. instance.position = new Rect(new Vector2(604, 290), new Vector2(650, 400));
  190. _sourceSprite = null;
  191. }
  192. }