- Home /
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?
Answer by WillNode · Dec 15, 2015 at 10:56 PM
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).
Answer by drchilitopicoso · Aug 07, 2016 at 01:24 PM
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.
Answer by wibble82 · Dec 15, 2015 at 01:52 PM
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:
http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/
If you want your mesh fully functional in the scene view then I would recommend this approach.
-Chris
the reason why i'm avoiding the $$anonymous$$eshFilter Simply because:
using
mesh
at edit mode will give you a warn about instantianting mesh at editorusing
shared$$anonymous$$esh
will be a trouble if you duplicating object in editor (the mesh isn't instantiated along with $$anonymous$$eshFilter)There is no way to tell you if the mesh are has been instantiated
(maybe bit off) i see
Text$$anonymous$$esh
isn't using$$anonymous$$eshFilter
to do that job
maybe you can figuring out how to solve these problems, anyway i appreciate your time here.