GUI.depth

Unity · Версия 2020.3 LTS · Scripting · GUI

GUI.depth

public static int depth;

Описание

Глубина сортировки текущего поведения GUI.

Установите этот параметр, чтобы определить порядок при одновременном выполнении разных сценариев. Элементы GUI, нарисованные с более низкими значениями глубины, будут отображаться поверх элементов с более высокими значениями (т. е. вы можете думать о глубине как о «расстоянии» от камеры).

Примечание. Чтобы увидеть, как работает этот пример, вам потребуется создать 2 скрипта. Не забудьте назвать скрипты теми же именами, что и имена классов, иначе это не сработает.

Одна кнопка за другой.
using UnityEngine;
using System.Collections;

// Makes this button go back in depth

public class Example1 : MonoBehaviour
{
    public int guiDepth = 0;
    public Example2 example2;

    private float buttonX, buttonY;

    void Start()
    {
        buttonX = (Screen.width / 2) - 100;
        buttonY = (Screen.height / 2) - 100;
    }

    void OnGUI()
    {
        GUI.depth = guiDepth;
        GUI.color = Color.yellow;

GUIStyle size = new GUIStyle("button");
        size.fontSize = 16;

        if (GUI.RepeatButton(new Rect(buttonX, buttonY, 200, 100), "Go Backwards", size))
        {
            guiDepth = 1;
            example2.guiDepth = 0;
        }
    }
}

И скопируйте этот другой пример в другой скрипт:

using UnityEngine;
using System.Collections;

// Makes this button go back in depth

public class Example2 : MonoBehaviour
{
    public int guiDepth = 1;
    public Example1 example1;

    private float buttonX, buttonY;

    void Start()
    {
        buttonX = (Screen.width / 2)  - 50;
        buttonY = (Screen.height / 2) - 50;
    }

    void OnGUI()
    {
        GUI.depth = guiDepth;
        GUI.color = Color.green;

GUIStyle size = new GUIStyle("button");
        size.fontSize = 16;

        if (GUI.RepeatButton(new Rect(buttonX, buttonY, 200, 100), "Go Backwards", size))
        {
            guiDepth = 1;
            example1.guiDepth = 0;
        }
    }
}

Обновлено 17 ноября 2026 г.