Unity用GL庫畫線


真機調試時,有時候需要在屏幕中快速添加一些調試信息,如人物的碰撞盒, 准星范圍等,可用gl在屏幕上畫矩形圓形等來顯示:

 

開始繪制:

void Begin (Color color)
{
    if (s_material == null)
        s_material = new Material (Shader.Find ("Unlit/Color"));
        s_material.SetPass (0);
        GL.LoadOrtho ();
        GL.Begin (GL.LINES);
        s_material.SetColor ("_Color", color);
}

結束繪制:

void End ()
{
    GL.End ();
}

 

繪制矩形:

public void DrawScreenRect(Rect rect, Color color)
{
    Begin (color);

    GL.Vertex3 (rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
    GL.Vertex3 (rect.xMin / Screen.width, rect.yMax / Screen.height, 0);

    GL.Vertex3 (rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
    GL.Vertex3 (rect.xMax / Screen.width, rect.yMax / Screen.height, 0);

    GL.Vertex3 (rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
    GL.Vertex3 (rect.xMax / Screen.width, rect.yMin / Screen.height, 0);

    GL.Vertex3 (rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
    GL.Vertex3 (rect.xMin / Screen.width, rect.yMin / Screen.height, 0);

    End ();
}

 

繪制橢圓:

public void DrawScreenEllipse(Vector2 center, float xRadius, float yRadius, Color color, int smooth = 50)
{
    Begin (color);
    for (int i = 0; i < smooth; ++i)
    {
        int nextStep = (i + 1) % smooth;
        GL.Vertex3 ((center.x + xRadius * Mathf.Cos (2 * Mathf.PI / smooth * i)) / Screen.width,
        (center.y + yRadius * Mathf.Sin (2 * Mathf.PI / smooth * i)) / Screen.height, 0);
        GL.Vertex3 ((center.x + xRadius * Mathf.Cos (2 * Mathf.PI / smooth * nextStep)) / Screen.width,
        (center.y + yRadius * Mathf.Sin (2 * Mathf.PI / smooth * nextStep)) / Screen.height, 0);
    }

    End ();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM