I’m trying to draw buttons in the scene view and in-scene stuff for a level editor. I have a level editor that’s a monobehaviour and I’m doing my drawing in OnSceneGUI. I’m totally happy to do it somewhere else with the editor as a different kind of object if that’s how it should be done! But I don’t know what that way is so I’m doing it this way right now.
I have a bunch of meshes, let’s say 5 meshes, and I want to draw a button for each mesh and then draw the mesh on the button. The AssetPreview.GetAssetPreview function is nice, but it is rendering my objects out as black silhouettes so I want to use DrawMeshNow with a material to draw them properly. I also want to draw the currently selected mesh (whatever button was clicked last) at the mouse pointer in the scene view after drawing a collision ray into the screen.
Here’s my current OnSceneGUI, which I’ve boiled down to remove some of the stuff that isn’t relevant…
private void OnSceneGUI()
{
Handles.BeginGUI();
int y = 60;
int size = 100;
int scrollBarWidth = 24;
int fieldHeight = (int)SceneView.lastActiveSceneView.position.height - y - 40;
Vector2 scrol = GUI.BeginScrollView(new Rect(10, y, size + scrollBarWidth, fieldHeight), new Vector2(0, scroll), new Rect(0, 0, size, size * available.Count));
scroll = scrol.y;
int localY = 0;
int xGutter = 10;
foreach (var collectable in available)
{
if (localY > scroll- size && localY < fieldHeight + scroll + size)
{
if (GUI.Button(new Rect(xGutter, localY, size, size), AssetPreview.GetAssetPreview(collectable.prefab)))
{
selected = collectable;
}
Mesh mesh = collectable.prefab.GetComponent<MeshFilter>().sharedMesh;
collectable.prefab.GetComponent<MeshRenderer>().sharedMaterial.SetPass(0);
Bounds bounds = mesh.bounds;
Matrix4x4 matrix =
//Camera.current.worldToCameraMatrix;
//Matrix4x4.TRS(new Vector3(xGutter + size/2, localY + scroll + size/2, 0), Quaternion.identity, Vector3.one * 100 / mesh.bounds.size.magnitude);
//Matrix4x4.TRS(new Vector3(xGutter + size/2, localY + scroll + size/2, 0), Quaternion.identity, Vector3.one * 100 / mesh.bounds.size.magnitude);
//Matrix4x4.Ortho(-bounds.extents.x,bounds.extents.x,-bounds.extents.y,bounds.extents.y,-bounds.extents.z,bounds.extents.z);
//GUI.matrix;
//matrix *= Camera.current.worldToCameraMatrix.inverse;
Camera.current.worldToCameraMatrix.inverse;
matrix *= GUI.matrix.inverse;
matrix *= Matrix4x4.Ortho(-10,10,-10,10,-10,10);
matrix *= Matrix4x4.TRS(new Vector3(xGutter + size/2, localY + scroll + size/2, 0), Quaternion.identity, Vector3.one * 100 / mesh.bounds.size.magnitude);
matrix *= Matrix4x4.Scale(Vector3.one * 10);
Graphics.DrawMeshNow(mesh, matrix);
}
localY += size;
}
GUI.EndScrollView();
Handles.EndGUI();
}
That’s just for the buttons, I’m happy to just sort the buttons first. I got the current camera’s inverse matrix idea from @Nothke and that’s really good, it gets rid of the scene view camera’s rotation, but there still seems to be a GUI matrix involved. When I scroll my scroll view the meshes I’m drawing move with it. Setting the GUI matrix to identity does nothing. Multiplying with inverse of gui matrix doesn’t seem to work either. Same with the handles matrix and the GL matrix. What matrices are left to destroy?? How can I control the rendering at this particular time?