Home > other >  How to draw a mesh as wire-frame in Unity, so it shows up in play-mode / build? (like with Gizmos.Dr
How to draw a mesh as wire-frame in Unity, so it shows up in play-mode / build? (like with Gizmos.Dr

Time:07-21

I used Gizmos.DrawMesh in unity but when I build the program, I can't see any colors. My code:

private void OnDrawGizmos()
{
    if(mesh)
    {
        Gizmos.color = Color.red;
        Gizmos.DrawMesh(mesh, transform.position, transform.rotation);
    }
}

When I built the app, the colors I coded didn't show up. I'm wondering if Gizmos can only be used when I view it in the unity editor. when built, it is no longer usable.

Why aren't these colors showing up?

CodePudding user response:

Yes, Gizmos are only visible in the editor. Maybe this can help you out: https://github.com/popcron/gizmos

Edit:
Sorry I did not see that you want to use DrawMesh(), that's not supported by that lib. (if you want to use it anyway you can add it through the package manager "add from url" and insert "com.popcron.gizmos")

Are you creating the mesh in code? If so, you could use a line mesh: https://docs.unity3d.com/ScriptReference/MeshTopology.html

It should also be possible to convert a triangle mesh into a line mesh without too much trouble.

Edit2:
(I think this is overkill and does not offer much configuration) I wrote a class that adds a context menu to the component "Mesh Filter". Place it in a folder named "Editor" and you should be able to right-click on a MeshFilter and convert it with "Convert To Line Mesh...". (The line mesh is very basic without a custom shader, so you wont get anything like line thickness)

If I have time I'll write a script that converts the lines to two triangles instead, that should then solve the issue.

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public static class MeshConverterEditor
{

    [MenuItem("CONTEXT/MeshFilter/Convert To Line Mesh...")]
    public static void ConvertToLineMesh(MenuCommand menuCommand)
    {
        MeshFilter mf = menuCommand.context as MeshFilter;
        Mesh m = mf.sharedMesh;
        if (m.GetTopology(0) != MeshTopology.Triangles)
        {
            Debug.LogError("Can only convert triangle meshes.");
            return;
        }
        mf.sharedMesh = ConvertToLineMesh(m);
    }

    public static Mesh ConvertToLineMesh(Mesh mesh)
    {
        var lineMesh = new Mesh();
        var tris = mesh.triangles;
        List<int> lineIndices = new List<int>(tris.Length * 2);
        for (int i = 0; i < tris.Length; i  = 3)
        {
            lineIndices.Add(tris[i]);
            lineIndices.Add(tris[i   1]);

            lineIndices.Add(tris[i   1]);
            lineIndices.Add(tris[i   2]);

            lineIndices.Add(tris[i   2]);
            lineIndices.Add(tris[i]);
        }
        lineMesh.vertices = mesh.vertices;
        lineMesh.uv = mesh.uv;
        lineMesh.normals = mesh.normals;
        lineMesh.tangents = mesh.tangents;
        lineMesh.SetIndices(lineIndices, MeshTopology.Lines, 0, true);
        return lineMesh;
    }
}

Edit3:

I found a very simple and nice looking library: https://github.com/Chaser324/unity-wireframe

Just copy the folders:

  • unity-wireframe-master\Assets\Wireframe\Shaders
  • unity-wireframe-master\Assets\Wireframe\Examples\Materials

into your project, drag a material on your GameObject and you should be good.

  • Related