- Home /
How do you get a list of project scripts at edit-time?
I have a custom class called MyCustomClass.
I would like to provide a UI selector in the editor so that the user can choose from one of their own classes that derives from MyCustomClass.
Either something similar to an ObjectField (which doesn't work for this purpose) where a user can open up a selector that lists all the objects you specify, or, a dropdown menu that lists all the objects you specify.
Then when the user selects one, I can set a backing variable to the user's choice, and store it on another class for later use at runtime.
My question is, how do I obtain a list of classes from within an editor script, both a CustomInspector, and an EditorWindow?
EditorGUILayout.ObjectField doesn't seem to work. I get an "InvalidCastException" error, yet when I print the member I feed that function, and the returned result, both print as being of type MyCustomClass. So I think that is an editor bug. I also tried Object.FindObjectsOfTypeIncludingAssets however I see some strange behaviour with this, as follows:
// Returns all meshes.
Mesh[] meshes = (Mesh[])Object.FindObjectsOfTypeIncludingAssets( typeof( Mesh ) );
// Returns all shaders, including built in shaders.
Shaders[] shaders = (Shader[])Object.FindObjectsOfTypeIncludingAssets( typeof( Shader ) );
// Returns every single asset.
MyCustomClass[] myClasses = (MyCustomClass[])Object.FindObjectsOfTypeIncludingAssets( typeof( MyCustomClass) );
Any information and help appreciated, perhaps there is a solution I have not considered, and am approaching this the wrong way.
Answer by Ian Smithers · Jan 03, 2013 at 10:20 AM
Hello, I managed to find a solution as follows - hope this helps if anyone has a similar task:
public static MonoScript[] GetScriptAssetsOfType<T>()
{
MonoScript[] scripts = (MonoScript[])Object.FindObjectsOfTypeIncludingAssets( typeof( MonoScript ) );
List<MonoScript> result = new List<MonoScript>();
foreach( MonoScript m in scripts )
{
if( m.GetClass() != null && m.GetClass().IsSubclassOf( typeof( T ) ) )
{
result.Add( m );
}
}
return result.ToArray();
}
Unity crashes when I iterate over array "scripts". I'm calling your method OnGui in editor script. Do you know why this could happen?
The problem was in the fact that the script is also finding Shaders, so you need an additional check. s.GetType() != typeof(Shader)
If you don't need editor scripts :
public static $$anonymous$$onoScript Find$$anonymous$$onoScriptFromType(Type type)
=> Array.Find($$anonymous$$onoImporter.GetAllRuntime$$anonymous$$onoScripts(),
(m) => m.GetClass() != null && m.GetClass().IsSubclassOf(type));
public static $$anonymous$$onoScript[] Find$$anonymous$$onoScriptsFromType(Type type)
=> Array.FindAll($$anonymous$$onoImporter.GetAllRuntime$$anonymous$$onoScripts(),
(m) => m.GetClass() != null && m.GetClass().IsSubclassOf(type));
Answer by CodeMasterMike · Jan 03, 2013 at 06:30 AM
When I retrieve all methods from a single script, I use reflection to get a list of all methods:
Assembly []referencedAssemblies = System.AppDomain.CurrentDomain.GetAssemblies();
for(int i = 0; i < referencedAssemblies.Length; ++i)
{
System.Type type = referencedAssemblies[i].GetType( scriptNameString );
if( type != null )
{ // I want all the declared methods from the specific class.
System.Reflection.MethodInfo []methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
}
}
Maybe you can use some similar way to get out all classes from a single script.
Good luck!
Thanks for the post, I realized my question title could have been misleading so I edited it. For clarification, I need to get a list of all the scripts in the project, that match a certain type, at edit-time. So lets say I have 100 scripts, and 25 of them all derive from $$anonymous$$yCustomClass, I want to let the user choose one of these from a custom editor UI. Currently I can only get Unity assets (such as $$anonymous$$eshes or Shaders) or all assets, as in my code snippet. Hopefully that clears it up, thanks again, cheers!
Ah, okey that makes it a little clearer.
You can get all scripts (which are monobehavior type scripts) by this function:
Component []objectCompoentList = currentGameObject.GetComponents(typeof($$anonymous$$onoBehaviour));
$$anonymous$$aybe you can sort out those who aren't derived from the $$anonymous$$yCustomClass somehow.
That will get scripts from a selected GameObject, but I am attempting to get the scripts in the project, the actual raw script assets themselves, similar to how they appear in the project view - except I wish to filter them and then display a filtered list of them in the custom editor.
Answer by glitchers · Jul 15, 2016 at 09:54 AM
Unity has made Object. FindObjectsOfTypeIncludingAssets
obsolete and recommends you use Resources. FindObjectsOfTypeAll
but this will not find all assets.
I have written a small piece of code that you can use in Editor only that takes a generic type and will return all objects in your project regardless of if they have been loaded yet.
public static List<T> FindAssetsByType<T>() where T : UnityEngine.Object
{
List<T> assets = new List<T>();
string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(T)));
for( int i = 0; i < guids.Length; i++ )
{
string assetPath = AssetDatabase.GUIDToAssetPath( guids[i] );
T asset = AssetDatabase.LoadAssetAtPath<T>( assetPath );
if( asset != null )
{
assets.Add(asset);
}
}
return assets;
}
Answer by Jennell555 · Aug 24, 2021 at 09:39 AM
You can use the Apps Script dashboard to view and manage individual executions of Apps Script project functions. You can find a full log of recent executions by selecting the My Executions item in the left nav.
The My Executions panel shows by default a log of all previous and currently running executions for projects for which you are an owner, editor or viewer. This list can also include function executions in projects that you don't have access to if they run on your behalf (for example, add-ons that you've installed and run). The execution list only shows the initial function that is called to start the execution. It does not show every function that was called during that execution.
You can control which type of execution is reported in the log using the filters at the top of the view. Each row of the log represents a single execution. The Start Time, Duration, and Status columns show the corresponding information about that execution.
The Function column shows the name of the function that initiated the execution. There is no name in this column if you don't have access to the execution's corresponding script project but it ran on your behalf.
The Type column shows what initiated the execution. Values include:
Add On. The execution originated from an add-on. Execution API. The execution originated from an invocation of the Apps Script API. Time Driven. The execution was caused by a time event. Trigger. The execution originated from a trigger source. Webapp. The execution originated from a deployed web app. Editor. The execution originated from the Apps Script editor.,The Apps Script dashboard lists all the script projects that you can view or ... The number of times a project has been "run" or executed.
The Apps Script dashboard lists all the script projects that you can view or edit. The left nav of the dashboard divides these projects into the following categories:
Starred Projects. Projects that you are monitoring. My Projects. Projects for which you are an owner. All Projects. Projects which you own or have view or edit permissions for. Shared with me. Projects that you do not own but that have been shared with you. Trash. Projects that you have removed from Google Drive. The project lists show the project name, owner, and last modified date. The icons next to the project name indicate whether the project is a standalone project or a bound project.