RectMask2D是Unity UGUI中的一個元件,用於實現2D遮罩效果。它可以限制子物件在指定的矩形區域內顯示,超出區域的部分將被遮罩隱藏。
RectMask2D元件通過裁剪子物件的渲染區域來實現遮罩效果。它使用一個矩形區域作為遮罩範圍,只有在該範圍內的子物件才會被顯示出來。
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public RectMask2D rectMask;
private void Start()
{
rectMask.enabled = true;
}
}
操作步驟:
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public RectMask2D rectMask;
public RectTransform maskRect;
private void Update()
{
rectMask.rectTransform.sizeDelta = maskRect.sizeDelta;
}
}
操作步驟:
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public RectMask2D rectMask;
public Button button;
private void Start()
{
button.onClick.AddListener(OnButtonClick);
}
private void OnButtonClick()
{
Debug.Log("Button Clicked!");
}
}
操作步驟:
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public RectMask2D rectMask;
public ScrollRect scrollRect;
private void Start()
{
scrollRect.enabled = true;
}
}
操作步驟:
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public RectMask2D rectMask;
public Animator animator;
private void Start()
{
animator.enabled = true;
}
}
操作步驟: