Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KEric · Nov 05, 2015 at 02:38 PM · guieditorcustomeditorguicustom-inspector

Drawing Editor Inspector GUI based on selected/current prefab (CustomPropertyDrawer)

Does anyone knows if it is possible to dynamically display GUI entries in the Editor's Inspector based on the GameObject/prefab of my choice?

Let's say I have a several instances of a player prefabs (for example various cars) and those cars have different elements such as guns, missiles launchers, defense shields etc Beside that I have a GameObject that describes all characteristics of a car (it's pricing, upgrades etc). This object has an array of my prefab cars and within that GameObject Inspector window I would like to show dynamically different GUI elements based on the prefab from the array.

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

2 Replies

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

Answer by KEric · Nov 06, 2015 at 01:11 PM

After doing some research (mainly watching tutorials that I'm attaching below and playing with intellisense completion in Visual Studio as well as debugging and logging to console output) I found what I was looking for.

First to have my custom editor window (or I suppose my "custom property") I needed to have respective [CustomPropertyDrawer(typeof(MyClass))] implemented within the /Editor folder. Then within void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) method I was able to get to my MyClass::car_prefab using GameObject prefab = (GameObject)prop.FindPropertyRelative("car_prefab").objectReferenceValue;

So the shortest answer to my problem seems to be the usage of objectReferenceValue of SerializedProperty returned from FindPropertyRelative function execution. Hope it helps somebody in future. Cheers

[1] https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/property-drawers-custom-inspectors

[2] https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector

PS. One thing worth mentioning - because property size in Editor Inspector will differ based on the information contained within the prefab of my choice, there is a need of overriding public override float GetPropertyHeight (SerializedProperty prop, GUIContent label) within CustomPropertyDrawer

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 SargeTrubix · Mar 15, 2018 at 08:03 AM 0
Share

So, in the end, when you are displaying some properties of choosen gameobject (prefab) and then changing their values in inspector, what is happening? It changes the values of properties of prefab or its instance, existing only in this particular script?

avatar image
0

Answer by HenryStrattonFW · Nov 05, 2015 at 05:57 PM

This is very possible, but is probably easier not to do as a property drawer, but as a custom inspector (CustomEditor class).

If you're writing your own CustomEditor (inspector) for your class you just need a way to select an item from the array (easiest solution may just be to have a variable on your inspector for the current index in the array, and some buttons (next, prev, first, last, whatever you need) those buttons then just change that index value (wrapping or clamping as necessary). You can get access to the array via the "target" of the CustomEditor you can either get the object as a serializedObject or as a regular game object, whichever works for you.

You can then just check what item in the array is identified with your index, and then draw whatever other elements you want on your Inspector based on that.

If you don't want to completely remake all of the other GUI elements you may need for the inspector, you can also just code these changes, and draw the rest of the inspector using the DrawDefaultInspector method. reducing the need for you to remake all the other displays.

Hope this helps point you in the right direction.

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 KEric · Nov 06, 2015 at 10:05 AM 0
Share

Thanks for the answer. I don't know if I made the problem clear in my previous comment. I just want to be able to show different set of values, components (class objects) in the inspector, based on the prefab of my choice (in this case the values of the prefab would dictate the inspector's view, for example if some value, let's say array of strings in the prefab is null then nothing should be showed in the inspector based on this prefab).

avatar image HenryStrattonFW KEric · Nov 06, 2015 at 10:35 AM 0
Share

Ah i see, so you have multiple prefabs and you're selecting them and want the inspector to change based on the selected object, not an array or prefabs with a selection of one of those. Ok.

Well its more or less the same thing. You just write a CustomEditor for your script. which then serves as the Inspector. in the inspector you just draw whatever values you DO want to display based on whatever key variable values you want to check on the selected object.

Have you written a custom inspector before? If not heres a usefull link to get you started. https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector

avatar image KEric HenryStrattonFW · Nov 06, 2015 at 01:02 PM 0
Share

I have written Custom Property years ago and also have seen building-custom-instpector tutorial. Thanks for your help, I think that I finally found what I was looking for (see the co$$anonymous$$g answer if you're interested)

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

35 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

Related Questions

Custom Material Editor 1 Answer

How do I set the current script as the selection in the project view? 1 Answer

How to create a lightweight Item/Mineral/etc System 1 Answer

Modify editor GUI content using mouse click 1 Answer

Custom Editor GUI Elements 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