Unity3d自动隐藏顶部工具栏

Unity3d自动隐藏顶部工具栏_第1张图片

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

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();
    }
	
	// 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自动隐藏顶部工具栏_第2张图片

你可能感兴趣的:(Unity)