using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEditor;
|
using System.Text.RegularExpressions;
|
using System.IO;
|
using UnityEngine.UI;
|
using vnxbqy.UI;
|
public class ReplaceImgTool : EditorWindow {
|
|
[MenuItem("策划工具/批量替换预制体图片", false)]
|
public static void CreateWindow()
|
{
|
ReplaceImgTool window = (ReplaceImgTool)EditorWindow.GetWindow(typeof(ReplaceImgTool), true, "批量替换预制体图片", true);
|
window.Show();
|
}
|
|
private static Sprite replaceSprite;
|
private static int redpointImgSize = 23;
|
private static string topFilePath=string.Empty;
|
private static string replaceName = string.Empty;
|
private static GameObject replacePrefab = null;
|
|
static ReplaceType replaceType = ReplaceType.RedPoint;
|
|
enum ReplaceType
|
{
|
RedPoint,
|
}
|
|
private void OnGUI()
|
{
|
EditorGUILayout.BeginHorizontal();
|
replaceSprite = EditorGUILayout.ObjectField("Replace Sprite", replaceSprite, typeof(Sprite), false,GUILayout.Width(300)) as Sprite;
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.BeginHorizontal();
|
redpointImgSize = EditorGUILayout.IntField("Img Size", redpointImgSize);
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.BeginHorizontal();
|
replaceType=(ReplaceType)EditorGUILayout.EnumPopup("ReplaceType:", replaceType);
|
EditorGUILayout.EndHorizontal();
|
|
switch (replaceType) {
|
case ReplaceType.RedPoint: {
|
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets);
|
topFilePath = (arr == null || arr.Length == 0) ? topFilePath : Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[0]);
|
EditorGUILayout.BeginHorizontal();
|
topFilePath = EditorGUILayout.TextField("Top File Path:", topFilePath);
|
EditorGUILayout.EndHorizontal();
|
}
|
break;
|
}
|
EditorGUILayout.BeginHorizontal();
|
if (GUILayout.Button("开始替换",GUILayout.Width(100))) {
|
Replace();
|
}
|
EditorGUILayout.EndHorizontal();
|
}
|
|
|
private static void Replace()
|
{
|
if (replaceSprite == null)
|
{
|
Debug.LogError("图片不能为空");
|
return;
|
}
|
if (replaceType ==ReplaceType.RedPoint&&Regex.IsMatch(topFilePath, @".*\..*")) {
|
Debug.LogError("必须为文件夹路径");
|
return;
|
}
|
switch (replaceType) {
|
case ReplaceType.RedPoint: {
|
string[] filepaths = Directory.GetFiles(topFilePath, "*.prefab", SearchOption.AllDirectories);
|
int startIndex = 0;
|
EditorApplication.update = delegate () {
|
string file = filepaths[startIndex];
|
file = file.Substring(file.IndexOf("Assets"));
|
bool isCancel = EditorUtility.DisplayCancelableProgressBar("替换图片资源", file, (float)startIndex / (float)filepaths.Length);
|
GameObject _prefab = AssetDatabase.LoadAssetAtPath<GameObject>(file);
|
ReplaceRedPoint(_prefab);
|
startIndex++;
|
if (isCancel || startIndex >= filepaths.Length) {
|
EditorUtility.ClearProgressBar();
|
EditorApplication.update = null;
|
startIndex = 0;
|
AssetDatabase.SaveAssets();
|
AssetDatabase.Refresh();
|
}
|
};
|
}
|
break;
|
}
|
}
|
|
private static void ReplaceRedPoint(GameObject source)
|
{
|
if (RecursionFind(source.transform)) {
|
EditorUtility.SetDirty(source);
|
}
|
}
|
|
private static bool RecursionFind(Transform parent)
|
{
|
RedpointBehaviour redpointBehaviour = parent.GetComponent<RedpointBehaviour>();
|
bool chanaged = false;
|
if (redpointBehaviour != null) {
|
if (redpointBehaviour.simpleRedpoint != null) {
|
Image img = redpointBehaviour.simpleRedpoint.GetComponent<Image>();
|
if (img != null) {
|
img.sprite = replaceSprite;
|
img.color = Color.white;
|
img.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
|
img.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
|
img.rectTransform.sizeDelta = new Vector2(redpointImgSize, redpointImgSize);
|
}
|
}
|
if (redpointBehaviour.quantityRedpoint != null) {
|
Image img = redpointBehaviour.quantityRedpoint.GetComponent<Image>();
|
if (img != null) {
|
img.sprite = replaceSprite;
|
img.color = Color.white;
|
img.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
|
img.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
|
img.rectTransform.sizeDelta = new Vector2(redpointImgSize, redpointImgSize);
|
}
|
}
|
if (redpointBehaviour.quantityText != null) {
|
redpointBehaviour.quantityText.color = new Color32(247, 247, 247, 255);
|
redpointBehaviour.quantityText.fontSize = 22;
|
redpointBehaviour.quantityText.rectTransform.sizeDelta = new Vector2(25, 25);
|
redpointBehaviour.quantityText.rectTransform.localPosition = Vector3.zero;
|
}
|
chanaged = true;
|
}
|
foreach (Transform child in parent) {
|
if (RecursionFind(child)) {
|
chanaged = true;
|
}
|
}
|
return chanaged;
|
}
|
}
|