码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d 检查哪些prefab引用了某个UIAtlas

时间:2015-11-07 12:10:27      阅读:534      评论:0      收藏:0      [点我收藏+]

标签:

适用情景:策划在用NGUI制作UI prefab时经常会使用一些临时的Atlas,然后再想改就不知道都哪些使用了。现在想修改下使用临时资源的GameObject

使用方式,先选中某个prefab或者某个包含prefab的文件夹,点Tools->Find atlas reference object in current select->输入Atlas的名字,不用带缀名

 

代码

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public class FindAtlasReference : ScriptableWizard
{
	[Tooltip("Atlas Name")]
	public string AtlasName;

	// Use this for initialization
	void Start()
	{

	}

	[MenuItem("Tools/Find atlas reference object in current select")]
	public static void OpenDialog()
	{
		DisplayWizard<FindAtlasReference>("Find object using this atlas", "Find", "Cancel");
	}

	void OnWizardCreate()
	{
		Find();
	}
	void OnWizardOtherButton()
	{
		Close();
	}


	public void Find()
	{
		if (string.IsNullOrEmpty(AtlasName))
		{
			return;
		}

		var objs = Selection.objects;

		List<string> strList = new List<string>();
		int i = 0;
		foreach (var obj in objs)
		{
			EditorUtility.DisplayProgressBar(AtlasName, obj.name, i * 1.0f / objs.Length);

			if (IsAssetAFolder(obj))
			{
				var path = AssetDatabase.GetAssetPath(obj.GetInstanceID());
				var pathList = new List<string>();
				GetPath(path, pathList, "*.prefab");
				foreach (var p in pathList)
				{
					strList.AddRange(FindInAsset(p));
				}
			}
			else if (obj as GameObject)
			{
				strList.AddRange(FindInAsset(obj as GameObject));
			}
			i++;
		}

		EditorUtility.ClearProgressBar();
		foreach (var str in strList)
		{
			Debug.Log(str);
		}
		Debug.Log("Using [" + AtlasName + "] Total=" + strList.Count.ToString() + "------------------------------------------end");
	}

	public List<string> FindInAsset(string path)
	{
		var obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
		if (null != obj)
		{
			return FindInAsset(obj);
		}
		return new List<string>();
	}

	public List<string> FindInAsset(GameObject asset)
	{
		var str = new List<string>();

		var go = GameObject.Instantiate(asset) as GameObject;
		go.name = asset.name;
		go.SetActiveRecursively(true);
		var uis = go.transform.GetComponentsInChildren<UISprite>();
		foreach (var ui in uis)
		{
			if (null != ui.atlas && ui.atlas.name.Contains(AtlasName))
			{
				str.Add(ui.transform.FullPath());
			}
		}

		GameObject.DestroyImmediate(go);

		return str;
	}

	private static bool IsAssetAFolder(Object obj)
	{
		string path = "";

		if (obj == null)
		{
			return false;
		}

		path = AssetDatabase.GetAssetPath(obj.GetInstanceID());

		if (path.Length > 0)
		{
			if (Directory.Exists(path))
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		return false;
	}

	void GetPath(string path, List<string> pathList, string filter = "*")
	{

		if (path != null)
		{
			string[] f1 = Directory.GetFiles(path, filter); ;
			string[] d1;

			foreach (string f11 in f1)
			{
				pathList.Add(f11);
			}
			try
			{
				d1 = Directory.GetDirectories(path);
				foreach (string d11 in d1)
				{
					try
					{
						GetPath(d11, pathList, filter);
					}
					catch (System.Exception) { }
				}
			}
			catch (System.Exception) { }
		}
	}
}

  

 

Unity3d 检查哪些prefab引用了某个UIAtlas

标签:

原文地址:http://www.cnblogs.com/mrblue/p/4944765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!