using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class ImageInfoHelper: MonoBehaviour
{
public static string UIPrefabPath = "../Project/Assets/Resources/UI";
public static string UIImagePath = "../Project/Assets/Resources/Images/UI";
private Dictionary<int, List<string>> info = new Dictionary<int, List<string>>();
public void Add(int id, string path)
{
if (info.ContainsKey(id))
{
if (!info[id].Contains(path))
{
info[id].Add(path);
}
}
else
{
List<string> tempSt = new List<string>();
tempSt.Add(path);
info.Add(id, tempSt);
}
}
private List<int> SamePath = new List<int>();
private List<Transform> panels = new List<Transform>();
int Num = 0;
public Dictionary<string, List<Image>> UiPrefabInfo = new Dictionary<string, List<Image>>();
private void AddPrefabInfo(string path,Image image)
{
Debug.Log(path);
Num = UiPrefabInfo.Count;
if(UiPrefabInfo.ContainsKey(path))
{
if(!UiPrefabInfo[path].Contains(image))
{
UiPrefabInfo[path].Add(image);
}
}
else
{
List<Image> images = new List<Image>();
images.Add(image);
UiPrefabInfo.Add(path, images);
}
}
private Transform parent = null;
public Transform Parent
{
get
{
if(parent==null)
{
parent = GameObject.Find("TheStoreCanvas").transform;
}
return parent;
}
}
[ContextMenu("创建所有panel")]
public void InitAllUiPanel()
{
panels.Clear();
Debug.Log("开始生成Panel");
foreach (string subdir in Directory.GetDirectories(UIPrefabPath))
{
foreach (string subFile in Directory.GetFiles(subdir))
{
if (Path.GetExtension(subFile) == ".prefab")
{
string name = Path.GetFileNameWithoutExtension(subFile); /*Assets / Resources /*/
string path = subFile.Replace("../Project/", "")/*.Replace("\\","/").Replace(".prefab","")*/;
InitPrefabs(path, name);
}
}
}
Debug.Log("生成Panel结束");
//OutputInfor();
ChickTusu();
//OutputInfor();
}
private void InitPrefabs(string path,string name)
{
GameObject gameObject= AssetDatabase.LoadAssetAtPath<GameObject>(path);
GameObject instance = PrefabUtility.InstantiatePrefab(gameObject) as GameObject;
//string[] ids = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Resources/UI" });
// GameObject gameObject =(GameObject) Resources.Load(path);
//Transform temp= GameObject.Instantiate(gameObject, Parent).transform;
//temp.name = name;
//temp.gameObject.SetActive(true);
if (!panels.Contains(instance.transform))
panels.Add(instance.transform);
}
//[ContextMenu("ChickTusu")]
private void ChickTusu()
{
info.Clear();
//Debug.Log("开始检查图素来源");
foreach (Transform child in panels)
{
if(child!=null&&child.parent==null)
{
// Debug.Log(" 检查child图素来源");
SaveInfo(child);
}
}
//Debug.Log("检查图素来源结束");
}
private void SaveInfo(Transform temp)
{
Image[] images = temp.GetComponentsInChildren<Image>();
foreach (Image child in images)
{
if (child.sprite != null)
{
int id = child.sprite.GetInstanceID();
string imgPath = "";
imgPath += child.name;
Transform tempParent = child.transform.parent;
while(tempParent != null /*&& !tempParent.Equals(parent)*/)
{
imgPath = tempParent.name+"/"+imgPath ;
tempParent = tempParent.parent;
}
Add(id, imgPath);
}
}
}
[ContextMenu("输出复用信息")]
public void OutputInfor()
{
Debug.Log("开始输出信息:");
SamePath.Clear();
foreach (KeyValuePair<int,List<string>> child in info)
{
if(child.Value.Count>1)
{
string rootParent = "";
string prefabPath = AssetDatabase.GetAssetPath(child.Key);
string info_txt = "";
bool isDeb = false;
info_txt+="多引用图素的地址:" + prefabPath+"\n";
foreach (string child_2 in child.Value)
{
info_txt+="\t引用图素的Image:" + child_2+"\n";
if(rootParent=="")
{
rootParent = child_2.Split('/')[0];
}
else
{
if(rootParent!=child_2.Split('/')[0])
{
isDeb = true;
if (!SamePath.Contains(child.Key))
{
info_txt += "添加";
SamePath.Add(child.Key);
}
}
}
}
if(isDeb)
{
Debug.Log(info_txt);
}
}
}
Debug.Log("信息输出完毕");
}
[ContextMenu("拷贝复用得图片到对应得文件夹")]
public void CopyMultipleReferenceFiles()
{
Debug.Log("开始拷贝");
UiPrefabInfo.Clear();
foreach (int id in SamePath)
{
string prefabPath = AssetDatabase.GetAssetPath(id);
string[] prefabPaths = prefabPath.Split('/');
if(prefabPaths[0].Equals("Resources"))
{
continue;
}
List<string> paths = new List<string>();
info.TryGetValue(id, out paths);
foreach (string child in paths)
{
//string imaPathOfPrefab = child.
Image tempIma = GameObject.Find(child).GetComponent<Image>();
if(tempIma==null)
{
Debug.Log(tempIma+" 未找到Image组件");
}
else
{
string[] panelsPath = child.Split('/');
string ImagePath = panelsPath[0];
if (!prefabPaths[prefabPaths.Length - 2].Equals(panelsPath[0]))
{
string newPath= Copy(prefabPath, ImagePath);
AssetDatabase.Refresh();
AddPrefabInfo(newPath, tempIma);
}
}
}
}
Debug.Log("拷贝完成");
}
private string Copy(string startPath, string pointName)
{
string aimPath = Path.Combine(UIImagePath, pointName);
DirectoryInfo directoryInfo = new DirectoryInfo(aimPath);
FileInfo file = new FileInfo(startPath);
if (!Directory.Exists(directoryInfo.FullName))
{
Directory.CreateDirectory(directoryInfo.FullName);
}
File.Copy(file.FullName, Path.Combine(directoryInfo.FullName, file.Name), true);
AssetDatabase.Refresh();
string newImagePath = "Images/UI/" + pointName + "/" + file.Name;
string AssetPath = "Assets/Resources/" + newImagePath;
TextureImporter textureImporter = TextureImporter.GetAtPath(AssetPath) as TextureImporter;
if(textureImporter!=null)
{
textureImporter.textureType = TextureImporterType.Sprite;
textureImporter.SaveAndReimport();
}
else
{
Debug.LogError("没有找打Image: "+ newImagePath);
}
return newImagePath;
}
[ContextMenu("关联新建得图片和UI得关系")]
public void Guanlian()
{
Debug.Log("开始关联");
foreach (var child in UiPrefabInfo)
{
Sprite sprite = Resources.Load<Sprite>(child.Key.Split('.')[0]);
if(sprite==null)
{
Debug.Log(sprite);
}
foreach(var image in child.Value)
{
if(image==null)
{
Debug.Log("error");
return;
}
image.sprite = sprite;
}
}
Debug.Log("关联完成");
Debug.Log("开始保存预设");
foreach (Transform tran in panels)
{
PrefabUtility.ReplacePrefab(tran.gameObject, PrefabUtility.GetCorrespondingObjectFromSource(tran.gameObject) /*GetPrefabParent(tran.gameObject)*/, ReplacePrefabOptions.ConnectToPrefab);
AssetDatabase.Refresh();
}
Debug.Log("保存预设完成");
}
[ContextMenu("移除所有创建得Panel")]
public void RemoveAllPanel()
{
//Transform[] tempPanels = parent.GetComponentsInChildren<Transform>();
//for(int i=0;i< tempPanels.Length;i++)
//{
// if (tempPanels[i]==null)
// continue;
// if(!tempPanels[i].name.Equals(parent.name))
// {
// DestroyImmediate(tempPanels[i].gameObject);
// }
//}
info.Clear();
SamePath.Clear();
panels.Clear();
UiPrefabInfo.Clear();
Num = 0;
}
}
从上往下执行 ,测试文本UI预设路径在Resources/UI里面
图片在Resources/Image/UI里面