Hello, thank you for reading.
Im currently trying to build a script that allows one to search for anything of a certain ‘type’ based on a selection from a popup of different ‘types’ however i am having trouble finding a way to populate this popup.
Is there a way for me to list all ‘types’ in a project?
Do you mean types of scripts? There is a lot of ways to get a list of them. You can create an adhoc list by just creating an array and making each entry ‘typeof(TheScriptTypeA), typeof(TheScriptTypeB), etc, etc’. Or you can reflect the current assembly and get all types that meet some criteria (inherit from, implement, etc). You can see this here:
Note the various ‘GetTypes’ methods.
But I don’t know if that’s what you mean by ‘type’. You could easily mean something like ‘type of prefab’ or ‘type of player’… what do you mean by ‘type’?
The above code will create an array of all fonts within the project. The same can be done for any ‘type’ but im looking for a way to list all of these ‘types’ so i can slot them in where font is.
GetType will return the type of a single object, but im looking to get an array with every type within it.
Well, i jumped in to trying to use Assembly.GetTypes and are immediately running in to error CS0120: An object reference is required to access non-static member
I am now very confused while reading how people are dealing with this.
Okay, after some more looking, i have the folliwing:
using UnityEngine;
using System.Collections.Generic;
using System;
using System.Reflection;
using System.Linq;
public static class TypeFinder{// : MonoBehaviour {
// Use this for initialization
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
{
if (assembly == null) throw new ArgumentNullException("assembly");
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
return e.Types.Where(t => t != null);
}
}
}
However i am recieving an error.: Assets/Scripts/TextTypes.cs(8,35): error CS0117: TypeFinder' does not contain a definition for GetTypes’.
I’m really not sure how to deal with this. Could i have some help please?
This is the point where we send you back to the learn section. If you can’t interpret this error you are not ready to tackle reflection. Reflection is a pretty advanced topic. You need a strong foundation in coding first.
And yes, im learning about things as i find they are needed for my project. This is needed as a foundation to everything behind it. So the advise of not doing this is not particularly helpful. Sorry.
What are you trying to do? There might be an easier way. Unless you are doing some heavy meta programming or writing extensive editor tools then reflection can usually be avoided.
I thought this would be all much simpler when i started. Im working with playmaker and i wanted a way to search for all the fonts in my project, because in my game, the player can choose the font they want to use. So i found out i could use Resources.FindObjectsOfTypeAll to do that, however, i wanted to make an action that was more flexable so i could use it for multiple things later.
So i figured if i made an action that populated an array with all of the objects of a specific type that is specified by a dropdown list, i would be able to reuse this for a lot of things down the road. However, i have found finding out what types there are to be rather complicated as you can see. Im currently working this out, and i think ive almost got it working. Im dealing with an error CS1501 right now when trying to call from the static class.
Learning programming the hard way, i know. but i seem to like making thigns hard for myself.
Im simply trying to print the list of types to test it here, and am getting that no overload for method’get loadable types’ takes 0 arguments.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class TextTypes : MonoBehaviour {
IEnumerable<Type> workingTypes;
// Use this for initialization
void Start () {
workingTypes = TypeFinder.GetLoadableTypes();
foreach (IEnumerable<Type> ofMyTypes in workingTypes){
print (ofMyTypes);
}
}
}
GetLoadable types is still the same from what i posted above.
I see where you are coming from now. Playmaker has let you cheat and skip the basics. You probably want to go back and learn them at some point. Normally by the time you can ask the question in the OP you are a pretty decent coder.
Have you checked out this stack overflow question? It seems to be pointing in the right direction.
Okay, good news and bad news. I got it to print out a list, however it is only a list of scripts in my project. it doesnt include anything useful like Font, which is the main reason to use it at the moment.
Do you think i should just make a switch that i edit as i want to make it search for more types instead?