Editor扩展常用API


如图

 

效果图

 

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
public class Mybianyi : EditorWindow
{
    string PasswordField = "";
    string m_textArea = "";
    float sliders = 0;
    int slidera = 0;
    string BeginToggleGroup = "BeginToggleGroup";
    bool ToggleGroup = false;
    string Textfield = "";
    bool fg = false;
    float sum = 0;
    int count = 0;
    string tag = "aaa";
    int Layerfield=0;
    string[] pathname = new string[] { "All", "Asset", "..." }; 
    float minVal = 1;
    float maxVal = 2;
    float minLimit = -5;
    float maxLimit = 5;
    static Vector3 center = new Vector3(1, 2, 3);
    static Vector3 size1 = new Vector3(1, 2, 3);
    Bounds _bounds = new Bounds(center, size1);
    Color m_color = Color.white;
    AnimationCurve m_curve = AnimationCurve.Linear(0, 0, 10, 10);
    Vector2 size = new Vector2(100,100);
    int flags = 0;
    string[] options = new string[] { "CanJump", "CanShoot", "CanSwim" ,"Canabc","Canacc"};
    GameObject game ;
    bool showFoldout;
    Vector2 m_vector2 = new Vector2();
    Vector3 m_vector3 = new Vector3();
    Vector4 m_vector4 = new Vector4();
    Transform selectedTransform;
    GameObject selectedGameObject;
    bool fold;
    bool fold2;
    [MenuItem("MyWindow/Window")]
    static void window()
    {
        Mybianyi mybianyi = GetWindow<Mybianyi>();
        mybianyi.Show();
    }
    private void OnGUI()
    {
     
        // GUILayout.Width  控制在窗口中物体所在的宽
        // GUILayout.Height  控制在窗口中物体所在的高
        //它们的返回的类型为GUILayoutOption
        #region GUILayout.Label 提示语句
 
        GUILayout.Label("我的编译器(My compiler)", GUILayout.Width(50), GUILayout.Height(10)); //提示语句
        #endregion
        #region GUILayout.Button( 按钮
        GUILayout.Label("按钮");
        if( GUILayout.Button("按钮", GUILayout.Width(40), GUILayout.Height(40)))
        {
 
        }
        #endregion
        #region GUILayout.TextField 文本
        GUILayout.Label("文本(可以输入)");
        Textfield = GUILayout.TextField(Textfield);//单行
        //参数2 maxLength 最大有效长度
        // Textfield = GUILayout.TextField(Textfield,5);
        #endregion
        #region GUILayout.Space 空行
        //参数为float类型代表空行的距离
        GUILayout.Space(10);
        #endregion
        #region EditorGUILayout.Toggle 开关(跟Toggle一样)
        fg = EditorGUILayout.Toggle("Toggle", fg);//开关
        #endregion
        #region GUILayout.BeginHorizontal 横向
        GUILayout.BeginHorizontal();//可以在里面存放多个如果不规定大小系统会平均分配大小
        GUILayout.Button("按钮");
        Textfield = GUILayout.TextField(Textfield);
        GUILayout.EndHorizontal();//结束语一定要有
        #endregion
        #region GUILayout.BeginVertical 纵向
        GUILayout.BeginVertical();//可以在里面存放多个如果不规定大小系统会平均分配大小
        GUILayout.Button("按钮");
        Textfield = GUILayout.TextField(Textfield);
        GUILayout.EndVertical();//结束语一定要有
        #endregion
        #region  GUILayout.HorizontalSlider(横) GUILayout.VerticalSlider(纵)  Slider(分横纵 上横下纵)
        sum = GUILayout.HorizontalSlider(sum, 0, 10);
        // sum = GUILayout.VerticalSlider(sum, 0, 10);
        GUILayout.Space(20);
        #endregion
        #region EditorGUILayout.Popup  下拉
        count = EditorGUILayout.Popup("下拉:",count,pathname);
        #endregion
        #region GUILayout.BeginScrollView  滑动列表  
        //两个true可以让横纵两条线显示出了
        //两个false可以让横纵两条线不显示出来
        size = GUILayout.BeginScrollView(size,true,true);
        
        GUILayout.EndScrollView();
        #endregion
        #region EditorGUILayout.BoundsField (边界输入)   EditorGUILayout.ColorField(颜色输入)  EditorGUILayout.CurveField(曲线输入)   输入框
        //BoundsField 边界输入框
        _bounds = EditorGUILayout.BoundsField("BoundsField:", _bounds);
        //ColorField 颜色输入框
        m_color = EditorGUILayout.ColorField("ColorField:", m_color);
        //CurveField 曲线输入框
        m_curve = EditorGUILayout.CurveField("CurveField:", m_curve);
        #endregion
        #region EditorGUILayout.TagField    tag(标签)
        tag = EditorGUILayout.TagField("TagField:", tag);
 
        #endregion
        #region   EditorGUILayout.LayerField(可以获取所有的Layer)
        //Layerfield 可以获取所有的Layer
        Layerfield = EditorGUILayout.LayerField("LayerField:", Layerfield);
 
        #endregion
        #region   EditorGUILayout.MaskField (下拉可以多选)
        flags = EditorGUILayout.MaskField("MaskField:", flags, options);
        //Debug.Log(flags);//      系统默认会添加Nothing (对应的值0) 和Everything(-1)
        #endregion
        #region EditorGUILayout.ObjectField(选择物体)
        game = (GameObject) EditorGUILayout.ObjectField(game,typeof(GameObject),true);//typeof(类型) 确定好类型系统会自动帮我找到所有的关于这个类型的物体
        #endregion
        #region  EditorGUILayout.Foldout 折叠
        showFoldout = EditorGUILayout.Foldout(showFoldout, "折叠子物体:");
        if (showFoldout)
        {
            EditorGUI.indentLevel++;//缩进级别
            EditorGUILayout.LabelField("折叠块内容1");
            EditorGUI.indentLevel++;
            EditorGUILayout.LabelField("折叠块内容2");
            EditorGUI.indentLevel--;
            EditorGUI.indentLevel--;
            EditorGUILayout.LabelField("折叠块内容3");
        }
        #endregion
        #region EditorGUILayout.BeginToggleGroup(开关)
        ToggleGroup = EditorGUILayout.BeginToggleGroup(BeginToggleGroup, ToggleGroup);
            Textfield = GUILayout.TextField(Textfield);
        EditorGUILayout.EndToggleGroup();
        #endregion
        #region  GUILayout.FlexibleSpace(布局之间左右对齐)
        EditorGUILayout.BeginHorizontal();//开始最外层横向布局
        GUILayout.FlexibleSpace();//布局之间左右对齐
        GUILayout.Label("-----------------分割线-----------------");
        GUILayout.FlexibleSpace();//布局之间左右对齐
        EditorGUILayout.EndHorizontal();
        #endregion
        #region  EditorGUILayout.HelpBox(提示语句)
        EditorGUILayout.HelpBox("HelpBox Error:", MessageType.Error);//红色错误号
        EditorGUILayout.HelpBox("HelpBox Info:", MessageType.Info);//白色提示号
        EditorGUILayout.HelpBox("HelpBox None:", MessageType.None);//解释号
        EditorGUILayout.HelpBox("HelpBox Warning:", MessageType.Warning);//黄色警告号
        #endregion
        #region EditorGUILayout.Slider(Slider)
        sliders = EditorGUILayout.Slider("Slider:",sliders,0,10);
        #endregion
        #region  EditorGUILayout.TextArea(text 自适应高)
        m_textArea = EditorGUILayout.TextArea(m_textArea);//可以多行
        #endregion
        #region GUILayout.PasswordField(可以改变成对应的符号)
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("密码text", GUILayout.Width(60));
        PasswordField = GUILayout.PasswordField(PasswordField, '*');//可以改变成对应的符号
        EditorGUILayout.EndHorizontal();
        #endregion
        #region  EditorGUILayout.Vector2Field  EditorGUILayout.Vector3Field  EditorGUILayout.Vector4Field  
        m_vector2 = EditorGUILayout.Vector2Field("Vector2:", m_vector2);
        m_vector3 = EditorGUILayout.Vector3Field("Vector3:", m_vector3);
        m_vector4 = EditorGUILayout.Vector4Field("Vector4:", m_vector4);
        #endregion
        #region EditorGUILayout.SelectableLabel (可以复制粘贴)
        EditorGUILayout.SelectableLabel("SelectableLabel");
        #endregion
        #region  EditorGUILayout.MinMaxSlider (取值范围)
        EditorGUILayout.LabelField("Min Val:", minVal.ToString());
        EditorGUILayout.LabelField("Max Val:", maxVal.ToString());
        EditorGUILayout.MinMaxSlider("MinMaxSlider", ref minVal, ref maxVal, minLimit, maxLimit);
                                                     //现在最小  现在最大 最小长度 最大长度      
        #endregion
        #region   EditorGUILayout.IntSlider(只能是整数)
        slidera = EditorGUILayout.IntSlider("IntSlider:", slidera, 1, 10);
        #endregion
        #region  EditorGUILayout.InspectorTitlebar(将物体返回回来)
        //Transform selectedTransform = Selection.activeGameObject.transform;
        //GameObject selectedGameObject = Selection.activeGameObject;//选择物体(GameObject)
        //fold = EditorGUILayout.InspectorTitlebar(fold, selectedTransform);
        //fold2 = EditorGUILayout.InspectorTitlebar(fold2, selectedGameObject);
        #endregion
    }
}


