ImgQuoteUIWindow

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

namespace Daemo
{
class ImgQuoteUIWindow : EditorWindow
{
[MenuItem("Tools/UI RES/Img Quote UI %#E")]
private static void ShowWindow()
{
ImgQuoteUIWindow cw = (ImgQuoteUIWindow)EditorWindow.GetWindow(typeof(ImgQuoteUIWindow));
cw.minSize = new Vector2(800, 700);
cw.title = "ImgQuoteUI";
}
public int showType = 0;
private List<GameObject> allPrefabs = new List<GameObject>();
private Dictionary<GameObject, List<Texture2D>> allPrefabsTexture2Ds = new Dictionary<GameObject, List<Texture2D>>();
public void Awake()
{
this.UpdateAllPrefabs();
this.UpdateMsg();
}
private void UpdateAllPrefabs() {
this.allPrefabs = EDCheckPrefabRef.GetAllUIPrefabs();
this.allPrefabsTexture2Ds.Clear();
for (int i = 0; i < this.allPrefabs.Count; i++) {
GameObject nowObj = this.allPrefabs[i];
Image[] imgs = nowObj.GetComponentsInChildren<Image>(true);
List<Texture2D> textures = new List<Texture2D>();
for (int j = 0; j < imgs.Length; j++) {
Image img = imgs[j];
if(img.sprite !=null && img.sprite.texture != null)
{
textures.Add(img.sprite.texture);
}
}
this.allPrefabsTexture2Ds[nowObj] = textures;
}
}
private void UpdateMsg()
{
imgQuoteUI.Clear();
noQuoteImg.Clear();
useImageMsg = string.Empty;
UnityEngine.Object[] ps = GetSelectedPrefabs();
for (int i = 0; i < ps.Length; i++)
{
if (i == 0)
{
useImageMsg += "选中Imgs:";
}
Texture2D img = ps[i] as Texture2D;
if (i < ps.Length - 1)
{
useImageMsg += img.name + ",";
}
else {
useImageMsg += img.name;
}
bool isUse = false;
foreach (GameObject key in this.allPrefabsTexture2Ds.Keys) {
List<Texture2D> cValue = this.allPrefabsTexture2Ds[key];
for (int j = 0; j < cValue.Count; j++) {
if (img.name == cValue[j].name) {
List<GameObject> imgQuoteUI_UIs = null;
if (imgQuoteUI.ContainsKey(img))
{
imgQuoteUI_UIs = imgQuoteUI[img];
}
else {
imgQuoteUI_UIs = new List<GameObject>();
imgQuoteUI[img] = imgQuoteUI_UIs;
}
imgQuoteUI_UIs.Add(key);
if (!isUse) {
isUse = true;
}
break;
}
}
}
if (!isUse)
{
noQuoteImg.Add(img);
}
}

}
private Dictionary<Texture2D,List<GameObject>> imgQuoteUI = new Dictionary<Texture2D, List<GameObject>>();
private List<Texture2D> noQuoteImg = new List<Texture2D>();
private Vector2 scrollPos ;
private Vector2 scrollPos1;

private GameObject prefabObj = null;
private Texture2D noUseImg = null;
private string useImageMsg = string.Empty;

private void OnGUI()
{
GUILayout.Space(10);
if (GUILayout.Button("更新"))
{
this.UpdateMsg();
}
GUILayout.Space(10);
GUILayout.Label("功能说明:展示选中Img组被那些UI Prefab使用");
GUILayout.Space(10);
GUILayout.Label("使用说明:选中需要检查的图片组点击更新即可。");
GUILayout.Space(10);
GUILayout.Label(useImageMsg);
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
scrollPos =
EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(800), GUILayout.Height(400));
foreach (Texture2D key in this.imgQuoteUI.Keys) {
List<GameObject> cValue = imgQuoteUI[key];
for (int i = 0; i < cValue.Count; i++) {
EditorGUILayout.BeginHorizontal();
GUILayout.Label("("+ key.name + ")被(" + cValue[i].name + ")引用:");
prefabObj = cValue[i];
prefabObj = (GameObject)EditorGUILayout.ObjectField(prefabObj, typeof(GameObject), false, GUILayout.MinWidth(200f));
EditorGUILayout.EndHorizontal();
}
}
EditorGUILayout.EndScrollView();
EditorGUILayout.EndHorizontal();

if (this.noQuoteImg.Count > 0) {
GUILayout.Space(10);
GUILayout.Label("未被UI使用图片组:"+ noQuoteImg.Count);
scrollPos1 =
EditorGUILayout.BeginScrollView(scrollPos1, GUILayout.Width(800), GUILayout.Height(200));
for (int i = 0; i < noQuoteImg.Count; i++)
{
EditorGUILayout.BeginHorizontal();
noUseImg = noQuoteImg[i];
noUseImg = (Texture2D)EditorGUILayout.ObjectField(noUseImg, typeof(Texture2D), false, GUILayout.MinWidth(200f));
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndScrollView();
}

}
private UnityEngine.Object[] GetSelectedPrefabs()
{
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
}

}

posted @ 2017-07-27 09:40  rexzhao  阅读(100)  评论(0编辑  收藏  举报