三国卡牌客户端基础资源仓库
hch
2025-06-19 d43529e2d01bc7b153501ff5c15e00629a5dfb63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using 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("将Text更换为TextEx"))
        {
            ChangeText();
            Debug.LogError("文字替换成功, 后续制作量不用bestfit的情况下 让字体显示范围最大化兼容海外版本的文字");
        }
 
        if (GUILayout.Button("将Image更换为ImageEx"))
        {
            ChangeImage();
        }
 
    }
 
    
    void ChangeText()
    {
        //将指定的tagetObj预制体中的所有Text更换为TextEx
        //Selection.activeObject;
        tagetObj = Selection.activeObject as GameObject;
 
        if (tagetObj == null)
        {
            Debug.LogError("请将预制体拖到Hierarchy 层次视图 并选中");
            return;
        }
        Text[] texts = tagetObj.GetComponentsInChildren<Text>(true);
 
        //将Text[]转成 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预制体中的所有Image更换为ImageEx
        //Selection.activeObject;
        tagetObj = Selection.activeObject as GameObject;
 
        if (tagetObj == null)
        {
            Debug.LogError("请将预制体拖到Hierarchy 层次视图 并选中");
            return;
        }
        Image[] images = tagetObj.GetComponentsInChildren<Image>(true);
 
        //将Image[]转成 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;
        }   
    }
}