手指触摸滑动物体带惯性的旋转 以及放大缩小(用插件easytouch 控制物体的旋转和缩放)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;


public class AR_TouchController : MonoBehaviour {




public GameObject[] scaleGameobject = new GameObject[]{} ;
public GameObject objectToRotate;
public float rotateSpead;
public float   scaleSpeed;
public bool smothMovement;


private bool interactive = true;
static AR_TouchController myInstance;
static int instances = 0;
public static AR_TouchController Instance
{
get
{
if (myInstance == null ) {
myInstance = FindObjectOfType( typeof(AR_TouchController )) as AR_TouchController;
}
return myInstance;
}
}

//Called at the biginning of the game
void Start()
{
//Calibrates the myInstance static variable
instances++;
if (instances > 1) {
// Debug.LogError ("Warning: There are more than one AR_TouchController at the level");
}else {
myInstance = this;
}
ResourceModel.myEvent += FindSon;
}


// Subscribe to events
void OnEnable(){
EasyTouch.On_SwipeStart += On_SwipeStart;
EasyTouch.On_Swipe += On_Swipe;
EasyTouch.On_SwipeEnd += On_SwipeEnd;


EasyTouch.On_PinchIn += On_PinchIn;
EasyTouch.On_PinchOut += On_PinchOut;
EasyTouch.On_PinchEnd += On_PinchEnd;
}

void OnDisable(){
UnsubscribeEvent();

}

void OnDestroy(){
UnsubscribeEvent();
}

void UnsubscribeEvent(){
EasyTouch.On_SwipeStart -= On_SwipeStart;
EasyTouch.On_Swipe -= On_Swipe;
EasyTouch.On_SwipeEnd -= On_SwipeEnd;
EasyTouch.On_PinchIn -= On_PinchIn;
EasyTouch.On_PinchOut -= On_PinchOut;
EasyTouch.On_PinchEnd -= On_PinchEnd;
}


// At the swipe beginning 
private void On_SwipeStart( Gesture gesture){
//Pich = objectToRotate.transform.eulerAngles.y;
//Yaw = objectToRotate.transform.eulerAngles.x;
}

float Yaw = 0;
float Pich = 0;
float targetYaw = 0;
float targetPich = 0;
// During the swipe
private void On_Swipe(Gesture gesture){
if (interactive) {
targetYaw = gesture.deltaPosition.x;
targetPich = gesture.deltaPosition.y;
if (smothMovement) {
// Yaw = Mathf.Lerp (Yaw, targetYaw, Time.deltaTime * rotateSpead);
// Pich = Mathf.Lerp (Pich, targetPich, Time.deltaTime * rotateSpead);
} else {
Yaw = Mathf.Lerp (Yaw, targetYaw, 1);
Pich = Mathf.Lerp (Pich, targetPich, 1);
}


}
}


void Update(){
if (objectToRotate == null)
return;
Yaw = Mathf.Lerp (Yaw, targetYaw, Time.deltaTime * rotateSpead);
Pich = Mathf.Lerp (Pich, targetPich, Time.deltaTime * rotateSpead);
//Debug.Log ("Yaw: " + Yaw + "  targetYaw:" + targetYaw);
if ((Yaw != targetYaw) || (Pich != targetPich)) {
objectToRotate.transform.Rotate (transform.up, -Mathf.Deg2Rad * Yaw * 20 * rotateSpead, Space.Self);
objectToRotate.transform.Rotate (transform.right, Mathf.Deg2Rad * Pich * 20 * rotateSpead, Space.World);
}
}
// At the swipe end 
private void On_SwipeEnd(Gesture gesture){
//Pich = objectToRotate.transform.eulerAngles.y;
//Yaw = objectToRotate.transform.eulerAngles.x;
targetYaw = 0;
targetPich = 0;
}


// At the pinch in
private void On_PinchIn(Gesture gesture){
if (interactive) {
float zoom = Time.deltaTime * gesture.deltaPinch * scaleSpeed;
Vector3 scale = objectToRotate.transform.localScale;
float zoomX = Mathf.Clamp (scale.x - zoom, 0.1f,3);
float zoomY = Mathf.Clamp (scale.y - zoom, 0.1f,3);
float zoomZ = Mathf.Clamp (scale.z - zoom, 0.1f,3);
objectToRotate.transform.localScale = new Vector3 (zoomX, zoomY, zoomZ);
//Debug.Log (objectToRotate.transform.localScale);
}
}
// At the pinch out
private void On_PinchOut(Gesture gesture){
if (interactive) {
float zoom = Time.deltaTime * gesture.deltaPinch * scaleSpeed;
Vector3 scale = objectToRotate.transform.localScale;
float zoomX = Mathf.Clamp (scale.x + zoom, 0.1f,3);
float zoomY = Mathf.Clamp (scale.y + zoom, 0.1f,3);
float zoomZ = Mathf.Clamp (scale.z + zoom, 0.1f,3);
objectToRotate.transform.localScale = new Vector3 (zoomX, zoomY, zoomZ);
//Debug.Log (zoomX);
}
}


// At the pinch end
private void On_PinchEnd(Gesture gesture){

}


// public mathod
public void SetTouchable(bool canTouch){
interactive = canTouch;
}


void FindSon(){
for (int i=0;i<scaleGameobject.Length;i++)
{
if (scaleGameobject [i].transform.childCount >= 1) {
objectToRotate = scaleGameobject [i].transform.GetChild (0).transform.transform.gameObject;
}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值