简体   繁体   English

如何在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();
        }
    }
}

I make the onGUI() method in Unity, but the method doesn't work. 我在Unity中制作了onGUI()方法,但是该方法不起作用。
I just follow the book and I don't know what makes the problem. 我只是看书,不知道是什么原因造成的。
Even I compile that code there is no error. 即使我编译该代码也没有错误。
The Unity of book version is 4.xx, and my Unity version is 5.1.2. 图书版本的Unity为4.xx,而我的Unity版本为5.1.2。

You have a typo for your onGUI method. 您的onGUI方法有一个错字。 It should be OnGUI with a capitalized "O". 它应该是带有大写“ O”的OnGUI

With the typo in place, even though it compiles, the Unity engine essentially ignores this method. 有了错字,即使编译了错字,Unity引擎实际上也忽略了该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM