[Unity3D] How to switch the canvas in uniyt to realize the interactive operation of the switching interface

When we switch between different interfaces, we often use the operation of switching scenes.

If you can switch the interface in one scene, if you use the switching scene to realize it, it will take up a lot of space. You might as well use the method of switching canvases in a scene to realize the interactive operation of switching the interface.

1. Add two canvases and text and button components to the unity scene

 The effect is shown in the figure:

2. Create a script to switch the canvas, named ChangeCanvas

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeCanvas : MonoBehaviour
{
    public GameObject CanvasOn;//定义打开画布
    public GameObject CanvasOff;//定义关闭画布
    
    public void changeCanvas()//定义切换画布的方法
    {
        CanvasOn.SetActive(true);//实现打开画布
        CanvasOff.SetActive(false) ;//实现关闭画布
    }
}

 3. Add the script to the Button component of the two canvases, and hang the two canvases

 In the first canvas, after clicking the button, the first canvas is closed and the second canvas is opened

 In the second canvas, close the second canvas and open the first canvas after clicking the button

4. Hide one of the canvases

 5. Running effect

 

Guess you like

Origin blog.csdn.net/dislike_carry/article/details/129795383