using UnityEngine; using UnityEditor; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Text.RegularExpressions; public class AddOutLineTool : EditorWindow { private static AddOutLineTool instance; static int _rate = 100; static string _targetUid; static string _pathRoot; static Color _outLineColor = Color.white; static Vector2 _outLineDistance = new Vector2(1,1); static GameObject go; static Sprite _sourceSprite; static int _ratePrefab = 10; static int _successCount = 0; [MenuItem("Tools/为使用指定纹理的按钮文字添加OutLine")] static void ShowExcelTools() { Init(); instance.Show(); } void OnGUI() { DrawOptions(); } private void DrawOptions() { var cur_guid = EditorGUILayout.TextField("GUID", _targetUid); var cru_sp = EditorGUILayout.ObjectField("目标纹理", _sourceSprite, typeof(Sprite),false) as Sprite; var tempSouce = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(cur_guid)); var tempGuid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(cru_sp)); if (tempSouce != null && cur_guid != _targetUid) { _sourceSprite = tempSouce; _targetUid = cur_guid; } else if (!string.IsNullOrEmpty(tempGuid)) { _targetUid = tempGuid; _sourceSprite = cru_sp; } EditorGUILayout.HelpBox("填写 GUID 或者 拖入纹理自动查找GUID 计算以 GUID 为准", MessageType.Info); EditorGUILayout.Space(); _pathRoot = EditorGUILayout.TextField("查找目录", _pathRoot); EditorGUILayout.Space(); _outLineColor = EditorGUILayout.ColorField("描边颜色", _outLineColor); EditorGUILayout.Space(); _outLineDistance = EditorGUILayout.Vector2Field("描边扩展距离", _outLineDistance); EditorGUILayout.Space(); if (GUILayout.Button("转换")) { EditorCoroutineRunner.StartEditorCoroutine(Conver()); } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); _rate = EditorGUILayout.IntField("每帧处理 文件 数",_rate); _ratePrefab = EditorGUILayout.IntField("每帧处理 预设 数", _ratePrefab); EditorGUILayout.HelpBox("每帧处理文件数,如果处理过程太卡,可以减少,当然也可以增加。\n目的是为了不让进程出现(未响应)",MessageType.Info); } IEnumerator Conver() { _successCount = 0; var files = Directory.GetFiles(_pathRoot, "*prefab*", SearchOption.AllDirectories); List prefabsPath = new List(); var path = AssetDatabase.GUIDToAssetPath(_targetUid); if(string.IsNullOrEmpty(path)) { Debug.LogErrorFormat("根据GUID,没有找到对应 地址 {0}",_targetUid); yield break; } _sourceSprite = AssetDatabase.LoadAssetAtPath(path); if(_sourceSprite == null) { Debug.LogErrorFormat("根据GUID,没有找到对应 文件 {0}",path); yield break; } //Debug.Log(AssetDatabase.GUIDToAssetPath(_targetUid)); //找到所有预设 float count = 0; foreach (var file in files) { count++; if (file.EndsWith(".prefab")) { var clearPaht = file.Replace(Application.dataPath,"Assets"); prefabsPath.Add(clearPaht); //Debug.Log(clearPaht); if (count % _rate == 0) { yield return 1; } EditorUtility.DisplayProgressBar("查找文件中的预设···", "查找中 " + file, count / files.Length); } } count = 0; foreach (var singlePaht in prefabsPath) { count++; var prefab = AssetDatabase.LoadAssetAtPath(singlePaht); if (prefab) { CheckSingPrefab(prefab); } if (count % _ratePrefab == 0) { yield return 1; } EditorUtility.DisplayProgressBar("添加或者修改Outline···", "当前 " + singlePaht, count / prefabsPath.Count); } yield return 1; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); EditorUtility.DisplayDialog("提示", "查找完成,一共替换 "+ _successCount + " 处", "好的呢", ""); } /// /// 循环遍历对象的每一个子对象,执行检查操作 /// /// void CheckSingPrefab(GameObject go) { CheckSingGameObject(go); foreach (Transform child in go.transform) { if(child.childCount > 0) { CheckSingPrefab(child.gameObject); } CheckSingGameObject(child.gameObject); } } /// /// 检查当前对象上是否有Image组件,如果有检查是否和指定的纹理相同,如果相同检查他的子对象的Text组件,尝试添加OutLine /// /// void CheckSingGameObject(GameObject go) { var img = go.GetComponent(); if(img) { if(img.sprite == _sourceSprite) { var t = go.GetComponentInChildren(); if(t) { Outline line = t.gameObject.GetComponent(); if(line == null) { line = t.gameObject.AddComponent(); } line.effectColor = _outLineColor; line.effectDistance = _outLineDistance; line.enabled = true; _successCount++; } } } } void OnSelectionChange() { //object[] selection = (object[])Selection.objects; //Debug.Log(selection .Length); ////判断是否有对象被选中 //if (selection.Length != 1) // return; ////遍历每一个对象判断不是文件夹 //string objPath = AssetDatabase.GetAssetPath((Object)selection[0]); //Debug.Log(objPath); //if( Directory.Exists(objPath) ) //{ // _pathRoot = objPath; // Repaint(); //} //else //{ //} } private static void Init() { instance=EditorWindow.GetWindow("添加描边"); _targetUid = ""; _pathRoot = Application.dataPath + "/LuaFramework/AssetBundleRes/ui"; instance.position = new Rect(new Vector2(604, 290), new Vector2(650, 400)); _sourceSprite = null; } }