Home > Net >  Today I drew with Gizmos.Draw Mesh in unity but when I build the program, I can't see any color
Today I drew with Gizmos.Draw Mesh in unity but when I build the program, I can't see any color

Time:07-19

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

here is my function code. 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. Need everyone's opinion

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 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;
    }
}
  • Related