From d43529e2d01bc7b153501ff5c15e00629a5dfb63 Mon Sep 17 00:00:00 2001 From: hch <305670599@qq.com> Date: 星期四, 19 六月 2025 22:40:15 +0800 Subject: [PATCH] 0312 图集 --- Assets/Editor/Tool/ChangeTextAndImage.cs | 136 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 136 insertions(+), 0 deletions(-) diff --git a/Assets/Editor/Tool/ChangeTextAndImage.cs b/Assets/Editor/Tool/ChangeTextAndImage.cs new file mode 100644 index 0000000..37e0d6b --- /dev/null +++ b/Assets/Editor/Tool/ChangeTextAndImage.cs @@ -0,0 +1,136 @@ +锘縰sing System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; +using UnityEngine.UI; + + +public class ChangeTextAndImage : EditorWindow { + + + [MenuItem("绋嬪簭/鏇挎崲灞傛瑙嗗浘涓鍒朵綋鐨勬枃鏈垨鍥剧墖")] + public static void Open() + { + EditorWindow.GetWindow(typeof(ChangeTextAndImage)); + } + + static GameObject tagetObj; + + void OnGUI() + { + GUILayout.Label("Hierarchy 灞傛瑙嗗浘涓殑棰勫埗浣�"); + if (GUILayout.Button("灏員ext鏇存崲涓篢extEx")) + { + ChangeText(); + Debug.LogError("鏂囧瓧鏇挎崲鎴愬姛, 鍚庣画鍒朵綔閲忎笉鐢╞estfit鐨勬儏鍐典笅 璁╁瓧浣撴樉绀鸿寖鍥存渶澶у寲鍏煎娴峰鐗堟湰鐨勬枃瀛�"); + } + + if (GUILayout.Button("灏咺mage鏇存崲涓篒mageEx")) + { + ChangeImage(); + } + + } + + + void ChangeText() + { + //灏嗘寚瀹氱殑tagetObj棰勫埗浣撲腑鐨勬墍鏈塗ext鏇存崲涓篢extEx + //Selection.activeObject; + tagetObj = Selection.activeObject as GameObject; + + if (tagetObj == null) + { + Debug.LogError("璇峰皢棰勫埗浣撴嫋鍒癏ierarchy 灞傛瑙嗗浘 骞堕�変腑"); + return; + } + Text[] texts = tagetObj.GetComponentsInChildren<Text>(true); + + //灏員ext[]杞垚 List<GameObject> + List<GameObject> golist = new List<GameObject>(); + foreach (var item in texts) + { + golist.Add(item.gameObject); + } + + + for (int i = 0; i < golist.Count; i++) + { + var go = golist[i]; + Text text = go.GetComponent<Text>(); + + if (text as TextEx != null) + { + continue; + } + + var textContent = text.text; + var textFontSize = text.fontSize; + var textColor = text.color; + + //鍒犻櫎Text + DestroyImmediate(text); + + // 鏇挎崲涔嬪悗鏂囧瓧鍙兘涓嶅彲瑙侊紝闇�鎵嬪姩璋冩暣鍚勪釜缁勪欢鐨勫楂� + // 灏介噺涓嶇敤bestfit鐨勬儏鍐典笅 璁╁瓧浣撴樉绀鸿寖鍥存渶澶у寲鍏煎娴峰鐗堟湰鐨勬枃瀛� + + TextEx textEx = go.AddMissingComponent<TextEx>(); + textEx.text = textContent; + textEx.font = FontUtility.preferred; + textEx.fontSize = textFontSize; + textEx.raycastTarget = false; + textEx.alignment = TextAnchor.MiddleCenter; + textEx.horizontalOverflow = HorizontalWrapMode.Wrap; + textEx.verticalOverflow = VerticalWrapMode.Truncate; + textEx.resizeTextForBestFit = false; + textEx.color = textColor; + + } + + + + } + + + void ChangeImage() + { + //灏嗘寚瀹氱殑tagetObj棰勫埗浣撲腑鐨勬墍鏈塈mage鏇存崲涓篒mageEx + //Selection.activeObject; + tagetObj = Selection.activeObject as GameObject; + + if (tagetObj == null) + { + Debug.LogError("璇峰皢棰勫埗浣撴嫋鍒癏ierarchy 灞傛瑙嗗浘 骞堕�変腑"); + return; + } + Image[] images = tagetObj.GetComponentsInChildren<Image>(true); + + //灏咺mage[]杞垚 List<GameObject> + List<GameObject> golist = new List<GameObject>(); + foreach (var item in images) + { + golist.Add(item.gameObject); + } + + for (int i = 0; i < golist.Count; i++) + { + var go = golist[i]; + Image image = go.GetComponent<Image>(); + + if (image as ImageEx != null) + { + continue; + } + + var imageSprite = image.sprite; + var imageColor = image.color; + + //鍒犻櫎Image + DestroyImmediate(image); + + ImageEx imageEx = go.AddMissingComponent<ImageEx>(); + imageEx.sprite = imageSprite; + imageEx.raycastTarget = false; + } + } +} -- Gitblit v1.8.0