查看: 5792|回复: 2
打印 上一主题 下一主题

Unity3D实现触屏手机滑动效果

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-4-13 14:06:47 |只看该作者 |倒序浏览
针对安卓平台,下面是主体代码:
using UnityEngine;
using System.Collections;
public class TestUI : MonoBehaviour {
public Vector2 scrollPosition = Vector2.zero;
public float scrollVelocity = 0f;
public float timeTouchPhaseEnded = 0f;
public float inertiaDuration = 0.5f;
public Vector2 lastDeltaPos;
// Use this for initialization
void Start () {
}
void OnGUI()
{
scrollPosition = GUI.BeginScrollView(new Rect(100, 40, 600, 400), scrollPosition, new Rect(0, 0, 500, 1600), false, true);
for (int i = 0; i < 32; i++)
{
GUI.Button(new Rect(0, i*50, 400, 50), "Button"+i);
}
GUI.EndScrollView();
}

// Update is called once per frame
void Update ()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
scrollPosition.y += Input.GetTouch(0).deltaPosition.y;
lastDeltaPos = Input.GetTouch(0).deltaPosition;
}
else if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
print ("End:"+lastDeltaPos.y+"|"+Input.GetTouch(0).deltaTime);
if (Mathf.Abs(lastDeltaPos.y)> 20.0f)
{
scrollVelocity = (int)(lastDeltaPos.y * 0.5/ Input.GetTouch(0).deltaTime);
print(scrollVelocity);
}
timeTouchPhaseEnded = Time.time;
}
}
else
{
if (scrollVelocity != 0.0f)
{
// slow down
float t = (Time.time - timeTouchPhaseEnded)/inertiaDuration;
float frameVelocity = Mathf.Lerp(scrollVelocity, 0, t);
scrollPosition.y += frameVelocity * Time.deltaTime;
if (t >= inertiaDuration)
scrollVelocity = 0;
}
}
}
}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

643

主题

1

听众

9937

积分

高级设计师

Rank: 6Rank: 6

纳金币
9935
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-4-13 20:29:28 |只看该作者
看不懂?????????
回复

使用道具 举报

643

主题

1

听众

9937

积分

高级设计师

Rank: 6Rank: 6

纳金币
9935
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-4-13 20:29:34 |只看该作者
好好好好
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-14 23:47 , Processed in 0.058742 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部