Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by TrueMasalan · Dec 16, 2021 at 03:20 PM · inspectordepth

Nested objects depth in inspector

alt text

Hello, can you please tell me if I can decrease the depth of neting in inspector, because i dont like the way it is now. I have a structure like this:

 public class PartCore : MonoBehaviour
 {
      public ConnectedParts connectedParts;
 }

 [System.Serializable]
 public class ConnectedParts
 {
     public LeftPart left; 
     public RightPart right; 
     public BottomPart bottom; 
     public UpperPart upper;
 }

 [System.Serializable]
 public class LeftPart : SidedPart
 {
 }
 [System.Serializable]
 public class RightPart : SidedPart
 {
 }
 [System.Serializable]
 public class BottomPart : SidedPart
 {
 }
 [System.Serializable]
 public class UpperPart : SidedPart
 {
 }
 public class SidedPart
 {
     public PartCore part;
 }

Can you please tell me if i can decrease it to just showing parts straightforward, without showing side of a part?

UPD: thanks for respond! Sorry for my terrible question format. Basically, I just want my variables to have a nice look, not like it's done now. Now I have to expand every single "Left" or "Right" to somehow edit "Part" field. The thing I want to do is to reposition Part fields the way it would be eazier to edit ConnectedParts. For example it would be very nice if Parts were side by side just like if they vere positioned there:

 [System.Serializable]
 public class FourFloats
 {
     public float a;
     public float b;
     public float c;
     public float d
 }

1.png (22.9 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
1

Answer by Bunny83 · Dec 16, 2021 at 04:59 PM

Your current code does only contain type definitions but no actual fields. You probably have a ConnectedParts instance in your PartCore class? Also your ConnectedParts class probably has 4 fields (named left, right, bottom, upper) of type SidedPart? Also SidedPart probably has a field named "part" of type PartCore? Why have you omitted all that information? It makes it really difficult to actually follow your issue.


Well, assuming my summary is more or less correct, I don't quite understand what you mean by decreasing the nesting. You have setup that kind of nesting, so how do you imagine that could look differently? If the SidedPart class has only a single field, you could implement a property drawer which draws the SidedPart class inline. Not sure if that's already what you wanted?


We can't even show you an example property drawer for your classes since your classes are missing all the fields. If you need any further help with this, please edit your question, add all the relevant code and also format your code properly. When you copy&pasted your code, just select all your code and press the 101 / 010 button. The code formatting requires an empty line before and after the code section and each line has to be indented by 4 spaces. That's what this button does.


edit


If I understood you correctly you simply want a property drawer like this:

 #if UNITY_EDITOR
 namespace Editor
 {
     using UnityEditor;
 
     [CustomPropertyDrawer(typeof(SidedPart), true)]
     public class SidedPartDrawer : PropertyDrawer
     {
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             EditorGUI.PropertyField(position, property.FindPropertyRelative("part"), label);
         }
     }
 }
 #endif

You can include this in your script file but it's better to put that class into its own file inside an editor folder. This will simply display the part field inline. Of course if you add more fields to your SidedPart class or any of the derived classes, those would not show up in the inspector since our custom property drawer only draws that one nested property.

Comment
Add comment · Show 4 · 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 TrueMasalan · Dec 16, 2021 at 07:15 PM 0
Share

Thank you for your answer. I edited my question, so can you please take a look at it once again?

avatar image Bunny83 TrueMasalan · Dec 16, 2021 at 09:20 PM 0
Share

I've edited my answer and included a property drawer example. Why do you have 4 seperate derived classes for each direction?

avatar image TrueMasalan Bunny83 · Dec 17, 2021 at 07:49 PM 0
Share

Hello, thank you for help a lot)

The reason for 4 separate classes is simple: polymorphism is one of the best solutions to replace those endless if-else statements.

For example the other way of implementing such a thing is creating enum with all four direction (as it is in my situation). In that case I have to use if-else every time I want to do something different with my objects and compere their direction from enum.

Inheritance makes such code much easier to read, expand and decreases code repetition. 4 separate classes derived from basic class allow me to do side-related work inside classes the way they need. For example I have a SidedPart (base of LeftPart etc.) method "Attach", that attaches all sided partsto main part. LeftPart, RightPart, BottomPart, UpperPart override "Attach" method to be attach in the position, parts need to be. Like LeftPart attaches itself on the left side of main part. Other parts attach them on the other sides and so on.

This pattern also help to hide realisation of the methods from user, what makes it more comfortable to write code.

I hope that I answered your question correctly)

Show more comments

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

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

Messing With Gradients At Runtime? 2 Answers

[Uni2D + Unity Inspector] Unity extremely slow, when a Sprite is selected 4 Answers

Inspector scripting, adding lists, checkboxes and buttons [With Image] 0 Answers

Can I use custom inspector widgets, such as a slider for a floating point property, without writing a custom editor? 3 Answers

no way to understand this 2 Answers


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