Unity Editor: Get Mesh when dragging a GameObject to input field

0 votes
asked May 22, 2015 in Programming by JD Student (300 points)


I want to have an object input field which should take a Mesh as input i should be able to directly drag a GameObject on.

void OnGUI(){
    mesh = (Mesh)EditorGUILayout.ObjectField("Mesh", mesh);

When i drag a game object from scene on this field the variable mesh stays null. How can i solve this?

1 Answer

0 votes
answered May 22, 2015 by Bugstacker Senior (4,800 points)

As the Mesh class is actually no component you have to do this via the MeshFilter component:


private MeshFilter filter;
private Mesh mesh;

void OnGUI()
    filter = EditorGUILayout.ObjectField("Mesh", filter, typeof(MeshFilter)) as MeshFilter;

    if(filter != null)
        mesh = filter.mesh;

        // Do some stuff with the mesh

Now you can simply drag any GameObject to your exposed field.

Welcome to Bugstacker Q&A, where you can ask questions concerning Game Development, Programming and Design and receive answers from other members of the community.

Help for