Make Graphics.DrawMesh clickable at scene editor

Hi, basically i want to make a fully customized procedural mesh for my project, so instead of using MeshFilter component, i’m using Graphics.DrawMesh for fulfilling my needs.

[ExecuteInEditMode]
public class MyMesh : MonoBehaviour
{
   void Update()
   {
       Graphics.DrawMesh(mesh,transform.localToWorldMatrix,material,0);
   }
}

but here’s the problem, since i’m not touching anything about MeshFilter and Graphics.DrawMesh only do the drawing of my mesh, the scene window will simply not recognize selection of this mesh (i.e. my mesh isn’t selectable on scene window), so i only can select them from hierarchy window.

how i can make Graphics.DrawMesh selectable on scene view?

You can use Gizmos, but not in the usual way. Use the [DrawGizmo()] attribute. You can pass it many types or options(check the documentation). I like to have a class placed in the Editor folder dedicated just for this.

public static class EditorGizmos
{
    [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.Pickable | GizmoType.Selected)]
    static void ExampleGizmo(TYPEOFACOMPONENT component, GizmoType type)
    {
        bool selected = (type & GizmoType.Selected) > 0;
         //You can make it always transparent if you want so it's not intrusive
        var col = new Color(0.0f, 0.7f, 1f, 1.0f);
        col.a = selected ? 0.3f : 0.05f;  //change color when selected to highlight
        Gizmos.matrix = component.gameObject.transform.localToWorldMatrix;
        Gizmos.color = col;
        Gizmos.DrawCube(Vector3.zero, Vector3.one * someSizeOrTheScaleValueFromTransform);
    }

}

Then you can simply click on the cube and the gameobject will be selected. When making custom stuff with Graphics.DrawMesh i recommend having a Bounds variable somewhere in the class so you can customize the selection area further if you want. Or use the bounds of the mesh.

I realise this isn’t specifically an answer to your question, but why do you not want to use MeshFilter? You can still set the mesh that MeshFilter uses. I ask because that is without a doubt the simplest way to make it still selectable in unity.

Quick tutorial here just in case you’d missed the fact that this was possible:

If you want your mesh fully functional in the scene view then I would recommend this approach.

-Chris

waking up from my bed, and came-up with idea like this:

 public class MyMesh : MonoBehaviour
 {
   Mesh m; //Keep it private to not be included when instantiante
   void OnValidate()
   {
        UpdateMesh();
   }
    void Update()
    {
        UpdateMesh();
    }
    void UpdateMesh()
    {
          //Do rest of the procedural code here
          GetComponent<MeshFilter>().mesh = m;
     }
 }

this snippet code is solving to my specific case (already mention that in comment).