12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using UnityEditor;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace vietlabs.fr2
- {
- public class FR2_SceneRef : FR2_Ref
- {
- internal static Dictionary<string, Type> CacheType = new Dictionary<string, Type>();
-
- // ------------------------- Ref in scene
- private static Action<Dictionary<string, FR2_Ref>> onFindRefInSceneComplete;
- private static Dictionary<string, FR2_Ref> refs = new Dictionary<string, FR2_Ref>();
- private static string[] cacheAssetGuids;
- public string sceneFullPath = "";
- public string scenePath = "";
- public string targetType;
- public HashSet<string> usingType = new HashSet<string>();
- public FR2_SceneRef(int index, int depth, FR2_Asset asset, FR2_Asset by) : base(index, depth, asset, by)
- {
- isSceneRef = false;
- }
- // public override string ToString()
- // {
- // return "SceneRef: " + sceneFullPath;
- // }
- public FR2_SceneRef(int depth, Object target) : base(0, depth, null, null)
- {
- component = target;
- this.depth = depth;
- isSceneRef = true;
- var obj = target as GameObject;
- if (obj == null)
- {
- var com = target as Component;
- if (com != null)
- {
- obj = com.gameObject;
- }
- }
- scenePath = FR2_Unity.GetGameObjectPath(obj, false);
- if (component == null)
- {
- return;
- }
- sceneFullPath = scenePath + component.name;
- targetType = component.GetType().Name;
- }
- public static IWindow window { get; set; }
- public override bool isSelected()
- {
- return component == null ? false : FR2_Bookmark.Contains(component);
- }
- public void Draw(Rect r, IWindow window, bool showDetails)
- {
- bool selected = isSelected();
- DrawToogleSelect(r);
-
- var margin = 2;
- var left = new Rect(r);
- left.width = r.width / 3f;
-
- var right = new Rect(r);
- right.xMin += left.width + margin;
-
- //Debug.Log("draw scene "+ selected);
- if ( /* FR2_Setting.PingRow && */ Event.current.type == EventType.MouseDown && Event.current.button == 0)
- {
- Rect pingRect = FR2_Setting.PingRow
- ? new Rect(0, r.y, r.x + r.width, r.height)
- : left;
- if (pingRect.Contains(Event.current.mousePosition))
- {
- if (Event.current.control || Event.current.command)
- {
- if (selected)
- {
- FR2_Bookmark.Remove(this);
- }
- else
- {
- FR2_Bookmark.Add(this);
- }
- if (window != null) window.Repaint();
- } else {
- EditorGUIUtility.PingObject(component);
- }
- Event.current.Use();
- }
- }
-
- EditorGUI.ObjectField(showDetails ? left : r, GUIContent.none, component, typeof(GameObject), true);
- if (!showDetails) return;
- bool drawPath = FR2_Setting.GroupMode != FR2_RefDrawer.Mode.Folder;
- float pathW = drawPath ? EditorStyles.miniLabel.CalcSize(new GUIContent(scenePath)).x : 0;
- string assetName = component.name;
- // if(usingType!= null && usingType.Count > 0)
- // {
- // assetName += " -> ";
- // foreach(var item in usingType)
- // {
- // assetName += item + " - ";
- // }
- // assetName = assetName.Substring(0, assetName.Length - 3);
- // }
- Color cc = FR2_Cache.Api.setting.SelectedColor;
- var lableRect = new Rect(
- right.x,
- right.y,
- pathW + EditorStyles.boldLabel.CalcSize(new GUIContent(assetName)).x,
- right.height);
- if (selected)
- {
- Color c = GUI.color;
- GUI.color = cc;
- GUI.DrawTexture(lableRect, EditorGUIUtility.whiteTexture);
- GUI.color = c;
- }
- if (drawPath)
- {
- GUI.Label(LeftRect(pathW, ref right), scenePath, EditorStyles.miniLabel);
- right.xMin -= 4f;
- GUI.Label(right, assetName, EditorStyles.boldLabel);
- }
- else
- {
- GUI.Label(right, assetName);
- }
- if (!FR2_Setting.ShowUsedByClassed || usingType == null)
- {
- return;
- }
- float sub = 10;
- var re = new Rect(r.x + r.width - sub, r.y, 20, r.height);
- Type t = null;
- foreach (string item in usingType)
- {
- string name = item;
- if (!CacheType.TryGetValue(item, out t))
- {
- t = FR2_Unity.GetType(name);
- // if (t == null)
- // {
- // continue;
- // }
- CacheType.Add(item, t);
- }
- GUIContent content;
- var width = 0.0f;
- if (!FR2_Asset.cacheImage.TryGetValue(name, out content))
- {
- if (t == null)
- {
- content = new GUIContent(name);
- }
- else
- {
- Texture text = EditorGUIUtility.ObjectContent(null, t).image;
- if (text == null)
- {
- content = new GUIContent(name);
- }
- else
- {
- content = new GUIContent(text, name);
- }
- }
- FR2_Asset.cacheImage.Add(name, content);
- }
- if (content.image == null)
- {
- width = EditorStyles.label.CalcSize(content).x;
- }
- else
- {
- width = 20;
- }
- re.x -= width;
- re.width = width;
- GUI.Label(re, content);
- re.x -= margin; // margin;
- }
-
- // var nameW = EditorStyles.boldLabel.CalcSize(new GUIContent(assetName)).x;
- }
- private Rect LeftRect(float w, ref Rect rect)
- {
- rect.x += w;
- rect.width -= w;
- return new Rect(rect.x - w, rect.y, w, rect.height);
- }
- // ------------------------- Scene use scene objects
- public static Dictionary<string, FR2_Ref> FindSceneUseSceneObjects(GameObject[] targets)
- {
- var results = new Dictionary<string, FR2_Ref>();
- GameObject[] objs = Selection.gameObjects;
- for (var i = 0; i < objs.Length; i++)
- {
- if (FR2_Unity.IsInAsset(objs[i]))
- {
- continue;
- }
- string key = objs[i].GetInstanceID().ToString();
- if (!results.ContainsKey(key))
- {
- results.Add(key, new FR2_SceneRef(0, objs[i]));
- }
- Component[] coms = objs[i].GetComponents<Component>();
- Dictionary<Component, HashSet<FR2_SceneCache.HashValue>> SceneCache = FR2_SceneCache.Api.cache;
- for (var j = 0; j < coms.Length; j++)
- {
- HashSet<FR2_SceneCache.HashValue> hash = null;
- if (coms[j] == null) continue; // missing component
-
- if (SceneCache.TryGetValue(coms[j], out hash))
- {
- foreach (FR2_SceneCache.HashValue item in hash)
- {
- if (item.isSceneObject)
- {
- Object obj = item.target;
- string key1 = obj.GetInstanceID().ToString();
- if (!results.ContainsKey(key1))
- {
- results.Add(key1, new FR2_SceneRef(1, obj));
- }
- }
- }
- }
- }
- }
- return results;
- }
- // ------------------------- Scene in scene
- public static Dictionary<string, FR2_Ref> FindSceneInScene(GameObject[] targets)
- {
- var results = new Dictionary<string, FR2_Ref>();
- GameObject[] objs = Selection.gameObjects;
- for (var i = 0; i < objs.Length; i++)
- {
- if (FR2_Unity.IsInAsset(objs[i]))
- {
- continue;
- }
- string key = objs[i].GetInstanceID().ToString();
- if (!results.ContainsKey(key))
- {
- results.Add(key, new FR2_SceneRef(0, objs[i]));
- }
- foreach (KeyValuePair<Component, HashSet<FR2_SceneCache.HashValue>> item in FR2_SceneCache.Api.cache)
- foreach (FR2_SceneCache.HashValue item1 in item.Value)
- {
- // if(item.Key.gameObject.name == "ScenesManager")
- // Debug.Log(item1.objectReferenceValue);
- GameObject ob = null;
- if (item1.target is GameObject)
- {
- ob = item1.target as GameObject;
- }
- else
- {
- var com = item1.target as Component;
- if (com == null)
- {
- continue;
- }
- ob = com.gameObject;
- }
- if (ob == null)
- {
- continue;
- }
- if (ob != objs[i])
- {
- continue;
- }
- key = item.Key.GetInstanceID().ToString();
- if (!results.ContainsKey(key))
- {
- results.Add(key, new FR2_SceneRef(1, item.Key));
- }
- (results[key] as FR2_SceneRef).usingType.Add(item1.target.GetType().FullName);
- }
- }
- return results;
- }
- public static Dictionary<string, FR2_Ref> FindRefInScene(string[] assetGUIDs, bool depth,
- Action<Dictionary<string, FR2_Ref>> onComplete, IWindow win)
- {
- // var watch = new System.Diagnostics.Stopwatch();
- // watch.Start();
- window = win;
- cacheAssetGuids = assetGUIDs;
- onFindRefInSceneComplete = onComplete;
- if (FR2_SceneCache.ready)
- {
- FindRefInScene();
- }
- else
- {
- FR2_SceneCache.onReady -= FindRefInScene;
- FR2_SceneCache.onReady += FindRefInScene;
- }
- return refs;
- }
- private static void FindRefInScene()
- {
- refs = new Dictionary<string, FR2_Ref>();
- for (var i = 0; i < cacheAssetGuids.Length; i++)
- {
- FR2_Asset asset = FR2_Cache.Api.Get(cacheAssetGuids[i]);
- if (asset == null)
- {
- continue;
- }
- Add(refs, asset, 0);
- ApplyFilter(refs, asset);
- }
- if (onFindRefInSceneComplete != null)
- {
- onFindRefInSceneComplete(refs);
- }
- FR2_SceneCache.onReady -= FindRefInScene;
- // UnityEngine.Debug.Log("Time find ref in scene " + watch.ElapsedMilliseconds);
- }
- private static void FilterAll(Dictionary<string, FR2_Ref> refs, Object obj, string targetPath)
- {
- // ApplyFilter(refs, obj, targetPath);
- }
- private static void ApplyFilter(Dictionary<string, FR2_Ref> refs, FR2_Asset asset)
- {
- string targetPath = AssetDatabase.GUIDToAssetPath(asset.guid);
- if (string.IsNullOrEmpty(targetPath))
- {
- return; // asset not found - might be deleted!
- }
- //asset being moved!
- if (targetPath != asset.assetPath)
- {
- asset.MarkAsDirty(true, false);
- }
-
- Object target = AssetDatabase.LoadAssetAtPath(targetPath, typeof(Object));
- if (target == null)
- {
- //Debug.LogWarning("target is null");
- return;
- }
- bool targetIsGameobject = target is GameObject;
- if (targetIsGameobject)
- {
- foreach (GameObject item in FR2_Unity.getAllObjsInCurScene())
- {
- if (FR2_Unity.CheckIsPrefab(item))
- {
- string itemGUID = FR2_Unity.GetPrefabParent(item);
- // Debug.Log(item.name + " itemGUID: " + itemGUID);
- // Debug.Log(target.name + " asset.guid: " + asset.guid);
- if (itemGUID == asset.guid)
- {
- Add(refs, item, 1);
- }
- }
- }
- }
- string dir = Path.GetDirectoryName(targetPath);
- if (FR2_SceneCache.Api.folderCache.ContainsKey(dir))
- {
- foreach (Component item in FR2_SceneCache.Api.folderCache[dir])
- {
- if (FR2_SceneCache.Api.cache.ContainsKey(item))
- {
- foreach (FR2_SceneCache.HashValue item1 in FR2_SceneCache.Api.cache[item])
- {
- if (targetPath == AssetDatabase.GetAssetPath(item1.target))
- {
- Add(refs, item, 1);
- }
- }
- }
- }
- }
- }
- private static void Add(Dictionary<string, FR2_Ref> refs, FR2_Asset asset, int depth)
- {
- string targetId = asset.guid;
- if (!refs.ContainsKey(targetId))
- {
- refs.Add(targetId, new FR2_Ref(0, depth, asset, null));
- }
- }
- private static void Add(Dictionary<string, FR2_Ref> refs, Object target, int depth)
- {
- string targetId = target.GetInstanceID().ToString();
- if (!refs.ContainsKey(targetId))
- {
- refs.Add(targetId, new FR2_SceneRef(depth, target));
- }
- }
- }
- public class FR2_Ref
- {
- static int CSVSorter(FR2_Ref item1, FR2_Ref item2)
- {
- var r = item1.depth.CompareTo(item2.depth);
- if (r != 0) return r;
-
- var t = item1.type.CompareTo(item2.type);
- if (t != 0) return t;
-
- return item1.index.CompareTo(item2.index);
- }
-
-
- public static FR2_Ref[] FromDict(Dictionary<string, FR2_Ref> dict)
- {
- if (dict == null || dict.Count == 0) return null;
- var result = new List<FR2_Ref>();
-
- foreach (var kvp in dict)
- {
- if (kvp.Value == null) continue;
- if (kvp.Value.asset == null) continue;
-
- result.Add(kvp.Value);
- }
-
- result.Sort(CSVSorter);
-
-
- return result.ToArray();
- }
- public static FR2_Ref[] FromList(List<FR2_Ref> list)
- {
- if (list == null || list.Count == 0) return null;
-
- list.Sort(CSVSorter);
- var result = new List<FR2_Ref>();
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].asset == null) continue;
- result.Add(list[i]);
- }
- return result.ToArray();
- }
-
- public FR2_Asset addBy;
- public FR2_Asset asset;
- public Object component;
- public int depth;
- public string group;
- public int index;
- public bool isSceneRef;
- public int matchingScore;
- public int type;
- public override string ToString()
- {
- if (isSceneRef)
- {
- var sr = (FR2_SceneRef) this;
- return sr.scenePath;
- }
-
- return asset.assetPath;
- }
- public FR2_Ref(int index, int depth, FR2_Asset asset, FR2_Asset by)
- {
- this.index = index;
- this.depth = depth;
- this.asset = asset;
- if (asset != null)
- {
- type = AssetType.GetIndex(asset.extension);
- }
- addBy = by;
- // isSceneRef = false;
- }
- public FR2_Ref(int index, int depth, FR2_Asset asset, FR2_Asset by, string group) : this(index, depth, asset,
- by)
- {
- this.group = group;
- // isSceneRef = false;
- }
- public string GetSceneObjId()
- {
- if (component == null)
- {
- return string.Empty;
- }
- return component.GetInstanceID().ToString();
- }
- public virtual bool isSelected()
- {
- return FR2_Bookmark.Contains(asset.guid);
- }
- public virtual void DrawToogleSelect(Rect r)
- {
- var s = isSelected();
- r.width = 16f;
-
- if (!GUI2.Toggle(r, ref s)) return;
-
- if (s)
- {
- FR2_Bookmark.Add(this);
- }
- else
- {
- FR2_Bookmark.Remove(this);
- }
- }
- // public FR2_Ref(int depth, UnityEngine.Object target)
- // {
- // this.component = target;
- // this.depth = depth;
- // // isSceneRef = true;
- // }
- internal List<FR2_Ref> Append(Dictionary<string, FR2_Ref> dict, params string[] guidList)
- {
- var result = new List<FR2_Ref>();
- if (FR2_Cache.Api.disabled)
- {
- return result;
- }
- if (!FR2_Cache.isReady)
- {
- Debug.LogWarning("Cache not yet ready! Please wait!");
- return result;
- }
- //filter to remove items that already in dictionary
- for (var i = 0; i < guidList.Length; i++)
- {
- string guid = guidList[i];
- if (dict.ContainsKey(guid))
- {
- continue;
- }
- FR2_Asset child = FR2_Cache.Api.Get(guid);
- if (child == null)
- {
- continue;
- }
- var r = new FR2_Ref(dict.Count, depth + 1, child, asset);
- if (!asset.IsFolder)
- {
- dict.Add(guid, r);
- }
- result.Add(r);
- }
- return result;
- }
- internal void AppendUsedBy(Dictionary<string, FR2_Ref> result, bool deep)
- {
- // var list = Append(result, FR2_Asset.FindUsedByGUIDs(asset).ToArray());
- // if (!deep) return;
- // // Add next-level
- // for (var i = 0;i < list.Count;i ++)
- // {
- // list[i].AppendUsedBy(result, true);
- // }
- Dictionary<string, FR2_Asset> h = asset.UsedByMap;
- List<FR2_Ref> list = deep ? new List<FR2_Ref>() : null;
- if (asset.UsedByMap == null) return;
-
- foreach (KeyValuePair<string, FR2_Asset> kvp in h)
- {
- string guid = kvp.Key;
- if (result.ContainsKey(guid))
- {
- continue;
- }
- FR2_Asset child = FR2_Cache.Api.Get(guid);
- if (child == null)
- {
- continue;
- }
- if (child.IsMissing)
- {
- continue;
- }
- var r = new FR2_Ref(result.Count, depth + 1, child, asset);
- if (!asset.IsFolder)
- {
- result.Add(guid, r);
- }
- if (deep)
- {
- list.Add(r);
- }
- }
- if (!deep)
- {
- return;
- }
- foreach (FR2_Ref item in list)
- {
- item.AppendUsedBy(result, true);
- }
- }
- internal void AppendUsage(Dictionary<string, FR2_Ref> result, bool deep)
- {
- Dictionary<string, HashSet<int>> h = asset.UseGUIDs;
- List<FR2_Ref> list = deep ? new List<FR2_Ref>() : null;
- foreach (KeyValuePair<string, HashSet<int>> kvp in h)
- {
- string guid = kvp.Key;
- if (result.ContainsKey(guid))
- {
- continue;
- }
- FR2_Asset child = FR2_Cache.Api.Get(guid);
- if (child == null)
- {
- continue;
- }
- if (child.IsMissing)
- {
- continue;
- }
- var r = new FR2_Ref(result.Count, depth + 1, child, asset);
- if (!asset.IsFolder)
- {
- result.Add(guid, r);
- }
- if (deep)
- {
- list.Add(r);
- }
- }
- if (!deep)
- {
- return;
- }
- foreach (FR2_Ref item in list)
- {
- item.AppendUsage(result, true);
- }
- }
- // --------------------- STATIC UTILS -----------------------
- internal static Dictionary<string, FR2_Ref> FindRefs(string[] guids, bool usageOrUsedBy, bool addFolder)
- {
- var dict = new Dictionary<string, FR2_Ref>();
- var list = new List<FR2_Ref>();
- for (var i = 0; i < guids.Length; i++)
- {
- string guid = guids[i];
- if (dict.ContainsKey(guid))
- {
- continue;
- }
- FR2_Asset asset = FR2_Cache.Api.Get(guid);
- if (asset == null)
- {
- continue;
- }
- var r = new FR2_Ref(i, 0, asset, null);
- if (!asset.IsFolder || addFolder)
- {
- dict.Add(guid, r);
- }
- list.Add(r);
- }
- for (var i = 0; i < list.Count; i++)
- {
- if (usageOrUsedBy)
- {
- list[i].AppendUsage(dict, true);
- }
- else
- {
- list[i].AppendUsedBy(dict, true);
- }
- }
- //var result = dict.Values.ToList();
- //result.Sort((item1, item2)=>{
- // return item1.index.CompareTo(item2.index);
- //});
- return dict;
- }
- public static Dictionary<string, FR2_Ref> FindUsage(string[] guids)
- {
- return FindRefs(guids, true, true);
- }
- public static Dictionary<string, FR2_Ref> FindUsedBy(string[] guids)
- {
- return FindRefs(guids, false, true);
- }
- public static Dictionary<string, FR2_Ref> FindUsageScene(GameObject[] objs, bool depth)
- {
- var dict = new Dictionary<string, FR2_Ref>();
- // var list = new List<FR2_Ref>();
- for (var i = 0; i < objs.Length; i++)
- {
- if (FR2_Unity.IsInAsset(objs[i]))
- {
- continue; //only get in scene
- }
- //add selection
- if (!dict.ContainsKey(objs[i].GetInstanceID().ToString()))
- {
- dict.Add(objs[i].GetInstanceID().ToString(), new FR2_SceneRef(0, objs[i]));
- }
- foreach (Object item in FR2_Unity.GetAllRefObjects(objs[i]))
- {
- AppendUsageScene(dict, item);
- }
- if (depth)
- {
- foreach (GameObject child in FR2_Unity.getAllChild(objs[i]))
- foreach (Object item2 in FR2_Unity.GetAllRefObjects(child))
- {
- AppendUsageScene(dict, item2);
- }
- }
- }
- return dict;
- }
- private static void AppendUsageScene(Dictionary<string, FR2_Ref> dict, Object obj)
- {
- string path = AssetDatabase.GetAssetPath(obj);
- if (string.IsNullOrEmpty(path))
- {
- return;
- }
- string guid = AssetDatabase.AssetPathToGUID(path);
- if (string.IsNullOrEmpty(guid))
- {
- return;
- }
- if (dict.ContainsKey(guid))
- {
- return;
- }
- FR2_Asset asset = FR2_Cache.Api.Get(guid);
- if (asset == null)
- {
- return;
- }
- var r = new FR2_Ref(0, 1, asset, null);
- dict.Add(guid, r);
- }
- }
- public class FR2_RefDrawer : IRefDraw
- {
- public enum Mode
- {
- Dependency,
- Depth,
- Type,
- Extension,
- Folder,
- Atlas,
- AssetBundle,
-
- None
- }
- public enum Sort
- {
- Type,
- Path,
- Size
- }
- public static GUIStyle toolbarSearchField;
- public static GUIStyle toolbarSearchFieldCancelButton;
- public static GUIStyle toolbarSearchFieldCancelButtonEmpty;
- internal readonly FR2_TreeUI2.GroupDrawer groupDrawer;
- private readonly bool showSearch = true;
- public bool caseSensitive = false;
- // STATUS
- private bool dirty;
- private int excludeCount;
-
- public string level0Group;
- public bool forceHideDetails;
-
- public string Lable;
- internal List<FR2_Ref> list;
- internal Dictionary<string, FR2_Ref> refs;
- // FILTERING
- private string searchTerm = string.Empty;
- private bool selectFilter;
- private bool showIgnore;
-
-
- // ORIGINAL
- internal FR2_Ref[] source
- {
- get { return FR2_Ref.FromList(list); }
- }
-
- public FR2_RefDrawer(IWindow window)
- {
- this.window = window;
- groupDrawer = new FR2_TreeUI2.GroupDrawer(DrawGroup, DrawAsset);
- }
- public string messageNoRefs = "Do select something!";
- public string messageEmpty = "It's empty!";
- public IWindow window { get; set; }
- void DrawEmpty(Rect rect, string text)
- {
- rect = GUI2.Padding(rect, 2f, 2f);
- rect.height = 40f;
-
- EditorGUI.HelpBox(rect, text, MessageType.Info);
- }
- public bool Draw(Rect rect)
- {
- if (refs == null || refs.Count == 0)
- {
- DrawEmpty(rect, messageNoRefs);
- return false;
- }
-
- if (dirty || list == null)
- {
- ApplyFilter();
- }
- if (!groupDrawer.hasChildren)
- {
- DrawEmpty(rect, messageEmpty);
- }
- else
- {
- groupDrawer.Draw(rect);
- }
- return false;
- }
-
- public bool DrawLayout()
- {
- if (refs == null || refs.Count == 0)
- {
- return false;
- }
- if (dirty || list == null)
- {
- ApplyFilter();
- }
-
- groupDrawer.DrawLayout();
- return false;
- }
- public int ElementCount()
- {
- if (refs == null)
- {
- return 0;
- }
- return refs.Count;
- // return refs.Where(x => x.Value.depth != 0).Count();
- }
- public void SetRefs(Dictionary<string, FR2_Ref> dictRefs)
- {
- refs = dictRefs;
- dirty = true;
- }
- void SetBookmarkGroup(string groupLabel, bool willbookmark)
- {
- string[] ids = groupDrawer.GetChildren(groupLabel);
- var info = GetBMInfo(groupLabel);
-
- for (var i = 0; i < ids.Length; i++)
- {
- FR2_Ref rf;
- if (!refs.TryGetValue(ids[i], out rf))
- {
- continue;
- }
-
- if (willbookmark)
- {
- FR2_Bookmark.Add(rf);
- }
- else
- {
- FR2_Bookmark.Remove(rf);
- }
- }
-
- info.count = willbookmark ? info.total : 0;
- }
- internal class BookmarkInfo
- {
- public int count;
- public int total;
- }
- private Dictionary<string, BookmarkInfo> gBookmarkCache = new Dictionary<string, BookmarkInfo>();
- BookmarkInfo GetBMInfo(string groupLabel)
- {
- BookmarkInfo info = null;
- if (!gBookmarkCache.TryGetValue(groupLabel, out info))
- {
- string[] ids = groupDrawer.GetChildren(groupLabel);
-
- info = new BookmarkInfo();
- for (var i = 0; i < ids.Length; i++)
- {
- FR2_Ref rf;
- if (!refs.TryGetValue(ids[i], out rf))
- {
- continue;
- }
- info.total++;
-
- var isBM = FR2_Bookmark.Contains(rf);
- if (isBM) info.count++;
- }
- gBookmarkCache.Add(groupLabel, info);
- }
- return info;
- }
-
- void DrawToggleGroup(Rect r, string groupLabel)
- {
- var info = GetBMInfo(groupLabel);
- var selectAll = info.count == info.total;
- r.width = 16f;
- if (GUI2.Toggle(r, ref selectAll))
- {
- SetBookmarkGroup(groupLabel, selectAll);
- }
- if (!selectAll && info.count > 0)
- {
- //GUI.DrawTexture(r, EditorStyles.
- }
- }
-
- private void DrawGroup(Rect r, string label, int childCount)
- {
- if (FR2_Setting.GroupMode == Mode.Folder)
- {
- Texture tex = AssetDatabase.GetCachedIcon("Assets");
- GUI.DrawTexture(new Rect(r.x - 2f, r.y - 2f, 16f, 16f), tex);
- r.xMin += 16f;
- }
- DrawToggleGroup(r, label);
- r.xMin += 18f;
- GUI.Label(r, label + " (" + childCount + ")", EditorStyles.boldLabel);
- bool hasMouse = Event.current.type == EventType.MouseUp && r.Contains(Event.current.mousePosition);
- if (hasMouse && Event.current.button == 1)
- {
- var menu = new GenericMenu();
- menu.AddItem(new GUIContent("Add Bookmark"), false, () => { SetBookmarkGroup(label, true); });
- menu.AddItem(new GUIContent("Remove Bookmark"), false, () =>
- {
- SetBookmarkGroup(label, false);
- });
- menu.ShowAsContext();
- Event.current.Use();
- }
- }
-
- private void DrawAsset(Rect r, string guid)
- {
- FR2_Ref rf;
- if (!refs.TryGetValue(guid, out rf))
- {
- return;
- }
- if (rf.isSceneRef)
- {
- if (rf.component == null)
- {
- return;
- }
- var re = rf as FR2_SceneRef;
- if (re != null)
- {
- r.x -= 16f;
- rf.DrawToogleSelect(r);
- r.xMin += 32f;
- re.Draw(r, window, !forceHideDetails);
- }
- }
- else
- {
- r.xMin -= 16f;
- rf.DrawToogleSelect(r);
- r.xMin += 32f;
- rf.asset.Draw (r,
- rf.depth == 1,
- !forceHideDetails && (FR2_Setting.GroupMode != Mode.Folder),
- !forceHideDetails && FR2_Setting.s.displayFileSize,
- !forceHideDetails && FR2_Setting.s.displayAssetBundleName,
- !forceHideDetails && FR2_Setting.s.displayAtlasName,
- !forceHideDetails && FR2_Setting.s.showUsedByClassed,
- window
- );
- }
- }
-
- private string GetGroup(FR2_Ref rf)
- {
- if (rf.depth == 0)
- {
- return level0Group;
- }
-
- if (FR2_Setting.GroupMode == Mode.None)
- {
- return "(no group)";
- }
-
- FR2_SceneRef sr = null;
- if (rf.isSceneRef)
- {
- sr = rf as FR2_SceneRef;
- if (sr == null) return null;
- }
- if (!rf.isSceneRef)
- {
- if (rf.asset.IsExcluded) return null; // "(ignored)"
- }
-
- switch (FR2_Setting.GroupMode)
- {
- case Mode.Extension: return rf.isSceneRef ? sr.targetType : rf.asset.extension;
- case Mode.Type:
- {
- return rf.isSceneRef ? sr.targetType : AssetType.FILTERS[rf.type].name;
- }
-
- case Mode.Folder: return rf.isSceneRef ? sr.scenePath : rf.asset.assetFolder;
- case Mode.Dependency:
- {
- return rf.depth == 1 ? "Direct Usage" : "Indirect Usage";
- }
- case Mode.Depth:
- {
- return "Level " + rf.depth.ToString();
- }
-
- case Mode.Atlas : return rf.isSceneRef ? "(not in atlas)" : (string.IsNullOrEmpty(rf.asset.AtlasName) ? "(not in atlas)" : rf.asset.AtlasName);
- case Mode.AssetBundle : return rf.isSceneRef ? "(not in assetbundle)" : (string.IsNullOrEmpty(rf.asset.AssetBundleName) ? "(not in assetbundle)" : rf.asset.AssetBundleName);
- }
-
- return "(others)";
- }
- private void SortGroup(List<string> groups)
- {
- groups.Sort((item1, item2) =>
- {
- if (item1.Contains("(")) return 1;
- if (item2.Contains("(")) return -1;
- return item1.CompareTo(item2);
- });
- }
- public FR2_RefDrawer Reset(string[] assetGUIDs, bool isUsage)
- {
- //Debug.Log("Reset :: " + assetGUIDs.Length + "\n" + string.Join("\n", assetGUIDs));
- gBookmarkCache.Clear();
-
- if (isUsage)
- {
- refs = FR2_Ref.FindUsage(assetGUIDs);
- }
- else
- {
- refs = FR2_Ref.FindUsedBy(assetGUIDs);
- }
-
- dirty = true;
- if (list != null)
- {
- list.Clear();
- }
- return this;
- }
- public FR2_RefDrawer Reset(GameObject[] objs, bool findDept, bool findPrefabInAsset)
- {
- refs = FR2_Ref.FindUsageScene(objs, findDept);
- var guidss = new List<string>();
- var dependent = FR2_SceneCache.Api.prefabDependencies;
- foreach (var gameObject in objs)
- {
- HashSet<string> hash;
- if (!dependent.TryGetValue(gameObject, out hash))
- {
- continue;
- }
- foreach (string guid in hash)
- {
- guidss.Add(guid);
- }
- }
-
- Dictionary<string, FR2_Ref> usageRefs1 = FR2_Ref.FindUsage(guidss.ToArray());
- foreach (KeyValuePair<string, FR2_Ref> kvp in usageRefs1)
- {
- if (refs.ContainsKey(kvp.Key))
- {
- continue;
- }
- if (guidss.Contains(kvp.Key))
- {
- kvp.Value.depth = 1;
- }
- refs.Add(kvp.Key, kvp.Value);
- }
-
- if (findPrefabInAsset)
- {
- var guids = new List<string>();
- for (var i = 0; i < objs.Length; i++)
- {
- string guid = FR2_Unity.GetPrefabParent(objs[i]);
- if (string.IsNullOrEmpty(guid))
- {
- continue;
- }
- guids.Add(guid);
- }
- Dictionary<string, FR2_Ref> usageRefs = FR2_Ref.FindUsage(guids.ToArray());
- foreach (KeyValuePair<string, FR2_Ref> kvp in usageRefs)
- {
- if (refs.ContainsKey(kvp.Key))
- {
- continue;
- }
- if (guids.Contains(kvp.Key))
- {
- kvp.Value.depth = 1;
- }
- refs.Add(kvp.Key, kvp.Value);
- }
- }
- dirty = true;
- if (list != null)
- {
- list.Clear();
- }
- return this;
- }
- //ref in scene
- public FR2_RefDrawer Reset(string[] assetGUIDs, IWindow window)
- {
- refs = FR2_SceneRef.FindRefInScene(assetGUIDs, true, SetRefInScene, window);
- dirty = true;
- if (list != null)
- {
- list.Clear();
- }
- return this;
- }
- private void SetRefInScene(Dictionary<string, FR2_Ref> data)
- {
- refs = data;
- dirty = true;
- if (list != null)
- {
- list.Clear();
- }
- }
- //scene in scene
- public FR2_RefDrawer ResetSceneInScene(GameObject[] objs)
- {
- refs = FR2_SceneRef.FindSceneInScene(objs);
- dirty = true;
- if (list != null)
- {
- list.Clear();
- }
- return this;
- }
- public FR2_RefDrawer ResetSceneUseSceneObjects(GameObject[] objs)
- {
- refs = FR2_SceneRef.FindSceneUseSceneObjects(objs);
- dirty = true;
- if (list != null)
- {
- list.Clear();
- }
- return this;
- }
- public FR2_RefDrawer ResetUnusedAsset()
- {
- List<FR2_Asset> lst = FR2_Cache.Api.ScanUnused();
- refs = lst.ToDictionary(x => x.guid, x => new FR2_Ref(0, 1, x, null));
- dirty = true;
- if (list != null)
- {
- list.Clear();
- }
- return this;
- }
- public void RefreshSort()
- {
- if (list == null)
- {
- return;
- }
-
- if (list.Count > 0 && list[0].isSceneRef == false && FR2_Setting.SortMode == Sort.Size)
- {
- list = list.OrderByDescending(x => x.asset != null ? x.asset.fileSize : 0).ToList();
- } else
- {
- list.Sort((r1, r2) =>
- {
- var isMixed = r1.isSceneRef ^ r2.isSceneRef;
- if (isMixed)
- {
- #if FR2_DEBUG
- var sb = new StringBuilder();
- sb.Append("r1: " + r1.ToString());
- sb.AppendLine();
- sb.Append("r2: " +r2.ToString());
- Debug.LogWarning("Mixed compared!\n" + sb.ToString());
- #endif
- var v1 = r1.isSceneRef ? 1 : 0;
- var v2 = r2.isSceneRef ? 1 : 0;
- return v2.CompareTo(v1);
- }
-
- if (r1.isSceneRef)
- {
- var rs1 = (FR2_SceneRef)r1;
- var rs2 = (FR2_SceneRef)r2;
-
- return SortAsset(rs1.sceneFullPath, rs2.sceneFullPath,
- rs1.targetType, rs2.targetType,
- FR2_Setting.SortMode == Sort.Path);
- }
- return SortAsset(
- r1.asset.assetPath, r2.asset.assetPath,
- r1.asset.extension, r2.asset.extension,
- false
- );
- });
- }
-
- // clean up list
- int invalidCount = 0;
- for (int i = list.Count-1; i >=0 ; i--)
- {
- var item = list[i];
-
- if (item.isSceneRef)
- {
- if (string.IsNullOrEmpty(item.GetSceneObjId()))
- {
- invalidCount++;
- list.RemoveAt(i);
- }
-
- continue;
- }
- if (item.asset == null)
- {
- invalidCount++;
- list.RemoveAt(i);
- continue;
- }
- }
- #if FR2_DEBUG
- if (invalidCount > 0) Debug.LogWarning("Removed [" + invalidCount + "] invalid assets / objects");
- #endif
-
- groupDrawer.Reset(list,
- rf =>
- {
- if (rf == null) return null;
- if (rf.isSceneRef) return rf.GetSceneObjId();
- return rf.asset == null ? null : rf.asset.guid;
- }, GetGroup, SortGroup);
- }
- public bool isExclueAnyItem()
- {
- return excludeCount > 0;
- }
- private void ApplyFilter()
- {
- dirty = false;
- if (refs == null)
- {
- return;
- }
- if (list == null)
- {
- list = new List<FR2_Ref>();
- }
- else
- {
- list.Clear();
- }
- int minScore = searchTerm.Length;
- string term1 = searchTerm;
- if (!caseSensitive)
- {
- term1 = term1.ToLower();
- }
- string term2 = term1.Replace(" ", string.Empty);
- excludeCount = 0;
- foreach (KeyValuePair<string, FR2_Ref> item in refs)
- {
- FR2_Ref r = item.Value;
- if (FR2_Setting.IsTypeExcluded(r.type))
- {
- excludeCount++;
- continue; //skip this one
- }
- if (!showSearch || string.IsNullOrEmpty(searchTerm))
- {
- r.matchingScore = 0;
- list.Add(r);
- continue;
- }
- //calculate matching score
- string name1 = r.isSceneRef ? (r as FR2_SceneRef).sceneFullPath : r.asset.assetName;
- if (!caseSensitive)
- {
- name1 = name1.ToLower();
- }
- string name2 = name1.Replace(" ", string.Empty);
- int score1 = FR2_Unity.StringMatch(term1, name1);
- int score2 = FR2_Unity.StringMatch(term2, name2);
- r.matchingScore = Mathf.Max(score1, score2);
- if (r.matchingScore > minScore)
- {
- list.Add(r);
- }
- }
- RefreshSort();
- }
- public void SetDirty()
- {
- dirty = true;
- }
- private int SortAsset(string term11, string term12, string term21, string term22, bool swap)
- {
- // if (term11 == null) term11 = string.Empty;
- // if (term12 == null) term12 = string.Empty;
- // if (term21 == null) term21 = string.Empty;
- // if (term22 == null) term22 = string.Empty;
- var v1 = String.Compare(term11, term12, StringComparison.Ordinal);
- var v2 = String.Compare(term21, term22, StringComparison.Ordinal);
- return swap ? (v1 == 0) ? v2 : v1 : (v2 == 0) ? v1 : v2;
- }
-
- public Dictionary<string, FR2_Ref> getRefs()
- {
- return refs;
- }
- }
- }
|