Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.
This repository was archived by the owner on May 9, 2025. It is now read-only.

BaseReferenceDrawer constant uses wrong height for a custom struct #119

@ghost

Description

Issue

After generating a Reference ScriptableObject script with the SO code generator using a custom serializable struct with its own propertyDrawer, the representation of the struct has the wrong height when folded out in the editor. As far as I know, this only happens for SO References, not any of the other SO classes. It requires the "Use Constant" flag to be true. The height for the resulting representation of the property is always wrong, as the foldout icon takes up an entire line's height.

image

image

Reproduction flow

Take a simple struct, in this case called CameraView. It contains two Vector3 values, to determine the desired rotation (in Euler angles) and position of a camera. This is the struct definition:

[Serializable]
public struct CameraView {
    public Vector3 cameraPosition;
    public Vector3 cameraRotation;
}

Since the struct is serializable by the UnityEditor, we have a very basic PropertyDrawer, that just draws the fields as intended:

OnGUI:

EditorGUI.PropertyField(position, property, label, true);

And GetPropertyHeight:

return EditorGUI.GetPropertyHeight(property);

After this, we generate SO Architecture code for the CameraView type, including a CameraViewReference. This allows us to create a few CameraViewVariable assets, which look fine in the editor:
image
(Of course, we're ignoring the developer description as that was already mentioned in a different issue.)

When we finally include this reference in a MonoBehaviour, like so:

public CameraViewReference cameraView;

It looks fine as a variable:
image

But when we select "Use Constant", we are immediately presented with the foldout icon, positioned a bit strangely:
image
This isn't a major issue though, as it's still possible to click the rightmost edge of the foldout icon, which results in the images shown at the top of this issue.

Another issue would be the indentation of this struct, as it would be better suited using the full width underneath. However, that is not the main focus.

Potential solution

After digging around in the BaseReferenceDrawer, I found a potential solution. In GetPropertyHeight, the SupportsMultiLine bool is false for custom structs. When manually set to true for testing purposes, the height changes when the field is folded out.
Closed:
image
Folded out:
image

But as you can see, the height is still a bit strange, as it now results in a bit of unused space. Regardless, I noticed the use of the [MultiLine] attribute for classes would allow one to make use of this behaviour. However, as it stands, this is only reserved for classes, and not for structs.

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions