Unity controls the display and hiding of objects

1. Use Button to control the display and hide of Image or Text
1. Create an Image and Text
insert image description here
2. Create Button insert image description here
3. Create code

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

public class BtnShowMsg : MonoBehaviour
{
    
    
    private bool isClick=false;	//默认是不显示的
	
    public void Click()
    {
    
    
        if (isClick == false)	//创建点击事件,如果当前不显示就让它显示
        {
    
    
            gameObject.SetActive(true);
            isClick =true;
        }
        else	//如果当前显示,就让它不显示
        {
    
    
            gameObject.SetActive(false);
            isClick = false;
        }
    }

}

4. Give the code to the thing you want to hide and turn off the display

insert image description here

5. Add click event to Button

insert image description here

Guess you like

Origin blog.csdn.net/ashmoc/article/details/108234698