Unity: TouchController.cs

TouchController.cs

转载: https://gist.github.com/hiroyuki-miyashita/64055dfcaab9623a73e4

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using UnityEngine.EventSystems;

public class TouchController : MonoBehaviour {

	
	float distanceHrizon;
	float distanceVetrical;
	private Vector3 prevMousePos;	// 1ループ前のマウス位置
	private Vector3 inertiaVel;		// 慣性力による速度
	private bool isDragging;		// ドラッグ中
	private float LinerDrag = 10.0f;	// 減速度合
	private float touchedTime;
	private float touchMoveTime;
	private Vector2 touchBegin;
	private Vector2 touchMove;
	private Vector2 touchMiddle;
	private Vector2 touchEnd;
	Vector3 position;
	int deltaPosRate = 0;
	Vector2 deltaPosition;
	float diff_x;
	float diff_y;
	float axisHorizontal;
	float axisVertical;
	float TypeAheadHorizontal;
	float TypeAheadaxisVertical;

	public int Direction { private set; get;}
	
	
	float diffX;
	float diffY;
	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
		//ゲーム中のパネルなりゲージなりをtapしたりすると突き抜ける問題があるので、IsPointerOverGameObjectでブロックする
//		if (EventSystem.current.currentSelectedGameObject != null) 
//		{
//			return;
//		}
		

		
		float horizonPos = Input.GetAxisRaw ("Horizontal");
		axisHorizontal = 0;
		if (gameObject.transform.localPosition.x < 1 && horizonPos == 1) {
			axisHorizontal = horizonPos;
		}
		if (gameObject.transform.localPosition.x > -1 && horizonPos == -1) {
			axisHorizontal = horizonPos;
		}
		axisVertical = Input.GetAxisRaw ("Vertical");
		
		ControllAnimation();
		
	}
	
	
	/*
	 * PlayAnimation
	*/
	void ControllAnimation(){

		/
		if (Input.touchCount == 1) {
			Touch touch = Input.GetTouch (0);
			TouchPhase phase = Input.GetTouch (0).phase;
			switch (phase) {
			case TouchPhase.Began:
				touchBegin = touch.position;
				touchedTime = Time.realtimeSinceStartup;
				isDragging = true;
				break;
			case TouchPhase.Moved:
				Vector3 tmpPos = transform.localPosition;
				touchMove = touch.position;
				deltaPosRate = 50;
				deltaPosition = touch.deltaPosition / deltaPosRate;//最大Viewで90-100,最小で50ぐらいがこのましい
				position.x = (position.x - deltaPosition.x);//移動x
				position.y = tmpPos.y;//移動y
				position.z = tmpPos.z;//移動y
				
				inertiaVel = new Vector3 (touch.deltaPosition.x, touch.deltaPosition.y, 0) * -1;
				
				Vector2 distance = touchBegin - touchMove;
				diff_x = Mathf.Abs (touchBegin.x - touchMove.x);
				diff_y = Mathf.Abs (touchBegin.y - touchMove.y);
				
				if (diff_x > diff_y) {
					//yoko
					axisVertical = 0;
					//left
					if (touchBegin.x > touchMove.x) {
						if (gameObject.transform.localPosition.x > -1) {
							axisHorizontal = -1;
						}
					} else if (touchBegin.x < touchMove.x) {
						if (gameObject.transform.localPosition.x < 1) {
							axisHorizontal = 1;
						}
					}
					
				} else {
					//tate					
					axisHorizontal = 0;
					//left
					if (touchBegin.y > touchMove.y) {
						axisVertical = -1;
					} else if (touchBegin.y < touchMove.y) {
						axisVertical = 1;
					}
					
				}
				break;
			case TouchPhase.Stationary:
				break;
				
			case TouchPhase.Ended:
				isDragging = false;
				touchEnd = touch.position;
				diffX = Mathf.Abs (touchBegin.x - touchEnd.x);
				diffY = Mathf.Abs (touchBegin.y - touchEnd.y);
				if (diffX < 40 && diffY < 40) {
					Debug.Log ("Touch!["+touchEnd.x+", "+touchEnd.y+"]");
				}
				
				break;
				
			case TouchPhase.Canceled:
				//print("Touch index " + touch.fingerId + " cancelled");
				break;
			}
		}
		
		/
		
		

		
		
		
		

			if (axisVertical == 1){
				//StartCoroutine("up"); 
				Debug.Log ("Up!!!!!!!!!!");
			}else if (axisVertical == -1) {
				//StartCoroutine("down");
				Debug.Log ("Down!!!!!!!!!!");
			}else if(axisHorizontal == -1){
				//StartCoroutine("left");
				Debug.Log ("Left!!!!!!!!!!");
			}else if(axisHorizontal == 1){
				//StartCoroutine("right");
				Debug.Log ("Right!!!!!!!!!!");
			}
			
		axisVertical = 0;
		axisHorizontal = 0;
	}
	
	
	//animator.SetTrigger
	private void AnimPlay(string AnimName){
		Debug.Log (">>>   anim play ["+AnimName+"]   >>>>>>>>>>>>>>");
                //ここらへんでAnimatorやらを呼んで動かす
	}
	float transNum;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值