其他分享
首页 > 其他分享> > Unity3d自动隐藏顶部工具栏

Unity3d自动隐藏顶部工具栏

作者:互联网

将以下脚本挂载到工具栏面板上即可。

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

[RequireComponent(typeof(RectTransform))]
public class AutoHideToolbar : MonoBehaviour {

    bool isShow = true;
    private RectTransform rectTr;
	// Use this for initialization
	void Start () {
        rectTr = GetComponent<RectTransform>();
    }
	
	// Update is called once per frame
	void Update () {
        if (Input.mousePosition.y > Screen.height - rectTr.sizeDelta.y)
        {
            if (isShow == false)
            {
                StartCoroutine(ShowToolBar(true));
                isShow = true;
            }
        }
        else
        {
            if (isShow == true)
            {
                
                StartCoroutine(ShowToolBar(false));
                isShow = false;
            }
        }
    }

    IEnumerator ShowToolBar(bool show)
    {
        int count = 20;
        float step = rectTr.sizeDelta.y / (float)count;
        step = isShow ? step : -step;

        for (int i = 0; i < count; i++)
        {
            rectTr.anchoredPosition = new Vector2(rectTr.anchoredPosition.x, rectTr.anchoredPosition.y+ step);
            yield return null;
        }
    }
}

标签:Unity3d,工具栏,isShow,step,ShowToolBar,false,rectTr,true,自动隐藏
来源: https://blog.csdn.net/zouxin_88/article/details/101224502