如何在Unity中制作onGUI方法?
How to make the onGUI method in Unity?
using UnityEngine;
using System.Collections;
public class GameRootScript : MonoBehaviour {
public GameObject prefab = null;
private AudioSource audio;
public AudioClip jumpSound;
public Texture2D icon = null;
public static string mes_text = "test";
// Use this for initialization
void Start () {
this.audio = this.gameObject.AddComponent<AudioSource> ();
this.audio.clip = this.jumpSound;
this.audio.loop = false;
}
void onGUI()
{
Debug.Log ("Image");
GUI.DrawTexture (new Rect (Screen.width/2, 64, 64, 64), icon);
GUI.Label (new Rect (Screen.width / 2, 128, 128, 32), mes_text);
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown (KeyCode.Z)){
Debug.Log("Prefab");
GameObject go = GameObject.Instantiate(this.prefab) as GameObject;
go.transform.position = new Vector3(Random.Range(-2.0f,2.0f), 1.0f, 1.0f);
this.audio.Play();
}
}
}
我在Unity中制作了onGUI()方法,但是这个方法不起作用。
只是照着书看,不知道是什么问题
即使我编译该代码也没有错误。
书上的Unity版本是4.xx,我的Unity版本是5.1.2.
您的 onGUI
方法有错字。它应该是 OnGUI
大写 "O".
由于存在拼写错误,即使可以编译,Unity 引擎基本上也会忽略此方法。
using UnityEngine;
using System.Collections;
public class GameRootScript : MonoBehaviour {
public GameObject prefab = null;
private AudioSource audio;
public AudioClip jumpSound;
public Texture2D icon = null;
public static string mes_text = "test";
// Use this for initialization
void Start () {
this.audio = this.gameObject.AddComponent<AudioSource> ();
this.audio.clip = this.jumpSound;
this.audio.loop = false;
}
void onGUI()
{
Debug.Log ("Image");
GUI.DrawTexture (new Rect (Screen.width/2, 64, 64, 64), icon);
GUI.Label (new Rect (Screen.width / 2, 128, 128, 32), mes_text);
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown (KeyCode.Z)){
Debug.Log("Prefab");
GameObject go = GameObject.Instantiate(this.prefab) as GameObject;
go.transform.position = new Vector3(Random.Range(-2.0f,2.0f), 1.0f, 1.0f);
this.audio.Play();
}
}
}
我在Unity中制作了onGUI()方法,但是这个方法不起作用。
只是照着书看,不知道是什么问题
即使我编译该代码也没有错误。
书上的Unity版本是4.xx,我的Unity版本是5.1.2.
您的 onGUI
方法有错字。它应该是 OnGUI
大写 "O".
由于存在拼写错误,即使可以编译,Unity 引擎基本上也会忽略此方法。