using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
public class ChangeAtlas : MonoBehaviour
{
[MenuItem("Wpw/Assets/ChangeAll")]
static void Test_Compoent()
{
Rect rc = new Rect(5, 5, 400, 200);
ChangeAtlasWindow mcw = EditorWindow.GetWindowWithRect<ChangeAtlasWindow>(rc, false, "新建地图", true);
}
}
public class ChangeAtlasWindow : EditorWindow
{
public Sprite oldSprite;
public Sprite newSprite;
public List<Image> imageList;
string fullPath = "Assets/Resources" + "/";
private void OnGUI()
{
GUI.color = Color.white;
oldSprite = EditorGUILayout.ObjectField("旧图", oldSprite, typeof(Sprite), false) as Sprite;
newSprite = EditorGUILayout.ObjectField("新图", newSprite, typeof(Sprite), false) as Sprite;
if (oldSprite != null && newSprite != null)
{
if (GUILayout.Button("更改"))
{
ChangeAtlas();
}
}
}
/// <summary>
/// 注意,如果不是全部都要替换成新图集内图片,慎用!
/// </summary>
private void ChangeAtlas()
{
imageList = new List<Image>();
LoadAssetByAllChildren(ref imageList, fullPath);
for (int i = 0; i < imageList.Count; i++)
{
if (imageList[i].sprite == oldSprite)
{
imageList[i].sprite = newSprite;
}
}
}
/// <summary>
/// 读取组件列表
/// </summary>
/// <param name="parList"></param>
/// <param name="fullPath"></param>
private void LoadAssetByAllChildren(ref List<Image> parList, string fullPath)
{
//获取指定路径下面的所有资源文件
if (Directory.Exists(fullPath))
{
DirectoryInfo direction = new DirectoryInfo(fullPath);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
//Debug.Log(files.Length);
for (int i = 0; i < files.Length; i++)
{
//Debug.Log("i === " + i.ToString() + " == " + files[i].Name);
if (files[i].Name.EndsWith(".meta"))
{
continue;
}
if (files[i].Name.EndsWith(".prefab"))
{
string ss = files[i].FullName.Replace("D:\\client\\ClientMainProject_code\\Assets\\", "");
GameObject gobj = AssetDatabase.LoadAssetAtPath("Assets/" + ss, typeof(GameObject)) as GameObject;
List<Image> localImageList = new List<Image>();
gobj.GetComponentsInChildren<Image>(true, localImageList);
for (int j = 0; j < localImageList.Count; j++)
{
if (localImageList[j] != null)
{
parList.Add(localImageList[j]);
}
}
}
}
}
}
}