Unity3D v5.4 - GUILayout does not exist

本文关键字:does not exist GUILayout v5 Unity3D | 更新日期: 2023-09-27 18:03:30

我正在尝试制作游戏菜单。为此,我需要GUIlayout和它的方法。然而,看起来Unity找不到GUIlayout对象,显示此错误:

Assets/scripts/GameManager.cs(38,25):错误CS0103:名称"GUIlayout"在当前上下文中不存在

我代码:

using UnityEngine;
using UnityEditor;
using System.Collections;
public class GameManager : MonoBehaviour {
public bool isMenuActive{get;set;}
void Awake () {
    isMenuActive = true;
}
void OnGUI(){
    const int Width = 300;
    const int Height = 200;
    if (isMenuActive){
        Rect windowRect = new Rect((Screen.width - Width) / 2 ,(Screen.height - Height) / 2, Width , Height);
        GUIlayout.window(0,windowRect,MainMenu,"Main menu");
    }
}
private void MainMenu(){
    // Debug.Log("menu is displayed");
}
}

任何想法?

Unity3D v5.4 - GUILayout does not exist

问题来自代码行:

GUILayout.Window(0, windowRect, MainMenu, "Main menu");
1

。是GUILayout,不是GUIlayoutL大写。

2

。使用GUILayout的静态函数是Window,而不是window。与问题#1相同的大小写问题

3 Window函数的第三个形参需要将带int形参的函数传递给它。您必须使用int来参数化MainMenu函数。

public class GameManager : MonoBehaviour
{
    public bool isMenuActive { get; set; }
    void Awake()
    {
        isMenuActive = true;
    }
    void OnGUI()
    {
        const int Width = 300;
        const int Height = 200;
        if (isMenuActive)
        {
            Rect windowRect = new Rect((Screen.width - Width) / 2, (Screen.height - Height) / 2, Width, Height);
            GUILayout.Window(0, windowRect, MainMenu, "Main menu");
        }
    }
    private void MainMenu(int windowID)
    {
        // Debug.Log("menu is displayed");
    }
}

最后,你不应该使用这个。你应该使用新的Unity UI。这里有一个教程