2024-08-23 15:49:34 +08:00

512 lines
19 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace EditorTool
{
class PrefabBindPointSpriteInfo
{
public List<RawImage> rawImageList = new List<RawImage>();
public GameObject originObj;
public GameObject newObj;
public PrefabBindPointSpriteInfo(GameObject _old, GameObject _new)
{
originObj = _old;
newObj = _new;
}
public void AddBindPointRawImage(RawImage image)
{
rawImageList.Add(image);
}
}
public class UGUIFixSpriteSize
{
[MenuItem("ProTool/UI/获取选中目标路径")]
public static void GetSelectedObjPath()
{
var obj = Selection.activeObject;
var path = AssetDatabase.GetAssetPath(obj).Replace('/', '\\');
Debug.LogError(path.Replace("\\", "\\\\"));
}
[MenuItem("ProTool/UI/获取添加Animator的UI")]
public static void FixSpriteSize()
{
string filePath = "C:/Users/Administrator/Desktop/animatorPath.txt";
List<PrefabBindPointSpriteInfo> infoList = new List<PrefabBindPointSpriteInfo>();
var prefabPathList = EditorCommonUtility.GetPathsFromSelectByExtension(".prefab");
Dictionary<string, List<string>> nameDic = new Dictionary<string, List<string>>();
for (int index = 0; index < prefabPathList.Count; index++)
{
var prefabOrigin = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPathList[index]);
var components = prefabOrigin.GetComponentsInChildren<Transform>();
for(int comIndex = 0; comIndex < components.Length; comIndex++)
{
var layerObj = components[comIndex].gameObject;
if(layerObj.GetComponent<Animator>())
{
if(nameDic.ContainsKey(prefabOrigin.gameObject.name))
{
nameDic[prefabOrigin.gameObject.name].Add(layerObj.name);
}
else
{
List<string> nameList = new List<string>();
nameList.Add(layerObj.name);
nameDic.Add(prefabOrigin.gameObject.name, nameList);
}
}
}
}
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
fs.Close();
if (File.Exists(filePath))
{
using (StreamWriter sw = new StreamWriter(filePath))
{
foreach (var info in nameDic)
{
sw.Write(info.Key + "\t");
for (int index = 0; index < info.Value.Count; index++)
{
sw.Write(info.Value[index] + "\t");
sw.Flush();
}
sw.WriteLine();
}
sw.Dispose();
sw.Close();
}
}
Debug.LogError("Write finish");
}
}
}
public class UITool : ScriptableObject
{
private static Vector3 position;
private static Quaternion rotation;
//private static Vector3 scale;
[MenuItem("ProTool/UI/ChangeAllPrefabTextToBaseFont")]
public static void ChangeFontToBaseFont()
{
var prefabPathList = EditorCommonUtility.GetPathsFromSelectByExtension(".prefab");
var fontAsset = AssetDatabase.LoadAssetAtPath<Font>("Assets\\Res_newMS\\UI\\第六版\\Z-字体\\BaseFont.TTF");
if(fontAsset == null)
{
Debug.LogWarning("fontAsset is null");
return;
}
for (int index = 0; index < prefabPathList.Count; index++)
{
var _hasChange = false;
var prefabObjOringin = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPathList[index]);
var prefabObj = GameObject.Instantiate(prefabObjOringin);
var textComponentList = prefabObj.GetComponentsInChildren<Text>(true);
for (int objIndex = 0; objIndex < textComponentList.Length; objIndex++)
{
if (textComponentList[objIndex].font && !textComponentList[objIndex].font.name.Equals("BaseFont"))
{
if (!_hasChange)
_hasChange = true;
Debug.LogError("ObjName : " + prefabObj.name + ", layerName : " + textComponentList[objIndex].name);
textComponentList[objIndex].font = fontAsset;
}
}
if (_hasChange)
{
Debug.LogError("Replace : " + prefabObj.name);
PrefabUtility.ReplacePrefab(prefabObj, prefabObjOringin, ReplacePrefabOptions.ReplaceNameBased);
}
GameObject.DestroyImmediate(prefabObj);
}
Debug.LogError("Finish");
}
[MenuItem("ProTool/UI/ChangeTextFont")]
public static void ChangeErrorShader()
{
var textObjs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
Debug.Log("Filter asset cnt:" + textObjs.Length);
var fontAsset = AssetDatabase.LoadAssetAtPath<Font>("Assets\\Res_newMS\\UI\\第五版\\Font\\BaseFont.OTF");
foreach (var textObj in textObjs)
{
if (!(textObj is GameObject))
continue;
var assetPath = AssetDatabase.GetAssetPath(textObj);
var uiPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
var textGO = GameObject.Instantiate(textObj as GameObject);
var textGroup = (textGO).GetComponentsInChildren<Text>(true);
foreach (var text in textGroup)
{
if (text == null)
continue;
if (text.font.name.Contains("BaseFont"))
{
text.font = fontAsset;
text.lineSpacing = 1f;
}
}
PrefabUtility.ReplacePrefab(textGO, textObj as GameObject, ReplacePrefabOptions.ReplaceNameBased);
GameObject.DestroyImmediate(textGO);
}
}
[MenuItem("ProTool/检测中文")]
public static void PointCha()
{
const string path = @"D:\path.txt";
if(!File.Exists(path))
{
File.Create(path);
}
//FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(path);
var allScripts = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (var script in allScripts)
{
var scriptPath = script.GetAssetPath();
if (scriptPath.Contains(@"SDK") || scriptPath.Contains(@"Shader") || scriptPath.Contains(@"UnityPort") || scriptPath.Contains(@"Plugin")
|| scriptPath.Contains(@"Editor"))
continue;
var fullPath = scriptPath.Split('/');
if (fullPath[fullPath.Length - 1].ToString().StartsWith("Table_")
|| fullPath[fullPath.Length - 1].Equals("各种类型参数说明.txt")
|| fullPath[fullPath.Length - 1].Equals("GMPanel.cs"))
continue;
if (!File.Exists(scriptPath))
{
Debug.LogError("File doesn't exit : " + scriptPath);
continue;
}
StreamReader scriptReader = new StreamReader(script.GetAssetPath());
int curline = 0;
bool isLinePass = false;
while (!scriptReader.EndOfStream)
{
curline++;
var lineScript = scriptReader.ReadLine();
lineScript = lineScript.Trim();
if (lineScript.StartsWith(@"/*") && !lineScript.EndsWith(@"*/"))
{
isLinePass = true;
continue;
}
if(isLinePass && !lineScript.EndsWith(@"*/"))
{
continue;
}
if (isLinePass && lineScript.EndsWith(@"*/"))
{
isLinePass = false;
continue;
}
if (lineScript.StartsWith(@"--") ||
lineScript.StartsWith(@"/*") ||
lineScript.StartsWith(@"/*") ||
lineScript.StartsWith(@"*") ||
lineScript.StartsWith(@"//")||
lineScript.EndsWith(@"*/")||
lineScript.StartsWith(@"#region")||
lineScript.StartsWith(@"Module.Log.")||
lineScript.StartsWith(@"LogModule.")||
lineScript.StartsWith(@"[MenuItem"))
continue;
var allLength = lineScript.Length;
if (lineScript.Contains(@"//"))
allLength = lineScript.IndexOf(@"//") - 1;
else if (lineScript.Contains(@"--"))
allLength = lineScript.IndexOf(@"--") - 1;
for (int index = 0; index < allLength; index++)
{
if (lineScript[index] >= 0x4E00 && lineScript[index] <= 0x9FA5)
{
if(fullPath.Length > 0)
{
sw.WriteLine(fullPath[fullPath.Length - 1] + ", line : " + curline);
Debug.LogError(fullPath[fullPath.Length - 1] + ", line : " + curline);
}
break;
}
}
}
scriptReader.Close();
}
sw.Close();
Debug.LogError("---Detective Fisnish---");
}
[MenuItem("ProTool/UI/ButtonFontColor")]
public static void AutoEmoji()
{
var textObjs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
List<Image> btnImgs = new List<Image>();
foreach (var textObj in textObjs)
{
if (!(textObj is GameObject))
continue;
var assetPath = AssetDatabase.GetAssetPath(textObj);
var uiPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
var textGO = GameObject.Instantiate(textObj as GameObject);
var textGroup = (textGO).GetComponentsInChildren<Image>(true);
foreach (var img in textGroup)
{
if (img == null)
continue;
if (img.sprite == null)
continue;
if (img.sprite.name.Contains("按钮黄") || img.sprite.name.Contains("按钮蓝"))
{
Debug.Log(textObj.name);
}
}
//PrefabUtility.ReplacePrefab(textGO, textObj as GameObject, ReplacePrefabOptions.ReplaceNameBased);
GameObject.DestroyImmediate(textGO);
}
}
[MenuItem("ProTool/UI/ListRawImages")]
public static void ListRawImages()
{
var textObjs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
Debug.Log("Filter asset cnt:" + textObjs.Length);
var UIRoot = GameObject.Find("UI Root/PopUIRoot");
foreach (var textObj in textObjs)
{
if (!(textObj is GameObject))
continue;
var assetPath = AssetDatabase.GetAssetPath(textObj);
var uiPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
var textGO = GameObject.Instantiate(textObj as GameObject);
//textGO.transform.SetParent(UIRoot.transform);
var textGroup = (textGO).GetComponentsInChildren<RawImage>(true);
if (textGroup.Length == 0)
{
GameObject.DestroyImmediate(textGO);
continue;
}
foreach (var text in textGroup)
{
if (text == null)
continue;
if (text.texture != null)
{
var texturepath = AssetDatabase.GetAssetPath(text.texture);
var sprite = AssetDatabase.LoadAssetAtPath<Sprite>(texturepath);
Debug.Log("RawGO:" + textObj.name + ", " + text.name + ", " + text.texture.name);
if (sprite != null && text.GetComponent<ScratchTicket>() == null )
{
var textRawGO = text.gameObject;
GameObject.DestroyImmediate(text);
var textImage = textRawGO.AddComponent<Image>();
textImage.sprite = sprite;
}
else
{
}
}
else
{
Debug.Log("RawGO:" + textObj.name + ", " + text.name);
}
}
PrefabUtility.ReplacePrefab(textGO, textObj as GameObject, ReplacePrefabOptions.ReplaceNameBased);
}
}
#region text
[MenuItem("ProTool/UI/ChangeText/ChangeAllSceneObj")]
public static void ChangeAllSceneObj()
{
var allGos = GameObject.FindObjectsOfType<GameObject>();
foreach (var go in allGos)
{
var componets = go.GetComponentsInChildren<FontChangeText>(true);
foreach (var componet in componets)
{
if (componet != null)
{
string textStr = componet.text;
var font = componet.font;
int fontSize = componet.fontSize;
var fontColor = componet.color;
var textGO = componet.gameObject;
var anchor = componet.alignment;
GameObject.DestroyImmediate(componet, true);
var text = textGO.AddComponent<Text>();
Debug.Log("ChangeText GO:" + textGO.name);
text.text = textStr;
text.font = font;
text.fontSize = fontSize;
text.color = fontColor;
text.alignment = anchor;
}
}
}
}
[MenuItem("ProTool/UI/ChangeText/ChangeSelectedPrefab")]
public static void ChangeSelectedPrefab()
{
var selectUI = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
for (int i = 0; i < selectUI.Length; ++i)
{
if (!(selectUI[i] is GameObject))
continue;
var assetData = selectUI[i] as GameObject;
var componets = assetData.GetComponentsInChildren<Component>(true);
foreach (var componet in componets)
{
if (componet == null)
continue;
string componetTypeName = componet.GetType().ToString();
if (componetTypeName.Contains("UnityEngine") ||
componetTypeName.Contains("UnityEditor"))
continue;
var fields = componet.GetType().GetFields( System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (var field in fields)
{
if (field.FieldType.IsArray || field.FieldType.ToString().Contains("List"))
{
string fieldTypeName = field.FieldType.ToString();
if (fieldTypeName.Contains("Text"))
{
var textList = field.GetValue(componet) as IList;
if (textList != null)
{
for(int j = 0; j< textList.Count; ++j)
{
var textComponet = textList[j] as Component;
if (textComponet != null)
{
var changeText = textComponet.GetComponent<FontChangeText>();
if (changeText != null)
{
var text = ChangeText(changeText);
textList[j] = text;
Debug.LogError("ChangeText textList:" + componet.name + ",fieldName:" + field.Name);
}
}
}
}
}
}
else if (field.FieldType.Name.Contains("Text"))
{
var textFieldVal = field.GetValue(componet);
var textComponet = textFieldVal as Component;
if (textComponet != null)
{
var changeText = textComponet.GetComponent<FontChangeText>();
if (changeText != null)
{
var text = ChangeText(changeText);
field.SetValue(componet, text);
Debug.Log("ChangeText componet:" + componet.name + ",fieldName:" + field.Name);
}
}
}
}
}
var fontTexts = assetData.GetComponentsInChildren<FontChangeText>(true);
foreach (var componet in fontTexts)
{
if (componet != null)
{
ChangeText(componet);
}
}
if (fontTexts != null && fontTexts.Length != 0)
{
Debug.Log("ChangeText prefab:" + assetData.name);
}
}
}
private static Text ChangeText(FontChangeText fontText)
{
string textStr = fontText.text;
var font = fontText.font;
int fontSize = fontText.fontSize;
var fontColor = fontText.color;
var textGO = fontText.gameObject;
var anchor = fontText.alignment;
var rayCast = fontText.raycastTarget;
GameObject.DestroyImmediate(fontText, true);
var text = textGO.AddComponent<Text>();
Debug.Log("ChangeText GO:" + textGO.name);
text.text = textStr;
text.font = font;
text.fontSize = fontSize;
text.color = fontColor;
text.alignment = anchor;
text.raycastTarget = rayCast;
return text;
}
#endregion
}