Unity3D制作触屏手机滑动动效果

想要苹果或安卓那种手指滑动屏幕的效果。

 

这里是原文,使用的是scrollview来演示。

http://www.mindthecube.com/blog/2010/09/adding-iphone-touches-to-unitygui-scrollview

 

它是iphone的开发,我发一下android上的,选择里边的item还没添加进来。

 

复制代码
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;
            }
        }
    }
}
复制代码

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值