zoukankan      html  css  js  c++  java
  • Unity3d 检查哪些prefab引用了某个UIAtlas

    适用情景:策划在用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) { }
    		}
    	}
    }
    

      

  • 相关阅读:
    算法学习笔记: 珂朵莉树
    算法学习笔记:2SAT
    0x62 图论最小生成树
    Codeforces Round #632 (Div. 2) C. Eugene and an array(尺取法/前缀和)
    BZOJ1912 异象石(LCA/DFS序/set)
    (六1)Firefox插件安装
    (六2)八种定位方式为了查找元素
    (六3)从查找元素到操作元素
    【OSI】网络协议模型
    读《Wireshark网络分析就这么简单》读书笔记
  • 原文地址:https://www.cnblogs.com/mrblue/p/4944765.html
Copyright © 2011-2022 走看看