0
\$\begingroup\$

I have a bespoke struct, which has XYZ variables just like Vector3, but unlike Vector3, the inspector does not display them neatly and horizontally. I have tried to read documentation and copy Vector3 to no avail. Please tell me how to make my custom struct display XYZ in the inspector exactly like Vector3. Below is a screenshot from the Unity editor to show the difference ("Test" is a Vector3, Position is my own class).

capture from inspector

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

As Zibelas noted, you can use a custom property drawer. However, let's analyze why you are making a custom struct with the same fields as a Vector3 (or why you probably shouldn't).

If your struct functions exactly the same as a Vector3 (or Vector3Int), there's no point in using it over Vector3, since Unity's APIs make extensive use of Vector3.

If your struct behaves like a Vector3 but adds new functionality, you should strongly consider using extension methods or helper functions instead. For example, let's imagine you want a way to swap the X and Y values for some reason. One way to do this is with extension methods:

public static class ExtensionMethods {
    public static void SwapXY(ref this Vector3 v3) {
        float temp = v3.x;
        v3.x = v3.y;
        v3.y = temp;
    }
}

This adds a new function to Vector3 which we can use like any other function! Note that I used the ref keyword because Vector3 is a value type and in this example we're modifying the original value.

Vector3 test = new Vector3(1, 2, 3);
test.SwapXY();
Debug.Log(test); //(2.0, 1.0, 3.0)

Alternatively, we can use static helper functions:

public static class Vector3Helpers {
    public Vector3 SwapXY(Vector3 value) {
        return new Vector3(value.y, value.x, value.z);
    }
}

So when should you consider creating your own struct?

  • To make the struct immutable. Vector3 is mutable (its fields can be changed after it is created). Mutable structs are generally discouraged in C#, but Unity makes extensive use of mutable structs for debatable reasons I won't get into here.
  • For type safety. For example, if a function argument expects a position and you want to be sure you don't accidentally pass in a Vector3 representing euler angles, you could make a Position struct that's otherwise identical to Vector3.
  • If you want to change how Vector3's existing functions work (I can't think of any reason you'd want to do this)
  • If you want to add new fields, or implement other new functionality that can't be accomplished with extension methods.
  • To make the struct serializable. That doesn't directly apply here because Vector3 is already serializable, but standard structs built into C# like TimeSpan are not compatible with Unity's serialization system.
  • To change the precision (e.g. use doubles instead of floats)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think this post has some useful resources, but to be an answer to the question that was asked, it should also at least briefly show how to use the CustomPropertyDrawer. \$\endgroup\$
    – DMGregory
    May 5 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .