- Home /
Nested objects depth in inspector
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
}
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.
Thank you for your answer. I edited my question, so can you please take a look at it once again?
I've edited my answer and included a property drawer example. Why do you have 4 seperate derived classes for each direction?
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)
Your answer

Follow this Question
Related Questions
Is it possible to show Static Variables in the Inspector? 10 Answers
Why are are there no terrain editing tools? 1 Answer
How can I get all the options this guide have in the inspector? 1 Answer
[SOLVED]How to use attributes from array of custom class instances? 1 Answer
Using keys to change Inspector enums 1 Answer