一个简单的prefab上Image/Texture替换工具
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;
public class ReplaceSprite : EditorWindow
{
[MenuItem("Assets/客户端/UI工具/查找引用图片替换")]
static void SearchRefrencePrefab()
{
ReplaceSprite window = (ReplaceSprite)EditorWindow.GetWindow(typeof(ReplaceSprite), false, "Searching", true);
window.Show();
}
private Object SearchObject;
private Sprite TargetObject;
private bool IsReplace=false;
bool showBtn =false;
private Vector2 scroPos;
private void OnGUI()
{
EditorGUILayout.BeginVertical();
EditorGUILayout.SelectableLabel("导出节点文本路径:"+m_savepath);
EditorGUILayout.EndVertical();
EditorGUILayout.BeginHorizontal();
SearchObject = (Sprite)EditorGUILayout.ObjectField(SearchObject, typeof(Object), true, GUILayout.Width(200));
if (GUILayout.Button("Search", GUILayout.Width(100)))
{
if (SearchObject == null)
return;
Find();
}
TargetObject = (Sprite)EditorGUILayout.ObjectField(TargetObject, typeof(Object), true, GUILayout.Width(200));
if (GUILayout.Button("Replace", GUILayout.Width(100)))
{
if (SearchObject == null)
return;
IsReplace = true;
Find();
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginVertical();
showBtn =EditorGUILayout.Toggle("是否导出查找节点", showBtn,GUILayout.Width(10));
if(showBtn)
{
IsReplace = true;
}
if (GUILayout.Button("Clear", GUILayout.Width(100)))
{
m_PrefabList.Clear();
m_prefablable = String.Empty;
}
EditorGUILayout.EndVertical();
scroPos=GUILayout.BeginScrollView(scroPos, false, true);
EditorGUILayout.BeginVertical();
for (int i = 0; i < m_PrefabList?.Count; i++)
{
EditorGUILayout.ObjectField(m_PrefabList[i], typeof(Object), true, GUILayout.Width(300));
}
EditorGUILayout.EndHorizontal();
GUILayout.EndScrollView();
}
public List<GameObject> GetAllPrefabByDirectory(string path)
{
string[] files = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
List<GameObject> _prefabList = new List<GameObject>();
GameObject _prefab;
foreach (var _path in files)
{
_prefab = AssetDatabase.LoadAssetAtPath(_path, typeof(GameObject)) as GameObject;
_prefabList.Add(_prefab);
}
return _prefabList;
}
List<GameObject> m_PrefabList = new List<GameObject>();
string m_savepath = Application.streamingAssetsPath + "/Searching.txt";
private string m_prefablable = string.Empty;
private void Find()
{
m_prefablable = string.Empty;
m_PrefabList.Clear();
string m_sprite = SearchObject.name;
if (m_sprite == null) return;
List<Image> _allImageList;
Debug.Log("选择了:" + m_sprite);
List<GameObject> _prefabList = GetAllPrefabByDirectory("Assets/Arts/px_ugui/ui_prefab");
int _length = _prefabList.Count;
bool _isChanged=false;
Debug.Log("prefab的数量:" + _prefabList.Count);
for (int i = 0; i < _prefabList.Count; i++)
{
EditorUtility.DisplayCancelableProgressBar("Checking", _prefabList[i].ToString(), (float)i / _length);
if (_prefabList[i] == null)
{
continue;
}
_allImageList = new List<Image>(_prefabList[i].GetComponentsInChildren<Image>(true));
if (_allImageList == null || _allImageList.Count <= 0)
{
continue;
}
foreach (var image in _allImageList)
{
if (image.sprite == null)
{
continue;
}
if (image.sprite.Equals(SearchObject))
{
if (TargetObject != null&& IsReplace)
{
image.sprite = TargetObject;
_isChanged = true;
}
m_prefablable +=i+("--PrefabName: " + _prefabList[i] + "--节点名: " + image.gameObject.name + "\n") ;
m_PrefabList.Add(_prefabList[i]);
}
}
if (_isChanged)
{
PrefabUtility.SavePrefabAsset(_prefabList[i]);
_isChanged = false;
}
}
EditorUtility.ClearProgressBar();
Debug.Log("引用图片---" + m_PrefabList.Count + " 个");
foreach (var item in m_PrefabList)
{
if (m_PrefabList.Count <= 0)
{
Debug.Log("无引用!");
}
else
{
Debug.Log(item);
}
}
if (showBtn)
{
SaveData();
}
else
{
if (File.Exists(m_savepath))
{
File.Delete(m_savepath);
}
}
}
void SaveData()
{
if (!File.Exists(m_savepath))
{
File.Create(m_savepath);
}
File.WriteAllText(m_savepath, m_prefablable);
Debug.Log("保存成功");
AssetDatabase.Refresh();
}
}
|