I'm trying to show a two-dimensional array in the editor like the "Layer Collision Matrix" is shown in Unity:
Though instead of checkboxes I need ints, and a full grid (not the triangle shape.) I can't seem to figure out how to do this though... I can get a custom editor, though making the grid fails. So, is there any way I can see the code of the Physics Manager's editor (the Layer Collision Matrix is in there) or maybe someone knows a good way to do this?
Note: Preferred language is c#, though any will do.
Thanks.
Setting up layer-based collision detectionIn the Unity menu bar, go to Edit > Project Settings > Physics to open the Physics Manager window. Select which layers on the Collision Matrix will interact with the other layers by checking them.
In the bottom of the physics window, you will find a Layer Collision Matrix. In our example we want to remove the interaction between the Sword collider and the Player Collider, so we uncheck the Sword/Player layer collision detection.
The Layer Collision Matrix allows you to control which GameObjects can collide and interact with others based on the layers to which they are assigned. This means you can have certain objects able to collide only with specific ones if desired.
The code involved in what you're looking for is pretty complex, so I'm going to rely on you to know what I'm talking about here. The Unity docs are your friend.
Create a GUIArea
where your special editor tool will be. Inside it, place a
function call that will then call the other rendering functions. I
suspect you'll want to do some encapsulation here. Encapsulating the gui in another function will allow you to duplicate its functionality (within the limits of your abstraction) and move things around on screen more easily.
Design three anchor points (Vector2
). They should each represent the top-left
coord of your row labels, column labels, and data field. Note that
the columns anchor needs to be directly above (same x-value as) the
row anchor, since rotating (next step) will transform the anchor.
Use GUIUtility.RotateAroundPivot()
to rotate the GUI transform
matrix by 90° around the column anchor point.
Write a long GUI.Label
(or several of them) for your labels.
Anchor them at the column anchor. Per the image above, your label
string could read something like
"Default\nTransparentFX\nIgnoreRaycast\nWater" where the \n creates
a newline.
Rotate again, -90° back to the original matrix. Alternatively, you
can copy GUI.matrix
prior to step 3, then assign it back for a
guaranteed matrix reset. Rotating back and forth may have some error due to floating-point and other imprecision.
Write labels for the rows. Same method as two steps back. Anchor them at the row anchor.
This is the harder part. Iterate through your data field, creating a
small EditorGUI.IntField()
or StringField()
or even
ObjectField()
for each element in your data. Each element will
need its own anchor, which is then summed with the data field anchor.
If your data field is a square 2D array, deriving the anchors will
be easy – though you'll also have empty elements in your array (if
you want the exact functionality described above). If you want to
conserve memory, you'll have to transform the element indexes into
coordinates using some tricky math. I'm not sure off the top of my
head how I'd do it.
Hope I'm not forgetting anything. Unity's GUI is a b----. Comment and I'll do my best to help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With