在unity脚本中控制Inspector面板的参数操作

作者:头号理想 时间:2023-07-22 11:39:58 

在编写脚本的过程中我们会遇到一些小问题

比如一个的变量 为了在其他脚本中可以调用 我们需要写成public类型的

这样的话在Inspector面板中会出现此变量 这篇博客会给大家介绍一些方法去避免这些小问题

1.[Header(" ")]

这个的作用是给它下面的所有变量一个总标题

2.[Tooltip("")]

这个的作用是给下面的第一行(紧邻的语句)注释

这个注释和双斜杠的注释不同

这个注释的效果是在unity中鼠标拖到变量的名字上 他会出现注释(括号的双引号中的注释)

在这里我给大家一个例子 让大家可以明白一点


[Header("GameObject")]
   [Tooltip(" * ")]
   private GameObject SmallGunPlayer;
   [Tooltip ("AK47"), SerializeField]
   private  GameObject BiglGunPlayer;

它的效果是

在unity脚本中控制Inspector面板的参数操作

然后把鼠标拖到变量上边的时候是

在unity脚本中控制Inspector面板的参数操作

大家对照脚本中的语句可以很轻易的理解了

3.[HideInInspector]

这个的用法是和在这篇博客开头的差不多

就是我们在一个不想出现在Inspector面板上出现的public变量

我们就在这个语句上添加[HideInInspector]即可

在unity的Inspector面板上就不会出现了

因为这个比较好理解 就不给例子了

4.[SerializeField]

这个和上面的语句相反 这个是private的变量

添加上这个语句之后的话

在unity的Inspector面板上可以改变其值 相当于public变量

不过private 具有一定的保护级别 这是和直接用public的区别

在这里最后提一下,以上语句可以一起用 效果可以重叠

具体效果在上面给出的代码和图片中可以发现

5.[Range( , )]

这个的用法是写在一个变量的前边(上边)

然后在unity的Inspector面板上就是一个类似于Slider的效果


[Range(2, 25)]
public int maxRandomValue = 5;

在unity脚本中控制Inspector面板的参数操作

6.[Serializable]

这个的作用是 我们在脚本中声明一个类的时候

我们可以把它其中的变量改成一个可以展开 折叠的变量

可能上面的描述不太准确 下面给大家一个例子


public Mydate date;
  [Serializable]
  public class Mydate
   {
       public  float q;
       public  float w;
       public  float m;
       public  float r;
   }

它的效果是

在unity脚本中控制Inspector面板的参数操作

在unity脚本中控制Inspector面板的参数操作

很容易就看懂了吧

在上边提到了类,在这里给一些小白朋友们介绍一下最简单的类在unity中的用法


[Serializable ]
   public class prefebs
   {
       public GameObject Bullet;
       public GameObject Robot;
       public Transform Position;
   }
   public prefebs Prefebs;
   private void Update
   {
   Prefebs.Robot.transform.Translate(transform.up*Time.deltatime*10);
   }

在上边很容易可以知道 在创建类之后在没在update start等中调用

通过上边介绍的所有方法可以制作出很多让你意想不到的效果

并且可以让你感觉这个真的好用

补充:Unity编辑器——Inspector 面板扩展

将EditorGUI 扩展在 Inspector 面板上

EditorGUI 和 GUI 的用法几乎完全一致,目前来说前者多用于编辑器开发,后者多用于发布后调试编辑器。总之,它们都是起辅助作用的。 EditorGUI 提供的组件非常丰富,常用的绘制元素包括文本、按钮、图片和滚动框等。做一个好的编辑器,是离不开 EditorGUI 的。如图:我们将 EditorGUI 拓展在 Inspector 面板上了,相关代码如下:

