Skip to content

BaseReferenceDrawer constant uses wrong height for a custom struct #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Apr 3, 2020 · 4 comments
Closed

BaseReferenceDrawer constant uses wrong height for a custom struct #119

ghost opened this issue Apr 3, 2020 · 4 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@ghost
Copy link

ghost commented Apr 3, 2020

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.

@DanielEverland
Copy link
Owner

Thanks for reporting this issue in so much detail!

I should hopefully have some time in the coming week to catch up with the increasing list of issues :)

@DanielEverland DanielEverland self-assigned this Apr 3, 2020
@DanielEverland DanielEverland added the bug Something isn't working label Apr 3, 2020
@ghost
Copy link
Author

ghost commented Apr 15, 2020

I've encountered the same problem in the collection editor.
image

I did things a bit different this time along. I generated these scriptable objects for a class instead of a struct, and putting the MultiLine attribute above the class declaration doesn't seem to have any effect.

I figure that this is probably the result of the same problem. To reproduce this one, just follow the original reproduction flow, but generate a collection as well. Using a class VS a struct might make a difference but I'm not completely sure about that.

@DanielEverland
Copy link
Owner

I'm working on a fix by implementing #110 - this is, as you might imagine, quite a big feature. Once it's done everything should render properly, since I'm also re-writing all the editors.

Once it's done you won't have to implement PropertyDrawers either, unless you want some custom behaviour, of course.

@DanielEverland
Copy link
Owner

This will be fixed in the new update. Just added the finishing touches.

This update includes drawing custom objects without property drawers. The above is an example of that, the type simply including the following:

[System.Serializable]
public class TestType
{
    [SerializeField]
    private float floatValue;
    [SerializeField]
    private Quaternion quaternionValue;
    [SerializeField]
    private SubType subType;

    [System.Serializable]
    public class SubType
    {
        public double publicValue;

        [SerializeField]
        private string privateValue;
    }
}

@DanielEverland DanielEverland added this to the Release 1.7 milestone Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant