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