包含此页的版本:
不含此页的版本:
尽管 Unity 的 IMGUI 系统主要用于创建开发人员工具和调试界面,但您仍然可以通过多种方式自定义它们并设置其样式。在 Unity 的 IMGUI 系统中,您可以使用许多细节微调控件的外观。控件外观由 GUIStyles 决定。默认情况下,当您在未定义 GUIStyle 的情况下创建控件时,将应用 Unity 的默认 GUIStyle。此样式是 Unity 内部的,可用于已发布的游戏以进行快速原型设计,或者如果您选择不设置控件的样式化。
当您有大量不同的 GUIStyle 要使用时,您可以在单个 GUISkin 中定义它们。GUISkin 只不过是 GUIStyle 的集合。
GUIStyles 旨在模仿 Web 浏览器的级联样式表 (CSS)。许多不同的 CSS 方法已被调整,包括区分样式的各个状态属性,以及内容和外观之间的分离。
其中 Control 定义内容,其中 Style 定义外观。这允许你创建像功能一样的组合切换 允许用户打开或关闭选项的复选框。更多信息
请参阅术语表它看起来像一个普通的 Button。
如前所述,GUISkin 是 GUIStyle 的集合。样式定义 GUI 控件的外观。如果要使用样式,则不必使用外观。
所有 GUI 控件函数都有一个可选的最后一个参数:用于显示控件的 GUIStyle。如果省略这一点,将使用 Unity 的默认 GUIStyle。这通过将控件类型的名称应用为字符串(即 GUI)在内部工作。Button() 使用“按钮”样式 GUI。Toggle() 使用“toggle”样式等。您可以通过将控件指定为最后一个参数来覆盖控件的默认 GUIStyle。
/* Override the default Control Style with a different style in the UnityGUI default Styles */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
void OnGUI () {
// Make a label that uses the "box" GUIStyle.
GUI.Label (new Rect (0,0,200,100), "Hi - I'm a label looking like a box", "box");
// Make a button that uses the "toggle" GUIStyle
GUI.Button (new Rect (10,140,180,20), "This is a button", "toggle");
}
}
当您声明公共 GUIStyle 变量时,Style 的所有元素都将显示在检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表.您可以在那里编辑所有不同的值。
/* Overriding the default Control Style with one you've defined yourself */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public GUIStyle customButton;
void OnGUI () {
// Make a button. We pass in the GUIStyle defined above as the style to use
GUI.Button (new Rect (10,10,150,20), "I am a Custom Button", customButton);
}
}
声明 GUIStyle 后,您可以在 Inspector 中修改该样式。您可以定义大量状态,并应用于任何类型的控制。
必须先为任何控制状态分配 背景颜色(Background Color) ,然后才能应用指定的文本颜色(Text Color)。
有关单个 GUIStyle 的更多信息,请阅读 GUIStyle 组件参考页面。
对于更复杂的 GUI 系统,将样式集合保存在一个地方是有意义的。这就是 GUISkin 的作用。GUISkin 包含多种不同的样式,本质上是为所有 GUI 控件提供完整的改头换面。
要创建 GUISkin,请从菜单栏中选择 Assets->Create->GUI Skin。这将在您的项目文件夹中创建一个 GUI 皮肤。选择它以查看检查器中皮肤定义的所有 GUIStyle。
要使用您创建的皮肤,请在 OnGUI() 函数中将其分配给 GUI.skin。
/* Make a property containing a reference to the skin you want to use */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public GUISkin mySkin;
void OnGUI () {
// Assign the skin to be the one currently used.
GUI.skin = mySkin;
// Make a button. This will get the default "button" style from the skin assigned to mySkin.
GUI.Button (new Rect (10,10,150,20), "Skinned Button");
}
}
您可以在单个 OnGUI() 调用中随心所欲地切换皮肤。
/* Example of switching skins in the same OnGUI() call */
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public GUISkin mySkin;
private bool toggle = true;
void OnGUI () {
// Assign the skin to be the one currently used.
GUI.skin = mySkin;
// Make a toggle. This will get the "button" style from the skin assigned to mySkin.
toggle = GUI.Toggle (new Rect (10,10,150,20), toggle, "Skinned Button", "button");
// Assign the currently skin to be Unity's default.
GUI.skin = null;
// Make a button. This will get the default "button" style from the built-in skin.
GUI.Button (new Rect (10,35,150,20), "Built-in Button");
}
}
这个例子将向您展示如何通过代码动态更改字体大小。
首先在 Unity 中创建一个新项目。然后创建一个名为 Fontsize.cs 的 C# 脚本,并将以下代码粘贴到其中:
using UnityEngine;
using System.Collections;
public class Fontsize : MonoBehaviour
{
void OnGUI ()
{
//Set the GUIStyle style to be label
GUIStyle style = GUI.skin.GetStyle ("label");
//Set the style font size to increase and decrease over time
style.fontSize = (int)(20.0f + 10.0f * Mathf.Sin (Time.time));
//Create a label and display with the current settings
GUI.Label (new Rect (10, 10, 200, 80), "Hello World!");
}
}
保存脚本并将其附加到空的游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
请参阅术语表,单击“播放”以查看字体循环的大小随时间增加和减少。您可能会注意到字体不会平滑地改变大小,这是因为字体大小没有无限数量。
此特定示例要求加载默认字体 (Arial) 并将其标记为动态字体。您无法更改任何未标记为动态的字体的大小。