2019
02.03

 

EventSystems 는 IPointer 인터페이스 사용하려다가 남은것.

마우스 드래그 영역을 따로 정해줄거면 IPointer로 처리해야함.

 

1. 전처리문을 사용한 안드로이드, PC 플랫폼 분리 (구식방법)

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

public class mCameraController : MonoBehaviour{

    Vector3 FirstPoint;
    Vector3 SecondPoint;
    float xAngle;
    float yAngle;
    float xAngleTemp;
    float yAngleTemp;

    public Transform Player;

    [HideInInspector]
    public bool isCanRotate = true;
    private bool isMouseDown = false;

    void Start()
    {
        xAngle = 0;
        yAngle = 50;
        transform.rotation = Quaternion.Euler(yAngle, xAngle, 0);
        transform.parent.position = Player.transform.position;
    }
    
    void Update()
    {
        transform.parent.position = Vector3.Lerp(transform.parent.position, Player.position, Time.deltaTime * 10f);

        if (isCanRotate != false)
        {

#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
            if (Input.touchCount > 0)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    FirstPoint = Input.GetTouch(0).position;
                    xAngleTemp = xAngle;
                    yAngleTemp = yAngle;
                }
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    SecondPoint = Input.GetTouch(0).position;
                    xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
                    yAngle = yAngleTemp - (SecondPoint.y - FirstPoint.y) * 90 * 3f / Screen.height; // Y값 변화가 좀 느려서 3배 곱해줌.

                    // 회전값을 40~85로 제한
                    if (yAngle < 40f)
                        yAngle = 40f;
                    if (yAngle > 85f)
                        yAngle = 85f;

                    transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
                }
            }
#else
            // 마우스가 눌림
            if (Input.GetMouseButtonDown(0))
            {
                FirstPoint = Input.mousePosition;
                xAngleTemp = xAngle;
                yAngleTemp = yAngle;
                isMouseDown = true;
            }
            
            // 마우스가 떼짐
            if (Input.GetMouseButtonUp(0))
            {
                isMouseDown = false;
            }

            if (isMouseDown)
            {
                SecondPoint = Input.mousePosition;
                xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
                yAngle = yAngleTemp - (SecondPoint.y - FirstPoint.y) * 90 * 3f / Screen.height; // Y값 변화가 좀 느려서 3배 곱해줌.

                // 회전값을 40~85로 제한
                if (yAngle < 40f)
                    yAngle = 40f;
                if (yAngle > 85f)
                    yAngle = 85f;

                transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
            }
#endif
        }
    }
    
}
 

 

 

2. IPointer를 사용한 안드로이드, PC 플랫폼 통합방법

public class mCameraController : MonoBehaviour, IBeginDragHandler, IDragHandler
{
    Vector3 FirstPoint;
    Vector3 SecondPoint;
    public float xAngle = 0f;
    public float yAngle = 55f;
    float xAngleTemp;
    float yAngleTemp;

    public void OnBeginDrag(PointerEventData eventData)
    {
        BeginDrag(eventData.position);
    }

    public void OnDrag(PointerEventData eventData)
    {
        OnDrag(eventData.position);
    }

    public void BeginDrag(Vector2 a_FirstPoint)
    {
        FirstPoint = a_FirstPoint;
        xAngleTemp = xAngle;
        yAngleTemp = yAngle;
    }

    public void OnDrag(Vector2 a_SecondPoint)
    {
        SecondPoint = a_SecondPoint;
        xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
        yAngle = yAngleTemp - (SecondPoint.y - FirstPoint.y) * 90 * 3f / Screen.height; // Y값 변화가 좀 느려서 3배 곱해줌.

        // 회전값을 40~85로 제한
        if (yAngle < 40f)
            yAngle = 40f;
        if (yAngle > 85f)
            yAngle = 85f;

        transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
    }
}

 

 

eventData.clickCount 으로 탭 더블 탭 구분가능하기도함.

 

3. IPointer를 사용한 더 간단한 방법

using UnityEngine;
using UnityEngine.EventSystems;

public class Obtain_ImageRotater : MonoBehaviour, IDragHandler
{
    public Transform targetObjTrans;

    public float xAngle = 0f;
    public float yAngle = 0f;

    public void OnDrag(PointerEventData eventData)
    {
        xAngle = eventData.delta.x * Time.deltaTime * 8f;
        yAngle = eventData.delta.y * Time.deltaTime * 8f;

        targetObjTrans.Rotate(yAngle, -xAngle, 0, Space.World);
    }
}

 

회전 + 핀치 줌인 아웃

 

https://unity3d.com/kr/learn/tutorials/topics/mobile-touch/pinch-zoom

 

 

 

 

 

 

 

 

 

COMMENT