⑴ unity3d如何实现手指向左滑动倒序播放图片序列,如果手指不动时停在当前的图片上,向右滑正播序列
C#写法:
// 定义一个手势的枚举
public enum Dir:int
{
Left = 0,
Stop,
Right
}
// C#脚本名为Test.cs
public class Test: MonoBehaviour {
public GameObject _plane;// 挂一个用来显示图片的plane对象
public float ration = 0.5f; // 每0.5秒换一张图片
public Texture2D[] _texAll; // 挂30张图片
Dir _touchDir; // 当前的手势
float curTime = 0;
int _index = 0;
void Update()
{
// 当运行平台为IOS或Android时
if(Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
{
// 当输入的触点数量大于0,且手指移动时
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
if(Input.GetTouch(0).deltaPosition.x < 0 - Mathf.Epsilon)
_touchDir = Dir.Left;
else
_touchDir = Dir.Right;
}
// 当输入的触点数量大于0,且手指不动时
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
_touchDir = Dir.Stop;
}
}
// 根据手势顺序或逆序换图
if(_touchDir != Dir.Stop)
{
if(_touchDir == Dir.Left)
{
curTime += Time.deltaTime;
if(curTime > ration)
{
curTime = 0;
_index = _index == 0 ? _texAll.Length - 1 : _index ;
_plane.renderer.material.mainTexture = _texAll[_index--];
}
}
else
{
curTime += Time.deltaTime;
if(curTime > ration)
{
curTime = 0;
_index = _index == _texAll.Length - 1 ? 0 : _index ;
_plane.renderer.material.mainTexture = _texAll[_index++];
}
}
}
}
}
⑵ unity触屏移动物体
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector3 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0,Space.Wrold);
}
}
⑶ unity3d手机触控
/*
Touch Orbit
Programmed by: Randal J. Phillips (Caliber Mengsk)
Original Creation Date: 12/16/2011
Last Updated: 12/16/2011
Desctiption: Simple orbit by one touch and drag, as well as pinch to zoom with two fingers.
*/
var x:float;
var y:float;
var xSpeed:float;
var ySpeed:float;
var pinchSpeed:float;
var distance:float = 10;
var minimumDistance:float = 5;
var maximumDistance:float = 100;
private var touch:Touch;
private var lastDist:float = 0;
private var curDist:float = 0;
private var gameCamera:Camera;
function Start ()
{
gameCamera = Camera.mainCamera;
}
function Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
//One finger touch does orbit
touch = Input.GetTouch(0);
x += touch.deltaPosition.x * xSpeed * 0.02;
y -= touch.deltaPosition.y * ySpeed * 0.02;
}
if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
{
//Two finger touch does pinch to zoom
var touch1 = Input.GetTouch(0);
var touch2 = Input.GetTouch(1);
curDist = Vector2.Distance(touch1.position, touch2.position);
if(curDist > lastDist)
{
distance += Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
}else{
distance -= Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
}
lastDist = curDist;
}
if(distance <= minimumDistance)
{
//minimum camera distance
distance = minimumDistance;
}
if(distance >= maximumDistance)
{
//maximum camera distance
distance = maximumDistance;
}
//Sets rotation
var rotation = Quaternion.Euler(y, x, 0);
//Sets zoom
var position = rotation * Vector3(0.0, 0.0, -distance) + Vector3(0,0,0);
如果你还有什么不懂的,可以网络搜下:编程回忆录,他们现在正在录制这方面的教程,都是零基础开始,由浅入深。
⑷ TouchdeltaPosition要怎么理解
由于输入响应一般是在update函数里
自然每次响应输入都有一个变化量和变化时间
⑸ Unity 手游 移动物件
你为什么不直接这么写呢?
Euglena.transform.Translate(touchposition*0.01f);
感觉 你的方法 没用对 Translate(float,float,float);是沿着这三个方向移动的意思
要是不做延时跟随 就直接 Euglena.transform.position = touchposition;
如果不希望Z轴发生移动 ,可以new 一个vector3 按照(touchposition.x * 0.01f, touchposition.y * 0.01f,Euglena.transform.position .z)这样 赋值
⑹ TouchdeltaPosition要怎么理解
由于输入响应一般是在update函数里 自然每次响应输入都有一个变化量和变化时间
⑺ TouchdeltaPosition要怎么理解
TouchdeltaPosition要怎么理解
由于输入响应一般是在update函数里 自然每次响应输入都有一个变化量和变化时间