Unity/c# object/UI display and hide

Object/UI show hide

  1. Object activation state control show hide
gameObject.SetActive(true);
gameObject.SetActive(false);

insert image description here
When the bool value is true, the object is displayed;
insert image description here
when the bool value is false, the object is hidden

  1. Get the material component control to show hidden
    (1) objects
GameObject.GetComponent<MeshRenderer>().enabled=true;
GameObject.GetComponent<MeshRenderer>().enabled=false;

insert image description here
When the bool value is true, the object is displayed;
insert image description here
when the bool value is false, the object is hidden

(2)UI

GameObject.GetComponent<Image>().enabled = true;
GameObject.GetComponent<Image>().enabled = false;

insert image description here
When the bool value is true, the object is displayed;
insert image description here
when the bool value is false, the object is hidden

3. The Canvas Group component controls the display and hiding of the UI. Adding
method Search the parameter Alpha in the
insert image description here
Canvas Group component : controls the transparency, 0 is completely transparent, 1 is completely opaque ; Block Raycasts: Whether to accept ray detection Ignore Parent Groups: Whether to respond to the parent group code as follows:
insert image description here





   canvasGroup.alpha = 1;
   canvasGroup.interactable = true;
   anvasGroup.blocksRaycasts = true;

Guess you like

Origin blog.csdn.net/m0_53934771/article/details/123248374