Unity编辑器 - 使用GL绘制控件

本文介绍如何在Unity编辑器中使用GL来绘制控件以提高复杂控件的绘制性能。通过具体示例——绘制垂直线段,展示了GL绘制的基本用法,并提供了跨平台实现的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unity编辑器 - 使用GL绘制控件

控件较为复杂时,可能造成界面卡顿,在EditorGUI中也可以灵活使用GL绘制来提升性能。
以绘制线段为例:
这里写图片描述

using UnityEngine;
using UnityEditor;

public class EditorGL {
    private static Material _sLineMat;

    static EditorGL() {
        Shader shader = Shader.Find("Hidden/Internal-Colored");
        _sLineMat = new Material(shader);
        _sLineMat.hideFlags = HideFlags.HideAndDontSave;
        _sLineMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
        _sLineMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
        _sLineMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
        _sLineMat.SetInt("_ZWrite", 0);
    }

    public static void DrawVerticalLine(float x, float minY, float maxY, Color color) {
        if (Event.current.type == EventType.Repaint) {
            Color color2 = Handles.color;
            _sLineMat.SetPass(0);
            if (Application.platform == RuntimePlatform.WindowsEditor) {
                GL.Begin(7);
            }
            else {
                GL.Begin(1);
            }
            DrawVerticalLineFast(x, minY, maxY, color);
            GL.End();
            Handles.color = color2;
        }
    }

    private static void DrawVerticalLineFast(float x, float minY, float maxY, Color color) {
        if (Application.platform == RuntimePlatform.WindowsEditor) {
            GL.Color(color);
            GL.Vertex(new Vector3(x - 0.5f, minY, 0f));
            GL.Vertex(new Vector3(x + 0.5f, minY, 0f));
            GL.Vertex(new Vector3(x + 0.5f, maxY, 0f));
            GL.Vertex(new Vector3(x - 0.5f, maxY, 0f));
        }
        else {
            GL.Color(color);
            GL.Vertex(new Vector3(x, minY, 0f));
            GL.Vertex(new Vector3(x, maxY, 0f));
        }
    }
}

转载于:https://www.cnblogs.com/CloudLiu/p/10746062.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值