unity perfab 资源检查
2017-04-14 16:14
267 查看
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class ResourceTool : EditorWindow
{
[MenuItem("Test/Resource Tools")]
static void ShowResourceTool()
{
//创建窗口
Rect rc = new Rect(0, 0, 800, 600);
ResourceTool window = (ResourceTool)EditorWindow.GetWindow(typeof(ResourceTool), true, "Test Resource Tool");
window.Show();
}
private int selectionGridInt = 0;
string[] selectionStrings;
Vector2 scrollPosition;
Dictionary<string, List<string>> Dependences = new Dictionary<string, List<string>>();
Dictionary<string, List<string>> References = new Dictionary<string, List<string>>();
List<string> currentDepends = new List<string>();
List<string> currentReferences = new List<string>();
public void Awake()
{
this.Dependences.Clear();
this.References.Clear();
}
bool disableSelectEvent = false;
void OnSelectionChange()
{
if (!disableSelectEvent && Selection.activeObject != null && Dependences.Count > 0)
{
UnityEngine.Debug.Log("OnSelectionChange:" + Selection.activeObject.name);
string curpath = AssetDatabase.GetAssetPath(Selection.activeObject);
this.Dependences.TryGetValue(curpath, out this.currentDepends);
this.References.TryGetValue(curpath, out this.currentReferences);
this.Repaint();
}
disableSelectEvent = false;
}
void SelectAsset(string path)
{
disableSelectEvent = true;
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(path);
}
void OnGUI()
{
if (GUILayout.Button("UI Resources Check Mode", GUILayout.Width(200)))
{
//perfab 目录可以有多个
this.GenerateDependence(new string[] { "Assets/Game/Textures", "Assets/Game/Textures/fx/"});
}
if (GUILayout.Button("Cleanup UI Resources", GUILayout.Width(200)))
{
this.CleanupResources();
}
if (GUILayout.Button("Modify Particle Material", GUILayout.Width(200)))
{
string[] files = Directory.GetFiles("Assets/FX/Materials");
foreach (string file in files)
{
Material mat = (Material)AssetDatabase.LoadMainAssetAtPath(file);
if (mat != null)
{
switch (mat.shader.name)
{
case "Particles/Additive":
mat.shader = Shader.Find("Mobile/Particles/Additive");
break;
case "Particles/Alpha Blended":
mat.shader = Shader.Find("Mobile/Particles/Alpha Blended");
break;
case "Particles/Multiply":
mat.shader = Shader.Find("Mobile/Particles/Multiply");
break;
default:
break;
}
}
}
}
//Dependences check area
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUIStyle.none);
GUILayout.BeginArea(new Rect(0, 0, 400, 1000));
GUILayout.Label("References:");
if (this.currentReferences != null)
{
foreach (string refer in this.currentReferences)
{
if (GUILayout.Button(refer, GUILayout.Width(380)))
{
this.SelectAsset(refer);
}
}
}
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(400, 0, 400, 1000));
GUILayout.Label("Depends:");
if (this.currentDepends != null)
{
foreach (string depend in this.currentDepends)
{
if (GUILayout.Button(depend, GUILayout.Width(380)))
{
this.SelectAsset(depend);
}
}
}
GUILayout.EndArea();
if (selectionStrings != null)
{
selectionGridInt = GUILayout.SelectionGrid(selectionGridInt, selectionStrings, 1);
if (selectionGridInt >= selectionStrings.Length)
selectionGridInt = selectionStrings.Length - 1;
if (selectionGridInt > 0)
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(selectionStrings[selectionGridInt]);
}
GUILayout.EndScrollView();
}
void CleanupResources()
{
//查找未被使用的资源
Dictionary<string, List<string>> AssetsRefMap = new Dictionary<string, List<string>>();
//string[] files = Directory.GetFiles("Assets/Resources/UI/","*", SearchOption.AllDirectories);
//foreach (string file in files)
//{
// if (file.EndsWith(".meta"))
// continue;
// if (file.EndsWith(".prefab"))
// continue;
// Assets[file.Replace("/","/")] = 0;
//}
string UIRES = "Assets/UI_OLD/";
string[] files = Directory.GetFiles(UIRES, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (file.EndsWith(".meta"))
continue;
AssetsRefMap[file.Replace("\\", "/")] = new List<string>();
}
files = Directory.GetFiles("ProjectSettings/", "*.asset", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && AssetsRefMap.ContainsKey(dep))
{
AssetsRefMap[dep].Add(key);
}
}
}
files = Directory.GetFiles("Assets/BuildOnlyAssets/UI/prefab/", "*.prefab", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && AssetsRefMap.ContainsKey(dep))
{
AssetsRefMap[dep].Add(key);
}
}
}
files = Directory.GetFiles("Assets/Prefabs/", "*.prefab", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && AssetsRefMap.ContainsKey(dep))
{
AssetsRefMap[dep].Add(key);
}
}
}
List<string> result = new List<string>();
foreach (KeyValuePair<string, List<string>> kv in AssetsRefMap)
{
if (kv.Value.Count == 0)
{
//result.Add(kv.Key);
File.Delete(kv.Key);
}
else
{
result.Add(kv.Key);
foreach (string file in kv.Value)
{
result.Add(file);
}
}
}
this.selectionStrings = result.ToArray();
}
void CheckDependsFX()
{
//查找未被使用的资源
Dictionary<string, int> Assets = new Dictionary<string, int>();
//string[] files = Directory.GetFiles("Assets/Resources/UI/","*", SearchOption.AllDirectories);
//foreach (string file in files)
//{
// if (file.EndsWith(".meta"))
// continue;
// if (file.EndsWith(".prefab"))
// continue;
// Assets[file.Replace("/","/")] = 0;
//}
string[] files = Directory.GetFiles("Assets/UI/", "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (file.EndsWith(".meta"))
continue;
Assets[file.Replace("\\", "/")] = 0;
}
files = Directory.GetFiles("Assets/Resources/UI/prefab/", "*.prefab", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && Assets.ContainsKey(dep))
{
Assets[dep]++;
}
}
}
List<string> result = new List<string>();
foreach (KeyValuePair<string, int> kv in Assets)
{
if (kv.Value == 0)
result.Add(kv.Key);
}
this.selectionStrings = result.ToArray();
}
public void GenerateDependence(string[] paths)
{
Debug.Log("GenerateDependence");
//查找未被使用的资源
List<string> files = new List<string>();
foreach (string path in paths)
{
files.AddRange(Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories));
}
for (int i = 0; i < files.Count; i++)
{
string key = files[i];
string asset = key.Replace('\\', '/');
string[] depends = AssetDatabase.GetDependencies(new string[] { asset });
foreach (string dep in depends)
{
if (dep != asset)
{
AddDepends(asset, dep);
}
}
EditorUtility.DisplayProgressBar("Collect Dependences", asset, (float)i / (float)files.Count);
}
EditorUtility.ClearProgressBar();
}
void AddDepends(string asset, string depend)
{
if (!this.Dependences.ContainsKey(asset))
this.Dependences[asset] = new List<string>();
if (!this.Dependences[asset].Contains(depend))
this.Dependences[asset].Add(depend);
if (!this.References.ContainsKey(depend))
this.References[depend] = new List<string>();
if (!this.References[depend].Contains(asset))
this.References[depend].Add(asset);
}
}
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class ResourceTool : EditorWindow
{
[MenuItem("Test/Resource Tools")]
static void ShowResourceTool()
{
//创建窗口
Rect rc = new Rect(0, 0, 800, 600);
ResourceTool window = (ResourceTool)EditorWindow.GetWindow(typeof(ResourceTool), true, "Test Resource Tool");
window.Show();
}
private int selectionGridInt = 0;
string[] selectionStrings;
Vector2 scrollPosition;
Dictionary<string, List<string>> Dependences = new Dictionary<string, List<string>>();
Dictionary<string, List<string>> References = new Dictionary<string, List<string>>();
List<string> currentDepends = new List<string>();
List<string> currentReferences = new List<string>();
public void Awake()
{
this.Dependences.Clear();
this.References.Clear();
}
bool disableSelectEvent = false;
void OnSelectionChange()
{
if (!disableSelectEvent && Selection.activeObject != null && Dependences.Count > 0)
{
UnityEngine.Debug.Log("OnSelectionChange:" + Selection.activeObject.name);
string curpath = AssetDatabase.GetAssetPath(Selection.activeObject);
this.Dependences.TryGetValue(curpath, out this.currentDepends);
this.References.TryGetValue(curpath, out this.currentReferences);
this.Repaint();
}
disableSelectEvent = false;
}
void SelectAsset(string path)
{
disableSelectEvent = true;
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(path);
}
void OnGUI()
{
if (GUILayout.Button("UI Resources Check Mode", GUILayout.Width(200)))
{
//perfab 目录可以有多个
this.GenerateDependence(new string[] { "Assets/Game/Textures", "Assets/Game/Textures/fx/"});
}
if (GUILayout.Button("Cleanup UI Resources", GUILayout.Width(200)))
{
this.CleanupResources();
}
if (GUILayout.Button("Modify Particle Material", GUILayout.Width(200)))
{
string[] files = Directory.GetFiles("Assets/FX/Materials");
foreach (string file in files)
{
Material mat = (Material)AssetDatabase.LoadMainAssetAtPath(file);
if (mat != null)
{
switch (mat.shader.name)
{
case "Particles/Additive":
mat.shader = Shader.Find("Mobile/Particles/Additive");
break;
case "Particles/Alpha Blended":
mat.shader = Shader.Find("Mobile/Particles/Alpha Blended");
break;
case "Particles/Multiply":
mat.shader = Shader.Find("Mobile/Particles/Multiply");
break;
default:
break;
}
}
}
}
//Dependences check area
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUIStyle.none);
GUILayout.BeginArea(new Rect(0, 0, 400, 1000));
GUILayout.Label("References:");
if (this.currentReferences != null)
{
foreach (string refer in this.currentReferences)
{
if (GUILayout.Button(refer, GUILayout.Width(380)))
{
this.SelectAsset(refer);
}
}
}
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(400, 0, 400, 1000));
GUILayout.Label("Depends:");
if (this.currentDepends != null)
{
foreach (string depend in this.currentDepends)
{
if (GUILayout.Button(depend, GUILayout.Width(380)))
{
this.SelectAsset(depend);
}
}
}
GUILayout.EndArea();
if (selectionStrings != null)
{
selectionGridInt = GUILayout.SelectionGrid(selectionGridInt, selectionStrings, 1);
if (selectionGridInt >= selectionStrings.Length)
selectionGridInt = selectionStrings.Length - 1;
if (selectionGridInt > 0)
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(selectionStrings[selectionGridInt]);
}
GUILayout.EndScrollView();
}
void CleanupResources()
{
//查找未被使用的资源
Dictionary<string, List<string>> AssetsRefMap = new Dictionary<string, List<string>>();
//string[] files = Directory.GetFiles("Assets/Resources/UI/","*", SearchOption.AllDirectories);
//foreach (string file in files)
//{
// if (file.EndsWith(".meta"))
// continue;
// if (file.EndsWith(".prefab"))
// continue;
// Assets[file.Replace("/","/")] = 0;
//}
string UIRES = "Assets/UI_OLD/";
string[] files = Directory.GetFiles(UIRES, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (file.EndsWith(".meta"))
continue;
AssetsRefMap[file.Replace("\\", "/")] = new List<string>();
}
files = Directory.GetFiles("ProjectSettings/", "*.asset", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && AssetsRefMap.ContainsKey(dep))
{
AssetsRefMap[dep].Add(key);
}
}
}
files = Directory.GetFiles("Assets/BuildOnlyAssets/UI/prefab/", "*.prefab", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && AssetsRefMap.ContainsKey(dep))
{
AssetsRefMap[dep].Add(key);
}
}
}
files = Directory.GetFiles("Assets/Prefabs/", "*.prefab", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && AssetsRefMap.ContainsKey(dep))
{
AssetsRefMap[dep].Add(key);
}
}
}
List<string> result = new List<string>();
foreach (KeyValuePair<string, List<string>> kv in AssetsRefMap)
{
if (kv.Value.Count == 0)
{
//result.Add(kv.Key);
File.Delete(kv.Key);
}
else
{
result.Add(kv.Key);
foreach (string file in kv.Value)
{
result.Add(file);
}
}
}
this.selectionStrings = result.ToArray();
}
void CheckDependsFX()
{
//查找未被使用的资源
Dictionary<string, int> Assets = new Dictionary<string, int>();
//string[] files = Directory.GetFiles("Assets/Resources/UI/","*", SearchOption.AllDirectories);
//foreach (string file in files)
//{
// if (file.EndsWith(".meta"))
// continue;
// if (file.EndsWith(".prefab"))
// continue;
// Assets[file.Replace("/","/")] = 0;
//}
string[] files = Directory.GetFiles("Assets/UI/", "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (file.EndsWith(".meta"))
continue;
Assets[file.Replace("\\", "/")] = 0;
}
files = Directory.GetFiles("Assets/Resources/UI/prefab/", "*.prefab", SearchOption.AllDirectories);
foreach (string key in files)
{
string[] depends = AssetDatabase.GetDependencies(new string[] { key });
foreach (string dep in depends)
{
if (dep != key && Assets.ContainsKey(dep))
{
Assets[dep]++;
}
}
}
List<string> result = new List<string>();
foreach (KeyValuePair<string, int> kv in Assets)
{
if (kv.Value == 0)
result.Add(kv.Key);
}
this.selectionStrings = result.ToArray();
}
public void GenerateDependence(string[] paths)
{
Debug.Log("GenerateDependence");
//查找未被使用的资源
List<string> files = new List<string>();
foreach (string path in paths)
{
files.AddRange(Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories));
}
for (int i = 0; i < files.Count; i++)
{
string key = files[i];
string asset = key.Replace('\\', '/');
string[] depends = AssetDatabase.GetDependencies(new string[] { asset });
foreach (string dep in depends)
{
if (dep != asset)
{
AddDepends(asset, dep);
}
}
EditorUtility.DisplayProgressBar("Collect Dependences", asset, (float)i / (float)files.Count);
}
EditorUtility.ClearProgressBar();
}
void AddDepends(string asset, string depend)
{
if (!this.Dependences.ContainsKey(asset))
this.Dependences[asset] = new List<string>();
if (!this.Dependences[asset].Contains(depend))
this.Dependences[asset].Add(depend);
if (!this.References.ContainsKey(depend))
this.References[depend] = new List<string>();
if (!this.References[depend].Contains(asset))
this.References[depend].Add(asset);
}
}
相关文章推荐
- Unity 检查丢失引用的资源 Missing
- [资源分享]一个类似农药的UNITY分享
- 玩转Unity资源,对象和序列化(上)
- Unity热更新系列之AssetBundle(2) ——详述如何打AssetBundle资源
- unity动态加载远程资源
- [Unity优化]资源优化之纹理优化
- C# Unity用AssetBundle加载本地资源 (1)
- Unity加载资源的3种方法
- 快速定位性能瓶颈,检查出所有资源(CPU、内存、磁盘IO等)的利用率(utilization)、饱和度(saturation)和错误(error)度量,即USE方法
- Unity Editor 编辑器扩展 十三 自定义创建资源
- 【VR】Leap Motion 官网文档(一)Unity资源与插件
- Unity下载网络资源到本地,再从本地调用
- Unity5.x ScriptableObject数据存储结合AssetBundle打包实现数据资源热更新
- [Unity]AssetBundle资源更新以及多线程下载
- 002-unity 资源及资源类型 srt字幕文件
- [Unity] Unity3D研究院编辑器之自定义默认资源的Inspector面板
- unity 资源冗余校验-删除一些没用不敢删的素材
- unity的animation动画资源压缩
- Unity读取本地图片资源
- [资源分享]unity激光效果分享