栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

鼠标移动到侧边自动唤起侧边栏,鼠标移开侧边栏滑走(附源码)(方法二)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

鼠标移动到侧边自动唤起侧边栏,鼠标移开侧边栏滑走(附源码)(方法二)

我个人认为此方法相比于第一种更简洁一点

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Smoothmove : MonoBehaviour
{

	[Range(0.0f, 10.0f)]//***
    [HideInInspector]
	public float moveSpeed = 2.0f;//图片移动速度
	/// 
	/// 显示上拉框的位置
	/// 
	private Vector2 showupPos;
	/// 
	/// 隐藏上拉框的位置
	/// 
	private Vector2 hideupPos;
	/// 
	/// 记录上拉框图片位置
	/// 
	private RectTransform _upTransfrom;
	private bool _isShow;
	/// 
	/// 显示左拉框的位置
	/// 
	private Vector2 showleftPos;
	/// 
	/// 隐藏左拉框的位置
	/// 
	private Vector2 _hideleftPos;
	/// 
	/// 记录左拉框图片位置
	/// 
	private RectTransform _leftTransfrom;
	/// 
	/// 判断是否移动到上面的菜单栏区域
	/// 
	bool _up;
	bool _left;
	private RectTransform uparea;
	private RectTransform leftarea;
	void Start()
	{
		uparea = GameObject.Find("moveup").gameObject.GetComponent();
		leftarea = GameObject.Find("moveleft").gameObject.GetComponent();
		
		_up = false;
	
		_left=false;


		_upTransfrom = GameObject.Find("Scroll View H").gameObject.GetComponent();//因为代码挂在上拉框上,直接获取上拉框初始位置
		showupPos = _upTransfrom.anchoredPosition;//初始显示位置
		hideupPos = new Vector2( showupPos.x,showupPos.y + 300);//隐藏时移动到的位置,垂直移动
																					 //_isShow = false;
		_leftTransfrom = GameObject.Find("di").gameObject.GetComponent();//di是左拉框
		showleftPos = _leftTransfrom.anchoredPosition;//初始显示位置
		_hideleftPos = new Vector2(showleftPos.x -300, showleftPos.y);//隐藏时移动到的位置,水平移动
	}
	void Update()
	{
		_up = RectTransformUtility.RectangleContainsScreenPoint(uparea, Input.mousePosition);
		_left = RectTransformUtility.RectangleContainsScreenPoint(leftarea, Input.mousePosition);

		if (_up)
        {
			ShowMenu();
        }
		if (_left)
		{
			 Showleft();
		}
		
		
	}
	/// 
	/// 调用显示上侧携程,可在别处调用此函数来显示菜单
	/// 
	public void ShowMenu()
	{
		StartCoroutine(Appear());
	}
	/// 
	/// 显示上侧菜单
	/// 
	/// 
	IEnumerator Appear()
	{
		float time = Time.time;
		float timeDiff = 0;
		while (timeDiff < 1)
		{
			timeDiff = (Time.time - time) * moveSpeed;
			Vector2 currentPos = Vector2.Lerp(showupPos, hideupPos, timeDiff);
			_upTransfrom.anchoredPosition = currentPos;
			yield return new WaitForEndOfFrame();
		}

		//_isShow = true;
	}
	/// 
	/// 调用显示左拉框的携程
	/// 
	public void Showleft()
	{
		StartCoroutine(Appearleft());
	}
	
	/// 
	/// 显示左拉框的携程
	/// 
	/// 
	IEnumerator Appearleft()
	{

		float time = Time.time;
		float timeDiff = 0;

		while (timeDiff < 1)
		{
			timeDiff = (Time.time - time) * moveSpeed;
			Vector2 currentPos = Vector2.Lerp(showleftPos, _hideleftPos, timeDiff);
			_leftTransfrom.anchoredPosition = currentPos;

			yield return new WaitForEndOfFrame();//***
		}

		_isShow = true;
	}
}

这是通过检测UI区域来实现的,还有一种方法在另一篇文章

20220803_211104

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1036222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号