Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AlwaysSunny · Mar 06, 2013 at 09:09 PM · custom-inspectorobjectfield

create "draggable reference" within custom inspector?

To illustrate, I'll share a screenshot of the custom inspector I'm working on.

alt text

I'm making logical groupings of DetailPrototype objects to paint to the terrain together at runtime. This uses two custom classes, VeggieGroups, and VeggieUnits.

Currently a VeggieUnit just has an index value referring to the DetailPrototype it represents in terrainData.detailPrototypes[], and a density value to use when painting.

I'm currently manually keying in the integer "index" value of a DetailPrototype object into instances of VeggieUnit objects. The whole point of a custom inspector in the first place was to avoid this, but I'm still not sure how. I can't seem to create any user-friendly alternative to keying integers, such as exposing the represented DetailPrototype object so I can "drag" the reference to a VeggieUnit's DetailPrototype property.

What are my options for intuitively giving my custom class instances a reference to these DetailPrototype objects? Thanks a million,

Edit
For your consideration, the following ObjectField won't compile, giving "can't convert DetailPrototype to Object" or, through variations on conversion methods, "can't convert Object to DetailPrototype"

     tvm.customDetailPrototypes[i] = EditorGUILayout.ObjectField(
         "prototype:",
         tvm.customDetailPrototypes[i],
         typeof(DetailPrototype),
         true)
         as DetailPrototype;


custom_editor.jpg (134.2 kB)
Comment
Add comment · Show 5
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 alexanderameye · Dec 04, 2016 at 01:58 PM 0
Share

How did you create this custom inspector?

avatar image Bunny83 alexanderameye · Dec 04, 2016 at 02:40 PM 0
Share

Just use Unity's I$$anonymous$$GUI system. Have a look at my GUI crash course in case you have never worked with OnGUI / OnInspectorGUI.

avatar image alexanderameye Bunny83 · Dec 04, 2016 at 02:52 PM 0
Share

I'm going to read that right now! :)

avatar image Glurth · Dec 04, 2016 at 05:02 PM 0
Share

Can you derive your DetailPrototype from Object? Better yet, derive it from ScriptableObject, and you can have the ability to even save these objects as project assets. (then you can drag them right from your project folders, into a component field.)

avatar image Bunny83 Glurth · Dec 04, 2016 at 06:19 PM 0
Share

DetailPrototype is a sealed Unity class that is used by the TerrainData ScriptableObject class. It's just a normal serializable class.

1 Reply

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

Answer by Bunny83 · Dec 04, 2016 at 06:17 PM

I know the question is 3.5+ years old but since it got bumped already ....

The ObjectField only works for classes which are derived from UnityEngine.Object. Such classes are always serialized seperately as assets or as instances inside a scene. References to such objects persist since they have their own instanceID.

"DetailPrototype" on the other hand is just an ordinary data class. Such data classes aren't serialized on their own. Unity simply serializes the variables in those classes as sub-fields of the MonoBehaviour that references them. So there actually isn't a serializable reference to such classes. Those instances shouldn't be seen as seperate objects but as part of the script instance.

So if you want to store additional data for each DetailPrototype that exists inside the terrain data, you have only limited options. The most straight forward way would be, as you currently do, is to simply use the index of each item. This of course will give you problems when you add or remove items as this will mess up the indices. You could additionally store serializable references that the DetailPrototype instance holds (like the prototype gameobject reference or the protptypeTexture) inside your own "extension" class in a hidden field. That way you can match your extension data with the actual instance based on those references. This would allow to detect when items got removed, added or changed.

So you still would use indices but additional use the other information to verify it's still the right object.

If the question was more about how to initially reference those DetailPrototype instances there's no ready-to-use solution. Unity's built-in DragAndDrop support only works for serialized assets. So you can't "drag" them over from the terrain.

What you could do is display a tiny preview of all DetailPrototypes available in the terrain data inside your own inspector and add drag and drop support for them manually. I suggest you download ILSpy and look at the ObjectField code as well as

Comment
Add comment · 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

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

13 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

Related Questions

'type mismatch' at custom inspector .asset 0 Answers

How to choose the transform tool from script ? 4 Answers

Custom editor: How to initialise a new array element? 1 Answer

Draw PropertyDrawers in PropertyDrawers? 0 Answers

Is it possible to call a custom inspector? 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