如何在Unity中制作onGUI方法

本文关键字:onGUI 方法 Unity | 更新日期: 2023-09-27 18:07:19

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。我的Unity版本是5.1.2。

如何在Unity中制作onGUI方法

您的onGUI方法有一个错别字。应该是OnGUI加上大写的"O"。

有了错字,即使编译,Unity引擎也会忽略这个方法。