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

摘要:
publicfloatinertia持续时间=0.5f;for(inti=0;“Button”+i);}//UpdateiscalledonceperframevoidUpdate(){if(Input.touchCount>lastDeltaPos=Input.GetTouch(0).deltaPosition;if(Mathf.Abs(lastDeltaPos.y)>

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

这里是原文,使用的是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;
}
}
}
}

Unity3D制作触屏手机滑动动效果第1张

Unity3D制作触屏手机滑动动效果第2张

免责声明:文章转载自《Unity3D制作触屏手机滑动动效果》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇命令行方式实现QQ自动登录(转)mysql8.0配置文件优化下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python基础【day01】:python 2和3区别(四)

许多Python初学者都会问:我应该学习哪个版本的Python。对于这个问题,我的回答通常是“先选择一个最适合你的Python教程,教程中使用哪个版本的Python,你就用那个版本。等学得差不多了,再来研究不同版本之间的差别”。 但如果想要用Python开发一个新项目,那么该如何选择Python版本呢?我可以负责任的说,大部分Python库都同时支持Pyt...

python面向对象 : 抽象类(接口类),多态,封装(私有制封装)

一. 抽象类(接口类)   与java一样, python也有抽象类的概念但是同样需要借助模块实现,抽象类是一个特殊的类, 它的特殊之处在于只能被继承, 不能被实例化.   从设计角度去看, 如果类是从现实对象抽象而来的, 那么抽象类就是基于类抽象而来的。     从实现角度来看, 抽象类与普通类的不同之处在于: 抽象类中有抽象方法, 该类不能被实例化,...

python接口自动化测试二十:函数写接口测试

python接口自动化测试二十:函数写接口测试  # coding:utf-8import requestsimport refrom bs4 import BeautifulSoup# s = requests.session() # 全局的sdef get_token(s): ''' fuction: 获取token args:...

查看KVM虚拟机IP

#!/bin/bash #ping当前网段内在线的主机,以便产生arp记录. subnet=`route -n|grep "UG" |awk '{print $2}'|sed 's/..$//g'` for ip in $subnet.{1..253};do { ping -c1 $ip >/dev/null 2>&1 }& d...

AI学习---数据IO操作&amp;amp;神经网络基础

数据IO操作 TF支持3种文件读取:    1.直接把数据保存到变量中    2.占位符配合feed_dict使用    3. QueueRunner(TF中特有的) 文件读取流程 文件读取流程(多线程 + 队列)1)构造文件名队列(先读取文件名到队列,方便快速读取文件)    file_queue = tf.train.string_input_p...

python 标准库subprocess

作者:Vamei 出处:http://www.cnblogs.com/vamei subprocess包主要功能是执行外部的命令和程序。subprocess的功能与shell类似。subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用。另外subprocess还提供了一些管理标准流(s...