GUI.skin 皮肤

static var skin : GUISkin

The global skin to use.

You can set this at any point to change the look of your GUI. If you set it to null, the skin will revert to the default Unity skin.

全局皮肤使用。你可以在任何时候改变GUI外观,如果你设置为null,那么皮肤将恢复默认的Unity GUI皮肤。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public GUISkin[] s1;
	private float hSliderValue = 0.0F;
	private float vSliderValue = 0.0F;
	private float hSValue = 0.0F;
	private float vSValue = 0.0F;
	private int cont = 0;
	void Update() {
		if (Input.GetKeyDown(KeyCode.Space))
			cont++;

	}
	void OnGUI() {
		GUI.skin = s1[cont % s1.Length];
		if (s1.Length == 0) {
			Debug.LogError("Assing at least 1 sking on the array");
			return;
		}
		GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
		GUI.Box(new Rect(10, 50, 50, 50), "A BOX");
		if (GUI.Button(new Rect(10, 110, 70, 30), "A button"))
			Debug.Log("Button has been pressed");

		hSliderValue = GUI.HorizontalSlider(new Rect(10, 150, 100, 30), hSliderValue, 0.0F, 10.0F);
		vSliderValue = GUI.VerticalSlider(new Rect(10, 170, 100, 30), vSliderValue, 10.0F, 0.0F);
		hSValue = GUI.HorizontalScrollbar(new Rect(10, 210, 100, 30), hSValue, 1.0F, 0.0F, 10.0F);
		vSValue = GUI.VerticalScrollbar(new Rect(10, 230, 100, 30), vSValue, 1.0F, 10.0F, 0.0F);
	}
}
//定义皮肤数组,在检视面板添加几个定义皮肤进来测试效果
var s1 : GUISkin[];

private var hSliderValue : float = 0.0;
private var vSliderValue : float = 0.0;
private var hSValue : float = 0.0;
private var vSValue : float = 0.0;
private var cont : int = 0;

function Update() {
	if(Input.GetKeyDown(KeyCode.Space))
		//按空格累加cont数
		cont++;
}

function OnGUI() {
	//通过你指定的数组长度和空格累加数求余来指定皮肤
	GUI.skin = s1[cont%s1.Length];	
	if(s1.Length == 0){
		Debug.LogError("Assing at least 1 sking on the array");
		//如果不指定皮肤文件,则返回,不执行下面的代码。
		return;
	}
	//下面是几个GUI元素
	GUI.Label(Rect(10, 10, 100, 20), "Hello World!");
	GUI.Box(Rect(10, 50, 50, 50), "A BOX");
	if (GUI.Button(Rect(10,110,70,30), "A button"))
		Debug.Log("Button has been pressed");
	hSliderValue = GUI.HorizontalSlider(Rect(10, 150, 100, 30), hSliderValue, 0.0, 10.0);
	vSliderValue = GUI.VerticalSlider(Rect(10, 170, 100, 30), vSliderValue, 10.0, 0.0);
	hSValue = GUI.HorizontalScrollbar(Rect(10, 210, 100, 30), hSValue, 1.0, 0.0, 10.0);
	vSValue = GUI.VerticalScrollbar(Rect(10, 230, 100, 30), vSValue, 1.0, 10.0, 0.0);
} 
最后修改:2011年1月14日 Friday 22:34

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。