• 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 /
  • Help Room /
avatar image
0
Question by TheCaptainJuneBug · Jun 03, 2016 at 06:40 PM · c#unity 5editorguieditorguilayouttypecasting

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:

alt text

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??

untitled.png (54.0 kB)
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

1 Reply

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

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.

Comment
Add comment · Show 3 · 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 TheCaptainJuneBug · Jun 03, 2016 at 07:36 PM 0
Share

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.

avatar image Grimmick TheCaptainJuneBug · Jun 03, 2016 at 07:52 PM 1
Share

I'm just talking about the cast, not the parameter of ObjectField. So

 data = (ObjectData)EditorGUILayout.ObjectField(data, selectedType, true); 

does not work?

avatar image TheCaptainJuneBug Grimmick · Jun 03, 2016 at 07:59 PM 0
Share

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to add EditorGUILabels and am getting a couple errors. 0 Answers

Custom EditorWindow indicator 0 Answers

Script attached to An enemy but dosent work right if there are multiple enemies. 2 Answers

How to get input of VR head movement??!! 1 Answer

InvalidCastException: Cannot cast from source type to destination type. 1 Answer


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