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;
|
}
|
}
|
}
|