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自动隐藏顶部工具栏

上一篇:前端点击按钮展示、隐藏对应的 元素


下一篇:工作日志-面向对象编程