UI页面切换按钮随页面切换缩放

16 篇文章 0 订阅

选中按钮放大其余按钮缩小的
效果:

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

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

public class BottomBarBtn : MonoBehaviour
{
    public RectTransform rect;
    public GameObject[] active;
    public Transform icon;
    float maxWidth = 360f;
    float minWidth = 180f;
    float maxScale = 1.5f;

    Coroutine closing;
    Coroutine expanding;
    bool isClose;
    public bool isPlaying;
    float width
    {
        get
        {
            return rect.sizeDelta.x;
        }
        set
        {
            Vector2 size = rect.sizeDelta;
            size.x = value;
            rect.sizeDelta = size;
        }
    }
    

    public void SetOpen(bool open)
    {
        isPlaying = false;
        float to = open ? maxWidth : minWidth;
        Vector3 toScale = !open ? Vector3.one : Vector3.one * maxScale;
        width = to;
        isClose = !open;
        icon.transform.localScale = toScale;
        SetActiveImg(open);
    }

    public void OpenBtn()
    {
        if (closing != null)
        {
            isPlaying = false;
            StopCoroutine(closing);
            closing = null;
        }
        if (expanding != null || !isClose) return;
        expanding = StartCoroutine(ExpandBtn(true));
        SetActiveImg(true);
    }


    public void CloseBtn()
    {

        if (expanding != null)
        {
            isPlaying = false;
            StopCoroutine(expanding);
            expanding = null;
        }
        if (closing != null || isClose) return;
        expanding = StartCoroutine(ExpandBtn(false));
        SetActiveImg(false);
    }

    void SetActiveImg(bool boolean)
    {
        foreach (GameObject obj in active)
        {
            obj.gameObject.SetActive(boolean);
        }
    }

    IEnumerator ExpandBtn(bool toBig)
    {
        float timer = 0;
        float time = 0.2f;
        isPlaying = true;
        float to = !toBig ? minWidth : maxWidth;
        Vector3 toScale = Vector3.one * (toBig ? maxScale : 1);
        float wFrom = width;
        Vector3 fromScale = icon.transform.localScale;
        while (timer < time)
        {
            width = rect.sizeDelta.x;
            timer += Time.deltaTime;
            float t = timer / time;
            float wCur = Mathf.Lerp(width, to, t);
            Vector3 curScale = Vector3.Lerp(fromScale, toScale, t);

            icon.transform.localScale = curScale;
            width = wCur;
            yield return null;
        }
        isPlaying = false;
        closing = null;
        expanding = null;
        width = to;
        isClose = !toBig;
        icon.transform.localScale = toScale;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值