原文链接:https://blog.csdn.net/qq_57896821/article/details/121264618

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
Unity Editor扩展是指通过编写代码来扩展Unity编辑器的功能。通过创建自定义的编辑器窗口、工具栏按钮、菜单项和面板等,开发者可以为自己的项目添加一些定制化功能,以提高开发效率和用户体验。 Unity提供了一套API来实现编辑器扩展,开发者可以利用这些API去创建自定义的编辑器界面。首先,我们需要创建一个继承自EditorWindow或Editor类的脚本,然后在这个脚本中实现我们想要的功能。比如,我们可以在自定义的编辑器窗口中创建一些GUI元素,如按钮、文本框、下拉菜单等,用于控制场景中的对象、调整参数或执行特定的操作。 另外,Unity还提供了一些常用的工具类,如SerializedObject、SerializedProperty等,以便开发者可以访问和修改Unity对象的属性。使用这些工具类,我们可以在编辑器扩展中实现对象的序列化、反序列化和检查等功能。 在开发过程中,Unity的编辑器扩展还可以与自定义的脚本进行交互。通过注册自定义的菜单项、工具栏按钮或快捷键,我们可以在编辑器中快速调用脚本的功能,并在自定义界面中显示脚本的运行结果。 总结来说,Unity Editor扩展是一种强大的工具,它可以帮助开发者提高开发效率和用户体验。通过编写代码,我们可以创建出各种各样的自定义编辑器界面,以满足不同项目的需求。无论是增加交互性、优化工作流程还是增加特定的功能,Unity的编辑器扩展都能提供灵活和强大的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值