要实现一个简单的UI左右折叠显示与隐藏,可以结合遮罩,通过代码控制UI区块的宽度和位移来实现。
具体可以按以下步骤实现:
1、新建一个Image组件,并添加精灵,调整大小后,复制一份作为该UI的父物体,然后在该父物体上添加Mask组件,并勾掉Show Mask Graphic选项,如图:

2、新建两个控制按钮,如下图:

3、编写控制脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SlideInOutUI : MonoBehaviour
{
public RectTransform uiPanel;
public Button showButton;
public Button hideButton;
public float slideSpeed = 5f;
private bool isPanelShown = true;
// Start is called before the first frame update
void Start()
{
showButton.onClick.AddListener(ShowPanel);
hideButton.onClick.AddListener(HidePanel);
}
// Update is call

本文详细描述了如何使用Unity中的Image组件、Mask和RectTransform实现UI的左右滑动折叠功能,通过按钮控制面板的显示和隐藏,以及关键的脚本编写过程。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



