codecamp

OnGUI label 控件

Unity 3D Label 控件用于在设备的屏幕上创建文本标签和纹理标签,和Box 控件类似,可以显示文本内容或图片。

Label 控件一般用于显示提示性的信息,如当前窗口的名称、游戏中游戏对象的名字、游戏对玩家的任务提示和功能介绍等。

具体使用方法如下:

public static function Label(position:Rect, text:string):void;
public static function Label(position:Rect, image:Texture):void;
public static function Label(position:Rect, content:GUIContent):void;
public static function Label(position:Rect, text:string, style:GUIStyle):void;
public static function Label(position:Rect, image:Texture, style:GUIStyle):void;
public static function Label(position:Rect, content:GUIContent, style:GUIStyle):void;

其中,positionLabel 显示的位置,textLabel 上显示的文本,imageLabel 上显示的纹理图片。

参数列表:

参数 描述
position 设置控件在屏幕上的位置及大小。
image 设置控件上显示的纹理图片。
style 设置控件使用的样式。
text 设置控件上显示的文本。
content 设置控件的文本、图片和提示。

使用案例

  1. 创建项目,将其命名为 Label,保存场景。

  1. 在 Unity 3D 菜单栏中执行 AssetsCreateJavaScript 命令,创建一个新的脚本文件。

  1. 在 Project 视图中双击该脚本文件,打开脚本编辑器,输入下列语句:

    var textureToDisplay:Texture2D;
    function OnGUI(){
        GUI.Label(Rect(10, 10, 100, 20), "Hello World!");
        GUI.Label(Rect(10, 40, textureToDisplay.width, textureToDisplay.height),textureToDisplay);
    }

  1. Ctrl+S 键保存脚本。

  1. 在 Project 视图中选择脚本,将其连接到 Main Camera

  1. 单击主摄像机,在 Inspector 属性面板中添加纹理图片。

  1. 单击 Play 按钮进行测试,如下图所示,界面上出现一串文字以及贴图。

OnGUI Box 控件
OnGUI Background Color 控件
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }