From 6fe592f5b2e1c3f30a5e2ef1de30a4111aab0ada Mon Sep 17 00:00:00 2001
From: hch <305670599@qq.com>
Date: 星期四, 19 六月 2025 15:37:43 +0800
Subject: [PATCH] 0312 psd导入拼图后文本和图片的处理
---
Assets/Editor/Tool/ChangeTextAndImage.cs.meta | 11 +++
Assets/Editor/Tool/ChangeTextAndImage.cs | 136 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 147 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;
+ }
+ }
+}
diff --git a/Assets/Editor/Tool/ChangeTextAndImage.cs.meta b/Assets/Editor/Tool/ChangeTextAndImage.cs.meta
new file mode 100644
index 0000000..fec97f6
--- /dev/null
+++ b/Assets/Editor/Tool/ChangeTextAndImage.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: edda728257d3f8d4e9ab068c742ccb07
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
--
Gitblit v1.8.0