首页 > 其他分享 >Unity IMGUI 快速上手【转】

Unity IMGUI 快速上手【转】

时间:2023-11-24 13:35:06浏览次数:32  
标签:control IMGUI GUI controls class will Unity 快速 event

文章转载自IMGUI crash course

Unity's IMGUI system is quite simple but very powerful. The main parts are:

  • The OnGUI callback. This callback is used to handle everything that has to do with GUI.
  • The Event class which is tightly connected to OnGUI.
  • The GUI controls defined in GUI, GUILayout and additional controls which are only available in the editor in EditorGUI and EditorGUILayout.
  • The GUIStyle class which defines how a single control looks like. It's actually responsible for any GUI drawing.
  • The GUISkin class which is basically just a collection of predefined default styles for all built in controls and has an array for unlimited custom styles at the end.
  • The utility classes GUIUtility and in the editor the EditorGUIUtility. Not to forget the tiny but important GUILayoutUtility class when it comes to using GUILayout classes.

OnGUI

OnGUI is a callback that is called automatically by the engine for several purposes. It's usually called at least two times per frame but might be called several times a frame when other events happens.

OnGUI is, in my opinion, a bad name. Of course it's main purpose is to "do GUI stuff", but it's actually an event processor callback. I will explain that in the next section.

The Event class

The event class holds information about the event that's currently processed in OnGUI. It's only static member current. This holds an instance of the Event class and is only valid in OnGUI.

The type property holds the current EventType that is processed.

Here's a quick overview of the different events:

  • Mouse events like MouseDown, MouseUp, MouseMove(editor only), MouseDrag, ScrollWheel. I should add that Unity will emulate those events on mobile touch devices. It calculates the mouseposition by taking the arithmetic mean of all touches.
  • Keyboard events like KeyDown and KeyUp. Note: unlike Input.GetKeyDown / Up those events map the system's OS keyboard events. So when you hold down a key, the keydown event is repeated by the systems repeat rate. This is of course important for text editor GUIs.
  • The Layout event. This is a special event that is always executed before all other eventa and is used to determine the size and position for automatic layouted controls. Note: This event can be disabled with useGUILayout but keep in mind that all GUILayout stuff won't work in this case.
  • The Repaint event. This event will actually draw the GUI elements.
  • The Used event type. Any control that processes an event and don't want others to get this event can use the Use function to "eat" the event. When Use is called the event type will be set to Used and every control should just ignore this event.

Beside the type of event, it holds additional information for the event.

  • mousePosition holds the mouse position in GUI coordinates. GUI coordinates are different from screen coordinates. The GUI has it's origin at the top left corner, screen coordinates has it's origin at the bottom left corner. The GUIUtility class has functions to convert those coordinates.
  • button. Only valid for mouse events and contains the mouse button index (0 - left button, 1 - right button, 2 - middle button)
  • modifiers hold information about Shift, Alt, Ctrl keys
  • keyCode. Only valid for keyboard events. Holds the KeyCode of the key that has been pressed / released.
  • Some additional things like shortcuts for modifier keys.

GUIStyle

As already mentioned this class is responsible for rendering a control to the screen. A GUI control function will call one of the Draw functions when the repaint event is "detected". An GUIStyle consists of different GUIStyleStates where only one is "active" at a time. Which state is used is determined by what Draw function is called, which parameters are passed and if the control has currently the keyboard focus or not.

When a single GUIStyle is drawn it can produce 0 to 3 draw calls. That's why very complicated GUIs are considered bad for mobile development, however we used the GUI system a lot on Android and iOS and hadn't much problems on more or less modern devices.

The things a GUIStyle might draw are:

  • The background image from the GUIStyleState. This is usually used to define how the button / textfield / ... generally looks like. If a background image is set it's always drawn first.
  • An content image defined in the GUIContent class that is passed to Dram.
  • The content's Text also defined in the GUIContent.

The ImagePosition in the GUIStyle defines where the image is displayed in relation to the text or to only draw the image and ignore the text or the other way round.

A GUIStyle has 8 GUIStyleStates where each state allows you to specify a background image as well as a text color. Note: A state is only used when it has a Background image. So unfortunately it's not possible to just setup text colors without an background image.

The different states are:

  • normal
  • hover
  • active
  • focused
  • onNormal
  • onHover
  • onSctive
  • onFocused

normal is the stylestate that is used in most cases. A label for example will only use this state.
hover is used by buttons, toggles and most other interactive elements when you hover over the element.
active is used for interactive elements while you click-and-hold on the element. It's the button down state.
focused is used by TextFields and TextAreas when the editor is active and you can edit the text.

All the onXXX states have the same function as the other 4 above, but are used for elements that have an "on" state like a Toggle. So if the toggle is unchecked and you click on it you will see the active state. When the toggle is already checked you will see the onActive state.

