CheckBundleRedundancy

From Unify Community Wiki
Jump to: navigation, search

Contents

CheckBundleRedundancy

Author: Bob Zhang

Description

You drag a file or folder from the Project View to the CheckBundleRedundancy EditorWindow, and click on the "check" button, and you will find the details about what has been included in the bundles.

Usage

You must place the script in a folder named Editor in your project's Assets folder for it to work properly.

c# - CheckBundleRedundancy.cs

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
 
/// <summary>
/// used to store information about objects included in bundle
/// </summary>
public struct ObjInBundle
{
   public int ID;
   public string Name;
   public System.Type Type;
   public string Url;
   public ObjInBundle(int id, string name, System.Type type, string url)
   {
       ID = id;
       Name = name;
       Type = type;
       Url = url;
   }
   public override string ToString()
   {
       string str = ID.ToString() + " " + Name + " " + Type.ToString();
       return str;
   }
   //Sort methods
   public static int compareByName(ObjInBundle a, ObjInBundle b)
   {
       return string.Compare(a.Name, b.Name);
   }
   public static int compareByID(ObjInBundle a, ObjInBundle b)
   {
       if (a.ID > b.ID)
       {
           return 1;
       }
       else if (a.ID < b.ID)
       {
           return -1;
       }
       else
       {
           return 0;
       }
   }
   public static int compareByType(ObjInBundle a, ObjInBundle b)
   {
       return string.Compare(a.Type.ToString(), b.Type.ToString());
   }
   public static int compareByUrl(ObjInBundle a, ObjInBundle b)
   {
       return string.Compare(a.Url,b.Url);
   }
}
public class CheckBundleRedundancy : EditorWindow {
 
   private List<ObjInBundle> bundleInfos = new List<ObjInBundle>();
 
   private Object PathObject;
 
   private static string basepath = Application.dataPath.Substring(0, Application.dataPath.IndexOf("/Assets"));
 
   [MenuItem("Window/CheckBundleRedundancy")]
   static void Init()
   {
       CheckBundleRedundancy window = (CheckBundleRedundancy)EditorWindow.GetWindow<CheckBundleRedundancy>();
   }
 
   private List<string> bundleList = new List<string>();
 
   private string filterStr = "";
 
   private string[] filterStrList;
 
   private int selectedIndex = 0;
 
   private int lastSel = 0;
 
   private string[] compareLabels = { "Name", "ID", "Type", "Url" };        
 
   void OnGUI()
   {
       PathObject = EditorGUILayout.ObjectField(PathObject, typeof(Object));
       filterStr = EditorGUILayout.TextField("filterByType",filterStr);
       GUILayout.BeginHorizontal();
       if (GUILayout.Button("check"))
       {
           ClearData();
           bundleList = getBundleFileList();
       }
       if (GUILayout.Button("filter"))
       {
           setFilter();
 
       }
       if (GUILayout.Button("clear"))
       {
           ClearData();
       }
       GUILayout.EndHorizontal();
       selectedIndex = GUILayout.Toolbar(selectedIndex, compareLabels);
       if (lastSel != selectedIndex)
       {
           lastSel = selectedIndex;
           changeSort(selectedIndex);
       }
       if (bundleList != null)
       {
           loadAllBundles(bundleList);
       }
       ShowInfos();
 
   }
 
   Vector2 scrollPos;
   private void ShowInfos()
   {
       scrollPos = GUILayout.BeginScrollView(scrollPos);
       for (int i = 0; i < bundleInfos.Count; i++)
       {
           if (!passFilter(bundleInfos[i].Type.ToString()))
           {
               continue;
           }
           GUILayout.BeginHorizontal();
           GUILayout.Label(bundleInfos[i].Name, GUILayout.Width(400));
           GUILayout.Label(bundleInfos[i].ID.ToString(), GUILayout.Width(100));
           GUILayout.Label(bundleInfos[i].Type.ToString(), GUILayout.Width(200));
           GUILayout.Label(bundleInfos[i].Url, GUILayout.Width(Screen.width - 700));
           GUILayout.EndHorizontal();
       }
       GUILayout.EndScrollView();
   }
   /// <summary>
   /// set keywords for filter, supporting multiple filters using ';'
   /// </summary>
   private void setFilter()
   {
       filterStrList = filterStr.Split(';');
       if (filterStrList == null)
       {
           filterStrList = new string[0];
       }
       for (int i = 0; i < filterStrList.Length; i++)
       {
           filterStrList[i] = filterStrList[i].ToLower();
       }
   }
 
   bool passFilter(string type)
   {
       type = type.ToLower();
       if (filterStrList == null) 
       {
           return true;
       }
       if (filterStrList.Length == 0)
       {
           return true;
       }
       for (int i = 0; i < filterStrList.Length; i++)
       {
           if (type.Contains(filterStrList[i]))
           {
               return true;
           }
       }
       return false;
   }
 
   private void ClearData()
   {
       wwwBuffers.Clear();
       bundleList.Clear();
       bundleInfos.Clear();
   }
 
   Dictionary<string,WWW> wwwBuffers = new Dictionary<string,WWW>();
 
   void loadAllBundles(List<string> allBundleUrls)
   {
       for (int i = bundleList.Count - 1; i >= 0 ; i--)
       {
           WWW www = null;
           if (wwwBuffers.ContainsKey(bundleList[i]))
           {
               www = wwwBuffers[bundleList[i]];
           }
           else
           {
               www = new WWW("File:///" + bundleList[i]);
               wwwBuffers.Add(bundleList[i], www);
           }
           if (www.isDone)
           {
               if (null != www.assetBundle)
               {
                   Object[] bundles = www.assetBundle.LoadAll();
                   Debug.Log(bundles.Length);
                   for (int j = 0; j < bundles.Length; j++)
                   {
                       ObjInBundle bi = new ObjInBundle(bundles[j].GetInstanceID(), bundles[j].name, bundles[j].GetType(), allBundleUrls[i]);
                       bundleInfos.Add(bi);
                   }
 
                   bundles = null;
                   www.assetBundle.Unload(true);
               }
               bundleList.RemoveAt(i);
           }
       }
   }
 
   List<string> getBundleFileList()
   {
       string path = basepath + "/" + AssetDatabase.GetAssetPath(PathObject);
 
       FileAttributes attr = File.GetAttributes(path);
       if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
       {
           string[] bundleList = Directory.GetFiles(path, "*.data", SearchOption.AllDirectories);
           return new List<string>(bundleList);
       }
       else
       {
           if (Path.GetExtension(path) == ".data")
           {
               List<string> ret = new List<string>();
               ret.Add(path);
               return ret;
           }
           else
           {
               return new List<string>();
           }            
       }        
   }
 
 
   private void changeSort(int selectedIndex)
   {
       switch (selectedIndex)
       {
           case 0:
           //default:
               bundleInfos.Sort(ObjInBundle.compareByName);
               break;
           case 1:
               bundleInfos.Sort(ObjInBundle.compareByID);
               break;
           case 2:
               bundleInfos.Sort(ObjInBundle.compareByType);
               break;
           case 3:
               bundleInfos.Sort(ObjInBundle.compareByUrl);
               break;
       }
   }
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox