⑴ 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函數里 自然每次響應輸入都有一個變化量和變化時間