Unity 画线

1、Debug.Draw

一般在Update/Fixed Update/LateUpdate里调用
只在Scene窗口里显示
并且不能设置材质
void Update()
{
    Debug.DrawLine (worldPos1, worldPos2,Color.yellow);//直线
    Debug.DrawRay(worldPos1, worldPos2, Color.yellow);//射线
}

2、Gizmos.Draw 

在OnDrawGizmos /OnDrawGizmosSelected里调用
只在Scene窗口里显示
并且不能设置材质

//draw line
public void OnDrawGizmosSelected() 
{
    Gizmos.DrawLine(Vector3.zero, new Vector3(0,3f,0));
}


    //draw cycle
    void OnDrawGizmosSelected()
    {
        float m_radius = 0.5f;
        Vector3 centerPoint = m_center;
        Vector3 firstPoint = Vector3.zero;
        Vector3 beginPoint = Vector3.zero;
        for (float radian = 0; radian < 2 * Mathf.PI; radian += 0.1f)
        {
            float x = m_radius * Mathf.Cos(radian);
            float z = m_radius * Mathf.Sin(radian);
            Vector3 endPoint = new Vector3(x, 0, z);
            if (radian == 0)
            {
                firstPoint = endPoint;
            }
            else
            {
                Gizmos.DrawLine(beginPoint + centerPoint, endPoint + centerPoint);
            }
            beginPoint = endPoint;
        }
        Gizmos.DrawLine(firstPoint + centerPoint, beginPoint + centerPoint);
    }

3、Graphic.DrawMesh 

一般在Update/Fixed Update/LateUpdate里调用
实际屏幕和Scene窗口都能显示
可以设置材质

public Mesh mesh;
public Material material;

void Update()
{
    Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0);
}

4、GL

一般在物体的OnRenderObject 或者相机的OnPostRender里调用
实际屏幕和Scene窗口都能显示
可以设置材质

一个GL.Begin/GL.End里的渲染是自动合并的,一般是一个Drawcall
画一些线,三角可以。用GL.TRIANGLES 显示整个Mesh的话会超卡。
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class DrawRectOperate : MonoBehaviour
{
    static DrawRectOperate _instance;

    private Material m_material;
    private float3 m_startPos;
    private float3 m_endPos;

    private bool Drow = false;

    public static DrawRectOperate Instance()
    {
        return _instance;
    }

    private void Awake()
    {
        _instance = this;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Drow = true;
            m_startPos = GetScreenTouchPos();
        }

        if (Drow)
        {
            m_endPos = GetScreenTouchPos();
        }

        if (Input.GetMouseButtonUp(0))
        {
            Drow = false;
            m_startPos = 0;
            m_endPos = 0;
            DrawAreaByPos();
        }
    }

    private void OnRenderObject()
    {
        if(Drow)
        {
            DrawAreaByPos();
        }
    }

    void DrawAreaByPos()
    {
        DrawArea(new Rect(math.min(m_startPos.x, m_endPos.x), math.min(m_startPos.y, m_endPos.y),
            math.max(m_startPos.x, m_endPos.x) - math.min(m_startPos.x, m_endPos.x),
            math.max(m_startPos.y, m_endPos.y) - math.min(m_startPos.y, m_endPos.y)));
    }

    void DrawArea(Rect rect)
    {
        Begin();

        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();
    }

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

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

    public static Vector3 GetScreenTouchPos()
    {
        return GetTouchPosition();
    }

    public static Vector3 GetTouchPosition()
    {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        return Input.mousePosition;
#else
         return Input.touches[0].position;
#endif
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值