如何在Unity中制作onGUI方法?

sna*_*nag 0 c# unity-game-engine 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在Unity中创建onGUI()方法,但该方法不起作用.
我只是按照这本书,我不知道是什么原因造成的.
即使我编译该代码也没有错误.
书籍版本为4.xx,Unity版本为5.1.2.

Chr*_*air 6

你的onGUI方法有一个错字.它应该是OnGUI大写的"O".

随着拼写错误的到位,即使它编译,Unity引擎也基本上忽略了这种方法.