using System.Collections;
public class TouchManager : MonoBehaviour {
// TODO Screen.sizeによって拡大率を変える必要がありそう
private GameObject mainCamera; // メインカメラ
private float orthoSizeMax = 6.0f; // 最大縮小サイズ
private float orthoSizeMin = 3.0f; // 最大拡大サイズ
private float baseOrthographicSize = 5.0f; // デフォルト拡大サイズ
private float orthographicSize; // カメラの拡大サイズ
private float nowOrthgraphicSize; // 現在のカメラの拡大サイズ
private Vector2 swipeVector; // スワイプで動く量
public GameObject sprite;
mainCamera = GameObject.Find("Main Camera");
mainCamera.camera.orthographicSize = baseOrthographicSize;
orthographicSize = mainCamera.camera.orthographicSize;
nowOrthgraphicSize = orthographicSize;
EasyTouch.On_PinchIn += On_PinchIn;
EasyTouch.On_PinchOut += On_PinchOut;
EasyTouch.On_PinchEnd += On_PinchEnd;
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;
EasyTouch.On_SwipeStart -= On_SwipeStart;
EasyTouch.On_Swipe -= On_Swipe;
EasyTouch.On_SwipeEnd -= On_SwipeEnd;
private void On_PinchIn( Gesture gesture ){
float zoom = Time.deltaTime * gesture.deltaPinch / 5.0f;
orthographicSize = mainCamera.camera.orthographicSize + zoom;
if(orthographicSize > orthoSizeMax){
orthographicSize = orthoSizeMax;
private void On_PinchOut( Gesture gesture ){
float zoom = Time.deltaTime * gesture.deltaPinch / 5.0f;
orthographicSize = mainCamera.camera.orthographicSize - zoom;
if(orthographicSize < orthoSizeMin){
orthographicSize = orthoSizeMin;
private void On_PinchEnd( Gesture gesture ){
private void On_SwipeStart( Gesture gesture ){
private void On_Swipe( Gesture gesture ){
private void On_SwipeEnd( Gesture gesture ){
if (gesture.touchCount==1 ){
swipeVector = gesture.deltaPosition * gesture.deltaTime;
if (orthographicSize != nowOrthgraphicSize) {
float tmp = (orthographicSize - nowOrthgraphicSize) / 10.0f;
nowOrthgraphicSize += tmp;
if(Mathf.Abs(tmp) < 0.1f) nowOrthgraphicSize = orthographicSize;
mainCamera.camera.orthographicSize = orthographicSize;
if(Input.GetMouseButtonDown(0))
StartCoroutine("CameraDragEvent");
if( swipeVector != Vector2.zero){
Vector2 tmp = swipeVector / 10.0f;
if(Mathf.Abs(Vector2.Distance(Vector2.zero, tmp)) <= 0.1f)
swipeVector = Vector2.zero;
Vector3 cameraPos = mainCamera.transform.position;
Vector3 tmp2 = new Vector3(tmp.x, tmp.y, 0);
mainCamera.transform.position -= tmp2;
private IEnumerator CameraDragEvent()
Vector2 prevPosition = mainCamera.camera.ScreenToWorldPoint(Input.mousePosition);
Vector2 curPosition = mainCamera.camera.ScreenToWorldPoint(Input.mousePosition);
Vector3 prevMousePosition = Input.mousePosition;
Vector3 curMousePosition = Input.mousePosition;
while (Input.GetMouseButton(0))
if (Input.touchCount > 1)
curMousePosition = Input.mousePosition;
if (prevMousePosition != curMousePosition)
curPosition = mainCamera.camera.ScreenToWorldPoint(curMousePosition);
Vector2 posDiff = (curPosition - prevPosition);
Vector3 posDiff3 = new Vector3(posDiff.x, posDiff.y, 0f);
mainCamera.transform.localPosition += -posDiff3;
prevMousePosition = curMousePosition;
private void LimitCheckAndFix()
SpriteRenderer sr = sprite.GetComponent<SpriteRenderer>();
Vector3 size = sr.bounds.size;
float aspect = mainCamera.camera.aspect;
if (mainCamera.transform.position.x < size.x / -2.0f + nowOrthgraphicSize * aspect){
mainCamera.transform.SetPositionX(size.x / -2.0f + nowOrthgraphicSize * aspect);
if (mainCamera.transform.position.x > size.x / 2.0f - nowOrthgraphicSize * aspect){
mainCamera.transform.SetPositionX(size.x / 2.0f - nowOrthgraphicSize * aspect);
if (mainCamera.transform.position.y < size.y / -2.0f + nowOrthgraphicSize){
mainCamera.transform.SetPositionY(size.y / -2.0f + nowOrthgraphicSize);
if (mainCamera.transform.position.y > size.y / 2.0f - nowOrthgraphicSize){
mainCamera.transform.SetPositionY(size.y / 2.0f - nowOrthgraphicSize);