Dynamic casting?
Im not sure if this is even the right question or terms being used.
So i have a EditorGUILayout.Popup field that displays all the classes that inherit from a abstract class. Throw those classes names into a string array to feed into my EditorGUILayout.Popup field. All that works perfectly fine.
I have an EditorGUILayour.ObjectField() thats casted to the abstract class.
This is the problem.
Type selectedType = Type.GetType(objectTypes[objectTypeIndex]); // objectTypes is an string[]
EditorGUILayout.PrefixLabel(selectedType.Name + ":");
data = (selectedType)EditorGUILayout.ObjectField(data, selectedType, true); /// ERROR LINE
I want to cast the return type of EditorGUILayout to the type of the variable selectedType.
Above is something i would like to try and do.
I thought about generics but, with this application, i would change the type during the application.
The reason i want to do this is to filter the options, the objectfield generated in the window you select an asset from.
EDITED:
But what i'm trying to do essentially is filter the results of the object picker panel based on the selectedType.
Instead of showing all of the ScriptableObjects that use those inherited scripts. Like "Archor Tower" and "Gun Tower" which are both of type TowerData, I just want to show "Gun Tower Skill" which is of type SkillData.
That is what it shows if i change the code to:
data = (ObjectData)EditorGUILayout.ObjectField(data, typeof(ObjectData), true);
At the time of this post i didnt know the window that pops up from a click on the object field was a object picker.
So how would i go about filtering the Object Picker to show the scriptableobjects of the type selected??
Answer by Grimmick · Jun 03, 2016 at 07:23 PM
You cannot cast to a type like this. The compiler needs to know the type you want to cast to statically.
If I understand correctly, data has the type of the abstract class and all objects implement this class. So you would just want to cast to the abstract class.
If you really need to do anything type specific which cannot be handled via members of the abstract class you can do type checks like "if (data is FancyClass)" or "FancyClass fancyData = data as FancyClass" and check for fancyData != null.
That is not exactly what i'm asking, that is how i usually do my casting, the way you specified.
But what i'm trying to do essentially is filter the results of the object picker panel based on the selectedType.
At the time of this post i didnt know the window that pops up from a click on the object field was a object picker.
I'm just talking about the cast, not the parameter of ObjectField. So
data = (ObjectData)EditorGUILayout.ObjectField(data, selectedType, true);
does not work?
I thought i tried this combination, but just tried it again and now it works!
I think my get type wasnt working when i tried this.
Thank you!
Your answer
Follow this Question
Related Questions
Trying to add EditorGUILabels and am getting a couple errors. 0 Answers
Custom EditorWindow indicator 0 Answers
Trail renderer always black? 0 Answers
How to use the results of a dice roll? 2 Answers
click idle and i use PlayerPrefs 2 Answers