在unity脚本中控制Inspector面板的参数操作


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Script_03_23 : MonoBehaviour
{
   public Vector3 scrollPos;
   public int myId;
   public string myName;
   public GameObject prefab;
   public MyEnum myEnum = MyEnum.One;
   public bool toogle1;
   public bool toogle2;
   public enum MyEnum
   {
       One=1,
       Two,
   }

#if UNITY_EDITOR
   [CustomEditor(typeof(Script_03_23))]
   public class ScriptEditor_03_23 : Editor
   {
       private bool m_EnableToogle;
       public override void OnInspectorGUI()
       {
           //获取脚本对象
           Script_03_23 script = target as Script_03_23;

//绘制滚动条
           script.scrollPos =
               EditorGUILayout.BeginScrollView(script.scrollPos, false, true);
           script.myName = EditorGUILayout.TextField("text", script.myName);
           script.myId = EditorGUILayout.IntField("int", script.myId);

script.prefab = EditorGUILayout.ObjectField("GameObject", script.prefab,
               typeof(GameObject), true) as GameObject;

//绘制按钮
           EditorGUILayout.BeginHorizontal();
           GUILayout.Button("1");
           GUILayout.Button("2");
           script.myEnum = (Script_03_23.MyEnum)EditorGUILayout.EnumPopup("MyEnum:",
               script.myEnum);

EditorGUILayout.EndHorizontal();

//Toogle 组件
           m_EnableToogle = EditorGUILayout.BeginToggleGroup("EnableToogle",
               m_EnableToogle);
           script.toogle1 = EditorGUILayout.Toggle("toogle1", script.toogle1);
           script.toogle2 = EditorGUILayout.Toggle("toogle2", script.toogle2);
           EditorGUILayout.EndToggleGroup();

EditorGUILayout.EndScrollView();
       }
   }
}
#endif

EditorGUILayout —— 是EditorGUI 自动 布局版本。

EditorGUILayout.BeginScrollView —— 函数原型: public static Vector2 BeginScrollView( Vector2 scrollPosition, bool alwaysShowHorizontal, bool alwaysShowVertical, params GUILayoutOption[] options);

scrollPosition参数: 显式使用的位置

alwaysShowHorizontal —— 表示可选的参数,始终显示水平滚动条。如果为false或省略,那么则仅当ScrollView中的内容比ScrollView本身更宽时才会显示。

alwayShowVertical —— 表示可选的参数,始终显示垂直滚动条。如果为false或省略,那么只有当ScrollView中的内容比ScrollView本身高时才会显示。

EditorWindows 窗口

Unity 提供编辑器窗口,开发者可以自由拓展自己的窗口。 Unity 编辑器系统自带的视图窗口其实也是用 EditorWindows 实现的。如图所示,我们来制作一个简单的编辑窗口,它绘制元素时同样使用 EditorGUI 代码。

在unity脚本中控制Inspector面板的参数操作


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_03_24Window : EditorWindow
{

[MenuItem("Window/Open My Window")]
   static void Init()
   {
       Script_03_24Window window = (Script_03_24Window)EditorWindow.GetWindow(typeof(Script_03_24Window));
       window.Show();
   }

private Texture m_MyTexture = null;
   private float m_MyFloat = 0.5f;
   void Awake()
   {
       Debug.LogFormat("窗口初始化时调用");
       m_MyTexture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/unity1.png");
   }
   void OnGUI()
   {
       GUILayout.Label("Hello World!!", EditorStyles.boldLabel);
       m_MyFloat = EditorGUILayout.Slider("Slider", m_MyFloat, -5, 5);
       GUI.DrawTexture(new Rect(0, 30, 100, 100), m_MyTexture);
   }
   void OnDestroy()
   {
       Debug.LogFormat("窗口销毁时调用");
   }
   void OnFocus()
   {
       Debug.LogFormat("窗口拥有焦点时调用");
   }
   void OnHierarchyChange()
   {
       Debug.LogFormat("Hierarchy视图发生改变时调用");
   }
   void OnInspectorUpdate()
   {
       //Debug.LogFormat ("Inspector每帧更新");
   }
   void OnLostFocus()
   {
       Debug.LogFormat("失去焦点");
   }
   void OnProjectChange()
   {
       Debug.LogFormat("Project视图发生改变时调用");
   }
   void OnSelectionChange()
   {
       Debug.LogFormat("Hierarchy或者Project视图中选择一个对象时调用");
   }
   void Update()
   {
       //Debug.LogFormat ("每帧更新");
   }
}

EditorWindows 下拉菜单

如图 所示,在 EditorWindows 编辑窗口的右上角,有个下拉菜单,我们也可以对该菜单中的选项进行拓展,不过这里需要实现 IHasCustomMenu 接口。

在unity脚本中控制Inspector面板的参数操作


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_03_25Window : EditorWindow,IHasCustomMenu
{
   void IHasCustomMenu.AddItemsToMenu(GenericMenu menu)
   {
       menu.AddDisabledItem(new GUIContent("Disable"));
       menu.AddItem(new GUIContent("Test1"), true, () =>
       {
           Debug.Log("Test1");
       });

menu.AddItem(new GUIContent("Test2"), true, () =>
       {
           Debug.Log("Test2");
       });

menu.AddSeparator("Test/");
       menu.AddItem(new GUIContent("Test/Tes3"), true, () =>
       {
           Debug.Log("Tes3");
       });
   }
       [MenuItem("Window/TAOTAO My Window")]
   static void Init()
   {
       Script_03_25Window window = (Script_03_25Window)EditorWindow.GetWindow(typeof(Script_03_25Window));
       window.Show();
   }
}

