如何在Unity中创建onGUI方法?

时间:2015-08-11 19:13:00

标签: c# unity3d unityscript

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()方法,但该方法不起作用 我只是按照这本书,我不知道是什么原因造成的 即使我编译该代码也没有错误 书籍版本为4.xx,Unity版本为5.1.2。

1 个答案:

答案 0 :(得分:6)

您的onGUI方法存在拼写错误。它应该是OnGUI,大写“O”。

即使编译了拼写错误,Unity引擎也基本上忽略了这种方法。