├── .gitignore ├── 687474703a2f2f6936372e74696e797069632e636f6d2f323369396f68632e6a7067.jpg ├── AssetReferenceFinder.meta ├── AssetReferenceFinder ├── Editor.meta └── Editor │ ├── ReferenceFinderWindow.cs │ ├── ReferenceFinderWindow.cs.meta │ ├── ReferencePair.cs │ ├── ReferencePair.cs.meta │ ├── SelectionSearchRequest.cs │ ├── SelectionSearchRequest.cs.meta │ ├── SelectionSearchResponse.cs │ └── SelectionSearchResponse.cs.meta ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | [Bb]uilds/ 6 | Assets/AssetStoreTools* 7 | 8 | # Visual Studio cache directory 9 | .vs/ 10 | 11 | # Autogenerated VS/MD/Consulo solution and project files 12 | ExportedObj/ 13 | .consulo/ 14 | *.csproj 15 | *.unityproj 16 | *.sln 17 | *.suo 18 | *.tmp 19 | *.user 20 | *.userprefs 21 | *.pidb 22 | *.booproj 23 | *.svd 24 | *.pdb 25 | *.opendb 26 | 27 | # Unity3D generated meta files 28 | *.pidb.meta 29 | *.pdb.meta 30 | 31 | # Unity3D Generated File On Crash Reports 32 | sysinfo.txt 33 | 34 | # Builds 35 | *.apk 36 | *.unitypackage 37 | -------------------------------------------------------------------------------- /687474703a2f2f6936372e74696e797069632e636f6d2f323369396f68632e6a7067.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordinarius/Unity-Asset-Reference-Finder/efbeb5ff95e9fa074521d440080839880b04b6d8/687474703a2f2f6936372e74696e797069632e636f6d2f323369396f68632e6a7067.jpg -------------------------------------------------------------------------------- /AssetReferenceFinder.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d305b70d5c8c55349a9989bc127972d4 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b43c02c8f90c2914c9094ae9399a195a 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/ReferenceFinderWindow.cs: -------------------------------------------------------------------------------- 1 | //MIT License 2 | 3 | //Copyright(c) 2019 Ömer faruk sayılır 4 | 5 | //Permission is hereby granted, free of charge, to any person obtaining a copy 6 | //of this software and associated documentation files (the "Software"), to deal 7 | //in the Software without restriction, including without limitation the rights 8 | //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | //copies of the Software, and to permit persons to whom the Software is 10 | //furnished to do so, subject to the following conditions: 11 | 12 | //The above copyright notice and this permission notice shall be included in all 13 | //copies or substantial portions of the Software. 14 | 15 | //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | //SOFTWARE. 22 | 23 | using System.Collections; 24 | using System.Collections.Generic; 25 | using System.IO; 26 | using System.Linq; 27 | using UnityEditor; 28 | using UnityEngine; 29 | 30 | public class ReferenceCounterWindow : EditorWindow 31 | { 32 | private static ReferenceCounterWindow win; 33 | private Object selection; 34 | private SelectionSearchResponse lastResponse; 35 | private Vector2 scrollPosition; 36 | 37 | [MenuItem("Window/Reference Counter")] 38 | public static void Init() 39 | { 40 | win = GetWindow(); 41 | win.titleContent = new GUIContent("Ref Counter"); 42 | win.ShowUtility(); 43 | } 44 | 45 | [MenuItem("GameObject/Look For References", false, -2)] 46 | [MenuItem("Assets/Look For References", false, -2)] 47 | public static void LookFromContext() 48 | { 49 | Init(); 50 | win.UpdateSelection(); 51 | win.DoSearch(); 52 | } 53 | 54 | private void OnInspectorUpdate() 55 | { 56 | UpdateSelection(); 57 | Repaint(); 58 | } 59 | 60 | private void UpdateSelection() 61 | { 62 | var selection = Selection.activeObject; 63 | if (selection != null) 64 | { 65 | if (selection is GameObject || selection is ScriptableObject || selection is Object) 66 | { 67 | this.selection = selection; 68 | } 69 | else 70 | { 71 | this.selection = null; 72 | } 73 | } 74 | else 75 | { 76 | this.selection = null; 77 | } 78 | } 79 | 80 | private void OnGUI() 81 | { 82 | if (selection != null) 83 | { 84 | if (GUILayout.Button("Search References")) 85 | { 86 | DoSearch(); 87 | } 88 | } 89 | 90 | if (lastResponse != null) 91 | { 92 | scrollPosition = GUILayout.BeginScrollView(scrollPosition); 93 | var selectedObj = lastResponse.request.selectedObject; 94 | EditorGUILayout.ObjectField("Searched object", selectedObj, selectedObj.GetType(), true); 95 | foreach (var item in lastResponse.referencePairs) 96 | { 97 | GUILayout.BeginVertical(GUI.skin.box); 98 | GUILayout.BeginHorizontal(); 99 | EditorGUILayout.ObjectField("Owner", item.referenceOwnerObject, item.referenceOwnerObject.GetType(), true); 100 | EditorGUILayout.ObjectField("Target Object", item.targetObject, item.targetObject.GetType(), true); 101 | GUILayout.EndHorizontal(); 102 | var style = new GUIStyle(EditorStyles.toolbarButton); 103 | style.stretchWidth = true; 104 | style.richText = true; 105 | style.fixedHeight = 40; 106 | style.fontSize = 13; 107 | string buttonLabel = "Variable name: "; 108 | buttonLabel += item.referenceFiledName; 109 | if (item.referenceArrayIndex.HasValue) 110 | { 111 | buttonLabel += "(At array index: " + item.referenceArrayIndex.Value + ")"; 112 | } 113 | buttonLabel += "\n at Path: " + item.referenceOwnerScriptPath + " " + 114 | item.referenceFiledLineNumber; 115 | GUILayout.Space(25); 116 | var lab = new GUIStyle(GUI.skin.label); 117 | lab.alignment = TextAnchor.MiddleCenter; 118 | lab.fontSize = 18; 119 | lab.fontStyle = FontStyle.Bold; 120 | if (string.IsNullOrEmpty(item.referenceOwnerScriptPath)) 121 | { 122 | GUILayout.Label("Internal type", lab); 123 | GUI.enabled = false; 124 | } 125 | else 126 | { 127 | GUILayout.Label("Click button to peek line", lab); 128 | } 129 | if (GUILayout.Button(buttonLabel, style)) 130 | { 131 | AssetDatabase.OpenAsset(item.scriptObject, item.referenceFiledLineNumber); 132 | } 133 | if (string.IsNullOrEmpty(item.referenceOwnerScriptPath)) 134 | { 135 | GUI.enabled = true; 136 | } 137 | GUILayout.EndVertical(); 138 | 139 | } 140 | GUILayout.EndScrollView(); 141 | } 142 | } 143 | 144 | private void DoSearch() 145 | { 146 | var allComps = Resources.FindObjectsOfTypeAll(); 147 | if (selection is GameObject) 148 | { 149 | var selectionComponents = (selection as GameObject).GetComponents().Cast().ToList(); 150 | selectionComponents.Add(selection); 151 | var req = new SelectionSearchRequest 152 | { 153 | selectedObject = selection, 154 | targetObjects = selectionComponents 155 | }; 156 | 157 | lastResponse = RequestSearch(allComps, req); 158 | } 159 | else if (selection is Object) 160 | { 161 | var req = new SelectionSearchRequest 162 | { 163 | selectedObject = selection, 164 | targetObjects = new List() { selection } 165 | }; 166 | 167 | lastResponse = RequestSearch(allComps, req); 168 | } 169 | } 170 | 171 | private static bool IsAssignableFrom(System.Type to, System.Type from) 172 | { 173 | if (to.IsAssignableFrom(from)) 174 | { 175 | return true; 176 | } 177 | else if (to.IsArray && to.GetElementType().IsAssignableFrom(from)) 178 | { 179 | return true; 180 | } 181 | else if (from == typeof(Texture2D) && to == typeof(Sprite) || to.HasElementType && to.GetElementType() == typeof(Sprite)) 182 | { 183 | return true; 184 | } 185 | 186 | return false; 187 | } 188 | 189 | private static bool AreEqual(Object a, Object b) 190 | { 191 | if (a == null || b == null) return false; 192 | if (a == b) 193 | { 194 | return true; 195 | } 196 | else if (a.GetType() == typeof(Texture2D) && b.GetType() == typeof(Sprite)) 197 | { 198 | var bTex = (b as Sprite).texture; 199 | return a == bTex; 200 | } 201 | else if (a.GetType() == typeof(Sprite) && b.GetType() == typeof(Texture2D)) 202 | { 203 | var aTex = (a as Sprite).texture; 204 | return b == aTex; 205 | } 206 | 207 | return false; 208 | } 209 | 210 | private static SelectionSearchResponse RequestSearch(Component[] allComps, SelectionSearchRequest req) 211 | { 212 | var resp = new SelectionSearchResponse 213 | { 214 | request = req, 215 | referencePairs = new List() 216 | }; 217 | 218 | var goType = typeof(GameObject); 219 | var spriteType = typeof(Sprite); 220 | var textureType = typeof(Texture2D); 221 | var targetObjects = req.targetObjects; 222 | var selectedObject = req.selectedObject; 223 | //All components in scene 224 | foreach (var sComp in allComps) 225 | { 226 | var type = sComp.GetType(); 227 | var fields = type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 228 | 229 | //All fields in that component 230 | foreach (var field in fields) 231 | { 232 | { 233 | var fieldType = field.FieldType; 234 | var isFieldArray = fieldType.IsArray; 235 | 236 | 237 | foreach (var targetObject in targetObjects) 238 | { 239 | System.Type targetType = targetObject.GetType(); 240 | if (isFieldArray && IsAssignableFrom(fieldType, targetType)) 241 | { 242 | var array = field.GetValue(sComp) as IList; 243 | if (array != null) 244 | { 245 | var index = 0; 246 | foreach (Object fieldValue in array) 247 | { 248 | if (AreEqual(fieldValue, targetObject)) 249 | { 250 | var pair = new ReferencePair() 251 | { 252 | referenceOwnerObject = sComp, 253 | targetObject = fieldValue, 254 | referenceFiledName = field.Name, 255 | referenceArrayIndex = index 256 | }; 257 | resp.referencePairs.Add(pair); 258 | } 259 | index++; 260 | } 261 | } 262 | } 263 | 264 | if (IsAssignableFrom(fieldType, targetType)) 265 | { 266 | var fieldValue = field.GetValue(sComp) as Object; 267 | if (AreEqual(fieldValue,targetObject)) 268 | { 269 | var pair = new ReferencePair() 270 | { 271 | referenceOwnerObject = sComp, 272 | targetObject = fieldValue, 273 | referenceFiledName = field.Name, 274 | }; 275 | resp.referencePairs.Add(pair); 276 | } 277 | } 278 | } 279 | } 280 | } 281 | } 282 | 283 | string[] searchInFolders = new string[] { "Assets" }; 284 | string dataPath = Application.dataPath.Replace("Assets", ""); 285 | 286 | foreach (var item in resp.referencePairs) 287 | { 288 | if (item.referenceOwnerObject is Component) 289 | { 290 | var guids = AssetDatabase.FindAssets("t:Script " + item.referenceOwnerObject.GetType().Name, searchInFolders); 291 | if (guids.Length > 0) 292 | { 293 | string assetPath = AssetDatabase.GUIDToAssetPath(guids[0]); 294 | item.referenceOwnerScriptPath = assetPath; 295 | string path = dataPath + assetPath; 296 | item.scriptObject = AssetDatabase.LoadAssetAtPath(assetPath); 297 | if (item.scriptObject != null) 298 | { 299 | using (StreamReader sr = new StreamReader(path)) 300 | { 301 | int linecount = 1; 302 | while (!sr.EndOfStream) 303 | { 304 | var line = sr.ReadLine(); 305 | if (line.Contains(item.referenceFiledName)) 306 | { 307 | item.referenceFiledLineNumber = linecount; 308 | break; 309 | } 310 | linecount++; 311 | } 312 | } 313 | } 314 | } 315 | } 316 | } 317 | 318 | return resp; 319 | } 320 | 321 | 322 | } 323 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/ReferenceFinderWindow.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 48d4359065459c14d9d00c33a37d0e6e 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/ReferencePair.cs: -------------------------------------------------------------------------------- 1 | //MIT License 2 | 3 | //Copyright(c) 2019 Ömer faruk sayılır 4 | 5 | //Permission is hereby granted, free of charge, to any person obtaining a copy 6 | //of this software and associated documentation files (the "Software"), to deal 7 | //in the Software without restriction, including without limitation the rights 8 | //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | //copies of the Software, and to permit persons to whom the Software is 10 | //furnished to do so, subject to the following conditions: 11 | 12 | //The above copyright notice and this permission notice shall be included in all 13 | //copies or substantial portions of the Software. 14 | 15 | //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | //SOFTWARE. 22 | 23 | using UnityEngine; 24 | 25 | public class ReferencePair 26 | { 27 | public Object targetObject; 28 | public Object referenceOwnerObject; 29 | public Object scriptObject; 30 | public string referenceOwnerScriptPath; 31 | public string referenceFiledName; 32 | public int referenceFiledLineNumber; 33 | public int? referenceArrayIndex; 34 | } -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/ReferencePair.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 858b01249386cf9438dd88a26a132434 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/SelectionSearchRequest.cs: -------------------------------------------------------------------------------- 1 | //MIT License 2 | 3 | //Copyright(c) 2019 Ömer faruk sayılır 4 | 5 | //Permission is hereby granted, free of charge, to any person obtaining a copy 6 | //of this software and associated documentation files (the "Software"), to deal 7 | //in the Software without restriction, including without limitation the rights 8 | //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | //copies of the Software, and to permit persons to whom the Software is 10 | //furnished to do so, subject to the following conditions: 11 | 12 | //The above copyright notice and this permission notice shall be included in all 13 | //copies or substantial portions of the Software. 14 | 15 | //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | //SOFTWARE. 22 | 23 | using System.Collections.Generic; 24 | using UnityEngine; 25 | 26 | public class SelectionSearchRequest 27 | { 28 | public Object selectedObject; 29 | public List targetObjects; 30 | } 31 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/SelectionSearchRequest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: efb2088c9b6320247ad6d6ff31dd7450 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/SelectionSearchResponse.cs: -------------------------------------------------------------------------------- 1 | //MIT License 2 | 3 | //Copyright(c) 2019 Ömer faruk sayılır 4 | 5 | //Permission is hereby granted, free of charge, to any person obtaining a copy 6 | //of this software and associated documentation files (the "Software"), to deal 7 | //in the Software without restriction, including without limitation the rights 8 | //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | //copies of the Software, and to permit persons to whom the Software is 10 | //furnished to do so, subject to the following conditions: 11 | 12 | //The above copyright notice and this permission notice shall be included in all 13 | //copies or substantial portions of the Software. 14 | 15 | //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | //SOFTWARE. 22 | 23 | using System.Collections.Generic; 24 | 25 | public class SelectionSearchResponse 26 | { 27 | public SelectionSearchRequest request; 28 | public List referencePairs; 29 | } 30 | -------------------------------------------------------------------------------- /AssetReferenceFinder/Editor/SelectionSearchResponse.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6c4fce0a0122cbe43a5515a799560007 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ömer faruk sayılır 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity-Asset-Reference-Finder 2 | 3 | An asset reference finder for asset heavy projects 4 | 5 | ![Alt text](687474703a2f2f6936372e74696e797069632e636f6d2f323369396f68632e6a7067.jpg) 6 | --------------------------------------------------------------------------------