18.EditorGUIUtility-绘制色板绘制曲线
18.1 知识点
绘制色板
在指定区域绘制一个色板矩形
EditorGUIUtility.DrawColorSwatch(Rect 绘制色板的矩形, Color 颜色);
主要配合 EditorGUILayout.ColorField 颜色输入控件使用
private Color color;
private void OnGUI()
{
// 绘制色板
// 一般配合选取颜色控件使用
color = EditorGUILayout.ColorField(new GUIContent("选取颜色"), color, true, true, true);
// 默认颜色,方便选取颜色
EditorGUIUtility.DrawColorSwatch(new Rect(180, 180, 30, 30), Color.blue);
}
绘制曲线
在指定区域绘制曲线
EditorGUIUtility.DrawCurveSwatch(Rect 绘制曲线的范围,
AnimationCurve 曲线,
SerializedProperty 要绘制为SerializedProperty的曲线,
Color 绘制曲线的颜色,
Color 绘制背景的颜色);
private AnimationCurve curve = new AnimationCurve();
private void OnGUI()
{
// 绘制曲线
// 配合 EditorGUILayout.CurveField 曲线输入控件显示当前显示的曲线
curve = EditorGUILayout.CurveField("曲线设置", curve);
EditorGUIUtility.DrawCurveSwatch(new Rect(0, 300, 100, 80), curve, null, Color.red, Color.white);
}
更多API
官方文档:EditorGUIUtility
18.2 知识点代码
Lesson18_EditorGUIUtility_绘制色板绘制曲线
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson18_EditorGUIUtility_绘制色板绘制曲线 : MonoBehaviour
{
void Start()
{
#region 知识点一 绘制色板
//在指定区域绘制一个色板矩形
//EditorGUIUtility.DrawColorSwatch(Rect 绘制色板的矩形, Color 颜色);
//主要配合 EditorGUILayout.ColorField 颜色输入控件使用
#endregion
#region 知识点二 绘制曲线
//在指定区域绘制曲线
//EditorGUIUtility.DrawCurveSwatch(Rect 绘制曲线的范围,
// AnimationCurve 曲线,
// SerializedProperty 要绘制为SerializedProperty的曲线,
// Color 绘制曲线的颜色,
// Color 绘制背景的颜色);
//主要配合 EditorGUILayout.CurveField 曲线输入控件使用
#endregion
#region 知识点三 更多API
// 官方文档:https://docs.unity3d.com/ScriptReference/EditorGUIUtility.html
#endregion
}
}
MyEditorGUIUtilityLearnWindow
using System;
using UnityEditor;
using UnityEngine;
public class MyEditorGUIUtilityLearnWindow : EditorWindow
{
[MenuItem("编辑器拓展教程/MyEditorGUIUtilityLearnWindow")]
private static void OpenMyEditorGUIUtilityLearnWindow()
{
MyEditorGUIUtilityLearnWindow win =
EditorWindow.GetWindow<MyEditorGUIUtilityLearnWindow>("EditorGUIUtility学习面板");
//win.titleContent = new GUIContent("EditorGUIUtility学习面板");
win.Show();
}
#region Lesson14_EditorGUIUtility_资源加载
private Texture img;
private Texture img2;
#endregion
#region Lesson15_EditorGUIUtility_搜索框查询对象选中提示
private Texture img3;
#endregion
#region Lesson18_EditorGUIUtility_绘制色板绘制曲线
private Color color;
private AnimationCurve curve = new AnimationCurve();
#endregion
private void OnGUI()
{
#region Lesson14_EditorGUIUtility_资源加载
//加载资源(如果资源不存在返回null)
if (GUILayout.Button("加载编辑器图片资源"))
img = EditorGUIUtility.Load("head.png") as Texture;
if (img != null)
GUI.DrawTexture(new Rect(0, 50, 160, 90), img);
//加载资源(如果资源不存在会直接报错)
if (GUILayout.Button("加载编辑器图片资源(如果资源不存在会直接报错)"))
img2 = EditorGUIUtility.LoadRequired("head.png") as Texture;
if (img2 != null)
GUI.DrawTexture(new Rect(0, 150, 160, 90), img2);
#endregion
#region Lesson15_EditorGUIUtility_搜索框查询对象选中提示
//搜索框查询
if (GUILayout.Button("打开搜索框查询窗口"))
{
EditorGUIUtility.ShowObjectPicker<Texture>(null, true, "head", 0);
}
if (Event.current.commandName == "ObjectSelectorUpdated")
{
img3 = EditorGUIUtility.GetObjectPickerObject() as Texture;
if (img3 != null)
Debug.Log(img3.name);
}
else if (Event.current.commandName == "ObjectSelectorClosed")
{
img3 = EditorGUIUtility.GetObjectPickerObject() as Texture;
if (img3 != null)
Debug.Log("窗口关闭 - " + img3.name);
}
//对象选中提示提示
if (GUILayout.Button("高亮选中对象"))
{
if (img3 != null)
EditorGUIUtility.PingObject(img3);
}
#endregion
#region Lesson16_EditorGUIUtility_窗口事件传递坐标转换
//窗口事件传递
if (GUILayout.Button("传递事件"))
{
//声明事件
Event e = EditorGUIUtility.CommandEvent("韬老狮的事件");
MyEditorGUILearnWindow win = EditorWindow.GetWindow<MyEditorGUILearnWindow>();
win.SendEvent(e);
}
//坐标转换
if (GUILayout.Button("坐标转换测试"))
{
Vector2 v = new Vector2(10, 10);
Vector2 screenPos = EditorGUIUtility.GUIToScreenPoint(v);
GUI.BeginGroup(new Rect(10, 10, 100, 100));
//转换函数 如果包裹在布局相关函数中 那么位置要加上布局的偏移 再进行转换
Vector2 screenPos2 = EditorGUIUtility.GUIToScreenPoint(v);
GUI.EndGroup();
Debug.Log("GUI:" + v + "Screen:" + screenPos + "布局内坐标:" + screenPos2);
}
#endregion
#region Lesson17_EditorGUIUtility_指定区域使用对应鼠标指针
EditorGUI.DrawRect(new Rect(0, 180, 100, 100), Color.green);
EditorGUIUtility.AddCursorRect(new Rect(0, 180, 100, 100), MouseCursor.FPS);
#endregion
#region Lesson18_EditorGUIUtility_绘制色板绘制曲线
//绘制色板
//一般配合选取颜色控件使用
color = EditorGUILayout.ColorField(new GUIContent("选取颜色"), color, true, true, true);
//默认颜色 方便选取颜色
EditorGUIUtility.DrawColorSwatch(new Rect(180, 180, 30, 30), Color.blue);
//绘制曲线
//配合 EditorGUILayout.CurveField 曲线输入控件显示当前显示的曲线
curve = EditorGUILayout.CurveField("曲线设置", curve);
EditorGUIUtility.DrawCurveSwatch(new Rect(0, 300, 100, 80), curve, null, Color.red, Color.white);
#endregion
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com