上述代码中,我们通过 AddItem()方法来添加列表元素,并且监听选择后的事件。

预览窗口

选择游戏对象或者游戏资源后, Inspector 面板下方将会出现它的预览窗口,但是有些资源是没有预览信息的,不过我们可以监听它的窗口方法来重新绘制它,如图 所示,相关代码如下。

在unity脚本中控制Inspector面板的参数操作


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPreview(typeof(GameObject))]
public class Script_03_26 : ObjectPreview
{
   public override bool HasPreviewGUI()
   {
       return true;
   }
   public override void OnPreviewGUI(Rect r, GUIStyle background)
   {
       GUI.DrawTexture(r, AssetDatabase.LoadAssetAtPath<Texture>("Assets/Unity.png"));
       GUILayout.Label("Hello World!");
   }
}

获取预览信息

有些资源是有预览信息的,比如模型资源。在预览窗口中,我们可以看到它的样式。如果需要在自定义窗口中显示它,就需要获取它的预览信息。如图所示,选择一个游戏对象后,会在自定义窗口中显示它,相关代码如下:

在unity脚本中控制Inspector面板的参数操作


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_03_27window : EditorWindow
{
   private GameObject m_MyGo;
   private Editor m_MyEditor;

[MenuItem("Window/TAOSHUI Open My Window")]
   static void Init()
   {
       Script_03_27window window = (Script_03_27window)EditorWindow.GetWindow(typeof(Script_03_27window));
       window.Show();
   }
    void OnGUI()
   {
       //设置一个游戏对象
       m_MyGo = (GameObject)EditorGUILayout.ObjectField(m_MyGo,
           typeof(GameObject), true);
       if(m_MyGo !=null)
       {
           if(m_MyEditor==null)
           {
               //创建Editor 实例
               m_MyEditor = Editor.CreateEditor(m_MyGo);
           }
           //预览它
           m_MyEditor.OnPreviewGUI(GUILayoutUtility.GetRect(500, 500),
              EditorStyles.whiteLabel );
       }
   }
}

在上述代码中,预览对象首先需要通过 Editor.CreateEditor()拿到它的 Editor 实例对象,接着调用 OnPreviewGUI()方法传入窗口的显示区域。

来源:https://blog.csdn.net/weixin_44302602/article/details/92773579

标签:unity,Inspector面板,参数
0
投稿

猜你喜欢

  • Android P实现静默安装的方法示例(官方Demo)

    2022-04-05 20:06:13
  • 一文精通Java 多线程之全方位解读

    2022-10-23 19:15:51
  • WinForm项目开发中Excel用法实例解析

    2022-07-11 10:23:03
  • Android 媒体开发之MediaPlayer状态机接口方法实例解析

    2023-04-20 06:53:22
  • Java设计模式之备忘录模式实现对象状态的保存和恢复

    2023-08-28 15:22:27
  • jmeter添加自定函数的实例(jmeter5.3+IntelliJ IDEA)

    2023-04-14 00:20:54
  • c#实现选择排序的示例

    2021-10-15 00:32:27
  • C# 中将数值型数据转换为字节数组的方法

    2023-09-29 05:46:17
  • Java实现商城订单超时取消功能

    2023-09-17 06:20:42
  • SpringMVC中的handlerMappings对象用法

    2023-01-09 21:38:36
  • Flutter runApp到渲染上屏分析详解

    2023-06-27 12:09:45
  • java实现日历(某年的日历,某月的日历)用户完全自定义

    2023-04-17 15:14:29
  • C# 添加对System.Configuration.dll文件的引用操作

    2022-03-05 22:20:31
  • Bean Searcher配合SpringBoot的使用详解

    2022-06-21 23:49:00
  • 详解 C# 中XML对象的序列化和反序列化

    2022-06-24 12:55:27
  • Java进阶:Struts多模块的技巧

    2023-06-18 09:40:47
  • WPF在VisualTree上增加Visual

    2023-03-20 00:06:47
  • Java下载远程服务器文件到本地(基于http协议和ssh2协议)

    2022-08-29 12:23:18
  • Flutter Widgets之标签类控件Chip详解

    2023-06-26 14:22:35
  • java用applet画图用到的方法(涉及双缓冲)

    2021-07-09 17:27:32
  • asp之家 软件编程 m.aspxhome.com