codecamp

OnGUI TextArea 控件

Unity 3D TextArea 控件用于创建一个多行的文本编辑区。用户可以在多行文本编辑区编辑文本内容。

该控件可以对超出控件宽度的文本内容实现换行操作。

TextArea 控件同样会将当前文本编辑区中的文本内容以字符串形式返回。

开发人员可以通过创建 String 变量来接收返回值并实现相关功能。

具体使用方法如下:

public static function TextArea(position:Rect, text:string):string;
public static function TextArea(position:Rect, text:string, maxLength:int):string;
public static function TextArea(position:Rect, text:string, style:GUIStyle):string;
public static function TextArea(position:Rect, text:string, maxLength:int,style:GUIStyle):string;

注:

  • 其中,position 为显示位置。

  • text 为字符。

参数列表

参数 描述
position 设置控件在屏幕上的位置及大小。
maxLength 设置输入的字符串的最大长度。
text 设置控件上默认显示的文本。
style 设置控件使用的样式。

使用案例

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

  1. 执行 AssetsCreateJavaScript 命令,创建一个新的脚本文件。

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

    var stringToEdit:String="Hello World\nI've got 2 lines...";
    function OnGUI(){
        stringToEdit=GUI.TextArea(Rect(10, 10, 200, 100), stringToEdit, 200);
    }

  1. Ctrl+S 键保存脚本。

  1. 在 Project 视图中选择脚本,并将其拖曳到 Hierarchy 视图中的 Main Camera 上,使脚本和摄像机产生关联。

  1. 进行脚本测试,如下图所示。

OnGUI TextField 控件
OnGUI ScrollView 控件
温馨提示
下载编程狮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; }