As mentioned above states without a background image are ignored. So if your button style has a "normal" and "active" state with a texture but no hover state, Unity will use the normal state when you hover over your button. If you want to just change the text color when you hover you have to use the same background image for "hover" as you used for "normal" otherwise the hover state will be ignored.

As last note: Unity has implemented an implicit casting operator (sounds complicated, but it isn't

标签:control,IMGUI,GUI,controls,class,will,Unity,快速,event
From: https://www.cnblogs.com/dewxin/p/17853541.html

相关文章

  • 【HarmonyOS】初识ArkUI——快速实现页面导航之Tabs
    ​ 【关键字】HarmonyOS、ArkUI、Tabs、自定义导航栏、底部导航 1、写在前面今天来介绍一下如何通过ArkUI来实现应用中常见的导航效果——底部导航,我们是通过Tabs来实现,并且会使用自定义导航栏的形式来构建,参考文档:https://developer.harmonyos.com/cn/docs/documentation/d......
  • 【HarmonyOS】初识ArkUI——快速实现页面导航之Navigation
    ​【关键字】HarmonyOS、ArkUI、Navigation、自定义导航栏、底部导航 1、写在前面今天继续来介绍如何通过ArkUI来实现应用中常见的导航效果——底部导航,上一篇中介绍了Tabs的实现方式,今天来介绍另一种组件Navigation,参考文档:https://developer.harmonyos.com/cn/docs/documen......
  • csdn 快速制作动态gif 插图
    文章目录1、简介2、下载工具LICEcap3、步骤1)安装下载软件2)双击打开3)点击record按钮录制4)结束录制1、简介制作csdn动态插图2、下载工具LICEcap官方下载网址:https://pc.qq.com/detail/0/detail_22760.html3、步骤1)安装下载软件2)双击打开出现如图所示透明窗口,可以拖动改变大小......
  • Java开发者的Python快速进修指南:面向对象基础
    当我深入学习了面向对象编程之后,我首先感受到的是代码编写的自由度大幅提升。不同于Java中严格的结构和约束,Python在面向对象的实现中展现出更加灵活和自由的特性。它使用了一些独特的关键字,如self和cls,这些不仅增强了代码的可读性,还提供了对类和实例的明确引用。正如Java,Python也......
  • 快速入门CMake
    一、CMake简介​ 使用简单方便,可以跨平台,构建项目编译环境。尤其比直接写Makefile简单(在构建大型工程编译时,需要写大量的文件依赖关系),可以通过简单的CMake生成负责的Makefile文件。二、CMake安装​ ubuntu上直接执行sudoaptinstallcmake安装完成,可以通过cmake-version查......
  • 快速生成一组环形数据
      sklearn是一个开源的机器学习库,支持不同种类的机器学习算法,并且提供了许多质量良好的数据集。假如我们想要得到一组环形数据集,借助sklearn的包很轻易就可以实现,不过换个角度思考,我们自己动手是否也可以生成一组数据,使之在散点图上环状分布;借助C++的random头文件以及一点高中数......
  • Unity异常提示 Invalid worldAABB. Object is too large or too far away from the or
    Unity在编辑器退出EditMode进入PlayMode之前,调用了一次Start和Update,然后提供了空的数据。这个时候容易造成除以0的情况,但是Unity没有立刻抛出异常,而是继续执行,生成了一个无穷大的数值。......
  • .NET8:快速集成Rapid.NET三维控件
    .NET8正式版本发布了,AnyCADRapid.NET针对.NET8进行了升级和优化。本文以WPF项目为例介绍在.NET8中使用AnyCADRapid.NET三维控件。1从.NET6升级若之前使用NET6升级到.NET8,升级过程非常简单,升级到AnyCADRapid.NET最新版本后,仅需要更改以下两处:(1).csproj文件<TargetFramewor......
  • Unity3d中使用sketchup 3dwarehouse模型
    1、选择菜单【文件】【导出】【三维模型】,打开导出对话框。选择保存类型为“*.fbx”。2、进行选项配置,“几何图形”类别勾选【导出两边的平面】【分离不连接的平面】,“材料”类别勾选【导出纹理映射】,“比例”类别中勾选【切换yz坐标(Y向上)】,单位选择【米】点击【确定】按钮。3......
  • 快速掌握keepalived
    Keepalived是一个基于VRRP(虚拟路由冗余协议)的开源软件,用于在Linux系统上实现高可用性和负载均衡。它的主要功能是通过多台服务器之间的协作,确保在其中一台服务器发生故障时,另一台服务器可以接管其功能,并继续提供服务,从而实现系统的高可用性。常用功能:虚拟IP(VirtualIP):Keepali......