Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by VivienS · Oct 25, 2017 at 05:50 PM · editorassetscriptableobjectfindscriptable object

How can I find all instances of a Scriptable Object in the Project (Editor)

I have the .cs script of a ScriptableObject in the project. Somewhere in the project, there are instances of this ScriptableObject. I don't know their names, because someone else created them. Is there an easy way to find them? "Find References" works only in the Scene and not in the Project.

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
15
Best Answer

Answer by PizzaPie · Oct 25, 2017 at 06:38 PM

 public static T[] GetAllInstances<T>() where T : ScriptableObject
     {
         string[] guids = AssetDatabase.FindAssets("t:"+ typeof(T).Name);  //FindAssets uses tags check documentation for more info
         T[] a = new T[guids.Length];
         for(int i =0;i<guids.Length;i++)         //probably could get optimized 
         {
             string path = AssetDatabase.GUIDToAssetPath(guids[i]);
             a[i] = AssetDatabase.LoadAssetAtPath<T>(path);
         }
 
         return a;
 
     }

This will return all objects of Type T, you may make it more general if you change the where T : ScriptableObject to something more basic, but should suffice fo your occasion. Run it inside a Custom Editor, or create a ScriptableObject(Create an instance in the editor) with an Array of wanted type of scriptables and inside OnEnable() call that function. (if it is one time thing second solution probably is the fastest). Cheers.

Edit: make sure to add using UnityEditor; , this will not work on runtime.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VivienS · Jan 28, 2018 at 11:36 AM 0
Share

Thanks, was hoping there was some out-of-the-box feature for this!

avatar image
7

Answer by JosehCastro · Mar 02, 2018 at 07:36 PM

There is already an accepted answer, but it seems there is a more ergonomic way.

In the Project tab, right beside the search box, find the Search By Type button, with the circle, triangle and square icon. Select entries in that dropdown and see how their search strings are prepended by a t:.

So, If you want to find all the ScriptableObject assets in your Project folders, you can search for:

  t:ScriptableObject



To filter it even further you can search for specific subclasses of ScriptableObject by searching for their type:

 t:MyScriptableObjectSubclass



It will look like this: The search box in the Project tab.

Edit: add more info and example code.

You can convert any search into a favorite by clicking on the star button.

I'm also using the script below to add a menu entry to make searching easier. Do notice that it uses reflection to access private methods inside the project window class. That is not safe and may break without notice whenever Unity changes its implementation.

 using System;
 using System.Reflection;
 using UnityEditor;
 using UnityEngine;
 
 namespace Editors {
     class EditorProjectWindowExtensions {
         [MenuItem("Assets/Find ScriptableObject Instances",false, 30)]
         private static void DoSomethingWithVariable() {
             SetSearchString("t:" + Selection.activeObject.name);
         }
 
         // Note that we pass the same path, and also pass "true" to the second argument.
         [MenuItem("Assets/Find ScriptableObject Instances", true)]
         private static bool NewMenuOptionValidation()
         {
             if (!(Selection.activeObject is MonoScript)) {
                 return false;
             }
             MonoScript o = (MonoScript) Selection.activeObject;
             Type ot = o.GetClass();
             return ot.IsSubclassOf(typeof(ScriptableObject));
         }
 
         private static void SetSearchString(string searchString)
         {
             Type projectBrowserType = Type.GetType("UnityEditor.ProjectBrowser,UnityEditor");
             if (projectBrowserType == null) {
                 return;
             }
             MethodInfo setSearchMethodInfo = projectBrowserType.GetMethod("SetSearch", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, null);
             if (setSearchMethodInfo == null) {
                 return;
             }
 
             EditorWindow window = EditorWindow.GetWindow(projectBrowserType);
             setSearchMethodInfo.Invoke(window, new object[] {searchString});
         }
     }
 }


findscriptableobjectasset.png (134.9 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deadcow_ · Jul 27, 2018 at 05:40 AM 0
Share

You may also add search query to favorites ins$$anonymous$$d of $$anonymous$$enuItem. That tiny star button at right to the search field. Unfairly forgotten feature)

avatar image
1

Answer by ogulcan_topsakal · Dec 27, 2021 at 08:40 AM

     /// <summary>
     /// Get all instances of scriptable objects with given type.
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <returns></returns>
     public static List<T> GetAllInstances<T>() where T : ScriptableObject
     {
         return AssetDatabase.FindAssets($"t: {typeof(T).Name}").ToList()
                     .Select(AssetDatabase.GUIDToAssetPath)
                     .Select(AssetDatabase.LoadAssetAtPath<T>)
                     .ToList();
     }

It was a problem I encountered while doing research. If anyone still interested there is an edited version of @PizzaPie 's answer with using LINQ.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer

Save ScriptableObject On Unity Application Quit? 1 Answer

Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers

How to Find Assets of the Same Type in Editor Script 1 Answer

ScriptableObject not saving data to asset 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges