์๋ฝ, ์์ฆ๋ ๋ํธ ์๋ฐฑ๋ฒ์ฉ ์ฐ์๋ค๊ฐ ์ง์ ๋ค๋ฅผ ๋ฐ๋ณตํ๊ณ ์๋๋ฐ ์ ์ ์ฐํ๋ ๋ชจ์ ์ด ์์ด์ ์ค๋์ ์ฝ๋ ๊ด๋ จ ๋ก์ง์ ํ๋ฒ ๋ค๊ณ ์๋ดค์.
๋น์ฐํ ์ ๋ํฐ์ ์ค์์ดํ ๋ํ ์ ์ฝ๋๊ฐ ์์ ์ค ์์๋๋ ํฐ์น ์ & ๋ค์ด ์ฝ๋๋ฐ์ ์๋๋ผ๊ณ ?
๊ฐ๋จํ ๋ก์ง์ด์ง๋ง ์ฒ์ ๊ตฌํํด๋ณด๋ ์ฌ๋๋ค์ ๋งํ ์๋ ์์ผ๋ ๊ณต์ ๊ฒธ ํด์ ๊ฒธ์ฌ๊ฒธ์ฌ ์ฌ๋ ค๋ด.
-------------------------------
์ฌ์ค ์ค์์ดํ ๋ํ ์ ๋ก์ง ์์ฒด๋ ๊ฐ๋จํจ. ์ธํ ๋ค์ด & ์ ๊ฐ๊ฐ์ ํฌ์ง์ ์ ์ ์ฅํด ๋๋ค๊ฐ, ๋ฒกํฐ ๊ณ์ฐํด์ ์ ์ด๋ ๋ฐฉํฅ์ผ๋ก ์ค์์ดํํ๊ตฐ, ์ ์์๋ด๋ฉด ๋จ.
ํ์ง๋ง ๋๋ ์ธํ up์ด ํธ์ถ๋์ง ์์๋ ์ผ์ ๊ฑฐ๋ฆฌ ์ด์์ ์ค์์ดํํ๋ฉด ๋ํ ์ ๋๋๋ก ๊ตฌํํ๊ณ ์ถ์ด์ ์ฝ๊ฐ ๋ก์ง์ ๋นํ์ด์ผ ํ๋ ๋ถ๋ถ์ด ์์์.
๊ทธ๋ผ ๋ก์ง์ ๋๊ฐ ๋ค์๊ณผ ๊ฐ์ด ๋๊ฒ ์ง?
์ฝ๋๋ก ์ฎ๊ธฐ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ ๊ฑฐ์ผ
Vector2 touchDownPos;
Vector2 swipeDirection;
void processMobileInput()
ย ย ย ย ย {
ย ย ย ย ย ย ย ย ย if (Input.touches.Length > 0)
ย ย ย ย ย ย ย ย ย {
ย ย ย ย ย ย ย ย ย ย ย ย ย Touch t = Input.GetTouch(0);
ย ย ย ย ย ย ย ย ย ย ย ย ย if (t.phase == TouchPhase.Began)
ย ย ย ย ย ย ย ย ย ย ย ย ย {
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย //save began touch 2d point
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย touchDownPos = new Vector2(t.position.x, t.position.y);
ย ย ย ย ย ย ย ย ย ย ย ย ย }
ย ย ย ย ย ย ย ย ย ย ย ย ย else if (t.phase == TouchPhase.Moved)
ย ย ย ย ย ย ย ย ย ย ย ย ย {
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Vector2 currentTouchPos = new Vector2(t.position.x, t.position.y);
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย bool swipeDetected = checkSwipe(touchDownPos, currentTouchPos);
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย swipeDirection = (currentTouchPos - touchDownPos).normalized;
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย if (swipeDetected)
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย onSwipeDetected(swipeDirection);
ย ย ย ย ย ย ย ย ย ย ย ย ย }
ย ย ย ย ย ย ย ย ย ย ย ย ย else if (t.phase == TouchPhase.Ended)
ย ย ย ย ย ย ย ย ย ย ย ย ย {
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Debug.Log("Swipe End!");
ย ย ย ย ย ย ย ย ย ย ย ย ย }
ย ย ย ย ย ย ย ย ย }
ย ย ย ย ย }
TouchPhase.Ended ๊ฒฝ์ฐ์๋ ํฐ์น๊ฐ ์ข ๋ฃ๋๋ ์์ ์ธ๋ฐ, ์ฆ ์๊ฐ๋ฝ์ ๋๋ค๋ ๋ง์ด์ง๋ง Swipe Detected ๋ก์ง์ด ์์.ย
์๋๋ฉด, ๋๋ ์๊ฐ๋ฝ์ ๋ ์ดํ์ ์ค์์ดํ ๋ํ ์ ์ ํ๋ ๊ฒ ์๋๋ผ ์๊ฐ๋ฝ์ด ์ผ์ ๊ฑฐ๋ฆฌ๋ฅผ ์์ง์ธ ๋ค์์ ์ค์์ดํ ๋ํ ์ ์ ํ๋๋ก ๊ตฌํํ๊ณ ์ถ์์ผ๋๊น.
์ค์์ดํ๋ฅผ ์ฒดํฌํ ์ดํ ๊ด๋ จ ๋ก์ง์ onSwipeDetected()์ ๋ค์ด๊ฐ๊ฒ ๋๊ณ , ์ฌ๊ธฐ์ ์์ํ ์ฒ๋ฆฌ๋ฅผ ํ ๊ฒ์ธ๋ฐ...๊ทธ ์ ์ checkSwipe ๋ก์ง์ ์ดํด๋ณด์.
bool checkSwipe(Vector3 downPos, Vector3 currentPos)
{
Vector2 currentSwipe = currentPos - downPos;
if (currentSwipe.magnitude >= minSwipeDist)
{
//Debug.Log("Swipe Detected : " + currentSwipe);
return true;
}
return false;
}
์์ฒญ ๊ฐ๋จํจ, ๊ทธ๋ฅ ์๊ฐ๋ฝ ์ฒ์ ํฐ์นํ ์์น์ Update()์์ ๋ฐ์ ํ์ฌ ์๊ฐ๋ฝ ์์น๋ฅผ ๊ฒ์ฌํด์ ์ผ์ ๊ฑฐ๋ฆฌ ์ด์ ์์ง์ด๋ฉด Swipe Detected๋ก ํ์ ํ๋๋ก ํ๋ ๊ฒ ๋.
์ฌ๊ธฐ์ minSwipeDist๋ ์ ๋นํ ๊ฑฐ๋ฆฌ๊ฐ์ ์ ๋ ฅํด์ผ ํ๋๋ฐ, ๋๋ ์คํฌ๋ฆฐ ์ฌ์ด์ฆ์ 1/14 ์ ๋๋ก ์ค์ ํด ๋จ์ด.ย
void Awake()
{
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
minSwipeDist = Mathf.Max(screenSize.x, screenSize.y) / 14f;
Debug.Log("MinSwipeDist : " + minSwipeDist);ย
ย }
๊ทผ๋ฐ ํ๋ธ๋ ์ข ๋ฅ๋ ์ด๋ฐ ๊ฑฐ์์ ํฐ์น๊ฐ์ด ์ ์ข์์ง๋ ๋ชฐ๋ผ์ ์ด ๋ถ๋ถ ๋ก์ง์ ๋๋ ์ข ์๋ฌธ์ด ๋๋ค. Swipe Detection Distance ๊ด๋ จํด์ ์๋ ๊ฒ ์๋ ๋ถ๋ค์ ์กฐ์ธ ์ข...
์ผ๋จ ๋ชจ๋ฐ์ผ ํฐ์น ๋ก์ง์ ์์ ๊ฐ์ง๋ง, PC์์ ์ค์์ดํ ์ ๋ ฅ์ด๋ ๋ค์ํ ๋๋ฒ๊ทธ๋ฅผ ์ํด์ ์ผ๋ถ๋ฌ PC์ฉ ๋ก์ง๋ ์งฐ์ด.ย
๋ง์ฐ์ค Down, Up์ ์ด์ฉํด์ ๊ตฌํํ๋๋ฐ, ๋ก์ง์ ์ ์ฒด์ ์ผ๋ก ๋น์ทํ๋ ์ด์ชฝ ์ค๋ช ์ ์๋ตํ๊ณ ...
C++์ฒ๋ผ ์ ์ฒ๋ฆฌ๊ธฐ๋ฅผ ์ด์ฉํด์ ๋ชจ๋ฐ์ผ๊ณผ PC ๋ก์ง์ ๋ถ๋ฆฌํด์ค ์ ์์ผ๋ ๋ค์๊ณผ ๊ฐ์ด Update๋ฅผ ์ฒ๋ฆฌํ ์ ์๊ฒ ์ง?
void Update()
{
#if UNITY_ANDROID
processMobileInput();
#else
processInput();
#endif
}
์ด ์์ ์์ ์ ์ฒด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ :ย
using UnityEngine;
using System.Collections;
using System;
public class GarlicInputManager : MonoBehaviour
{
Vector2 screenSize;
float minSwipeDist;
Vector2 swipeDirection;
void Awake()
{
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
minSwipeDist = Mathf.Max(screenSize.x, screenSize.y) / 14f;
Debug.Log("MinSwipeDist : " + minSwipeDist);
}
void Update()
{
#if UNITY_ANDROID
processMobileInput();
#else
processInput();
#endif
}
Vector2 touchDownPos;
void processMobileInput()
{
if (Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
//save began touch 2d point
touchDownPos = new Vector2(t.position.x, t.position.y);
}
else if (t.phase == TouchPhase.Moved)
{
Vector2 currentTouchPos = new Vector2(t.position.x, t.position.y);
bool swipeDetected = checkSwipe(touchDownPos, currentTouchPos);
swipeDirection = (currentTouchPos - touchDownPos).normalized;
if (swipeDetected)
onSwipeDetected(swipeDirection);
}
else if (t.phase == TouchPhase.Ended)
{
Debug.Log("Swipe End!");
}
}
}
Vector3 mouseDownPos;
void processInput()
{
if (Input.GetMouseButtonDown(0) == true)
{
mouseDownPos = Input.mousePosition;
}
else if (Input.GetMouseButton(0) == true)
{
bool swipeDetected = checkSwipe(mouseDownPos, Input.mousePosition);
swipeDirection = (Input.mousePosition - mouseDownPos).normalized;
if (swipeDetected)
onSwipeDetected(swipeDirection);
}
else if (Input.GetMouseButtonUp(0) == true)
{
Debug.Log("Swipe End!");
}
}
bool checkSwipe(Vector3 downPos, Vector3 currentPos)
{
//๋๋ ์ด ๋ฑ์ผ๋ก ์ธํด ์ธํ์ ๋ง๊ณ ์๋ค.
if (isInputBlocked == true)
return false;
Vector2 currentSwipe = currentPos - downPos;
if (currentSwipe.magnitude >= minSwipeDist)
{
//Debug.Log("Swipe Detected : " + currentSwipe);
return true;
}
return false;
}
void onSwipeDetected(Vector2 swipeDirection)
{
//Swipeย
}
private bool isInputBlocked = false;
public void blockInput()
{
isInputBlocked = true;
}
public void unBlockInput()
{
isInputBlocked = false;
}
}
1)์ค๊ฐ์ ๊ตต๊ฒ ์ด normalized๋ ์ค์์ดํ ๋ฐฉํฅ์ ์ฒ๋ฆฌํ๋ ๋ก์ง. ํฌ๊ธฐ๊ฐ 1์ง๋ฆฌ์ธ ๋ฒกํฐ๋ก ๋ ธ๋ง๋ผ์ด์ฆ ์์ผ์ค.ย
์ค๋ธ์ ํธ๊ฐ ์ด๋ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋์ง ์๊ณ ์ถ์ ๋ ์ ์ฉํ๊ฒ ์ธ ์ ์์.
์ ๋ํฐ์์ ์ ๊ณตํ๋ ๋ฒกํฐ๋ ์ธ๋งํ ํจ์๊ฐ ์์ฃผ ๋ง์ผ๋ ์ ์ฉํ๊ฒ ์ ์ฐ๋ ๊ฒ ์ข์. ๊ณ ๋ฑํ์ ๋ ์ํ ์ ์กธ๊ณ ๋ค์์ผ๋ฉด ๊ฐ๋จํ ๋ฒกํฐ ๊ด๋ จ ์ด๋ก ์ ์๊ณ ์๊ฒ ์ง?(๋ชจ๋ฅธ๋ค๋ฉด, ๋ฒกํฐย ๊ด๋ จ ๊ธฐ๋ณธ ์ง์์ ๊ณต๋ถํ๋ ๊ฒ ๋ฐ์ฏค ํ์๋ผ๊ณ ์๊ฐํจ...์๊ฐํ๋ ์ด์์ผ๋ก ์์ฃผ, ์ ์ฉํ๊ฒ ์ฐ์ด๋๊น...)
2)์ ์ผ ์๋ซ๋จ์ ๋ณด๋ฉด blockInput ๊ด๋ จ ๋ก์ง์ด ์์ด.
๊ณต๊ฒฉ ๋ชจ์ ์ ์ทจํ๋ ๋์ค(Attack Start) ์ํ ๋ฑ๋ฑ์ ๊ฒฝ์ฐ, ๊ฐ์ ์ ์ผ๋ก ์ฌ์ฉ์์ ์ธํ์ ๋ง์๋ฒ๋ ค์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ ์ด๋ฅผ ์ํด ๊ตฌํํ์.ย
checkswipe()์์ ๊ฐ์ ๋ก false๋ฅผ ๋๋ ค์ค๋ฒ๋ฆฌ๋ฉด ๊ฐ๋จํ ํด๊ฒฐ๋๊ฒ ์ง?
----------------------------------
์ฌ๊ธฐ๊น์ง ์ง๊ณ ๋์ ๋ฐ๊ฒฌํ ๋ฌธ์ ๋ย
1.์ฌ์ฉ์๊ฐ ์๊ฐ๋ฝ์ ๋ผ์ง ์๊ณ ํ๋ฉด์ ํ ๋ฒ์ ์ญ์ฑ ๋ฐ ๊ฒฝ์ฐ, 2๋ฒ ์ด์์ swipe input์ด ๋ค์ด๊ฐ๋ฒ๋ฆผ.
->์์ธ์ touch down pos๋ ๊ทธ๋๋ก์ธ๋ฐ current pos๋ ๊ณ์ ์ ๋ฐ์ดํธ ๋๊ณ ์์ผ๋, ์ ๋ ฅ์ ๊ฐ์ ๋ก ๋ง๊ณ ์๋ isInputBlocked๊ฐ false๊ฐ ๋์๋ง์ ๊ณง๋ฐ๋ก ํฐ์น ์ ๋ ฅ์ด ๋ค์ด๊ฐ๋ฒ๋ฆฌ๋ ๊ฒ.
ํ ๋ฒ ์ค์์ดํ๋ฅผ ํ์ผ๋ฉด, ์๊ฐ๋ฝ์ ๋ผ๊ณ , ๋ค์ ํฐ์นํ ํ, ์ค์์ดํ๋ฅผ ํ๋๋ก ๊ตฌํํด ์ฃผ๋ฉด ํด๊ฒฐ๋๊ฒ ์ง?
2.์~~~์ฒญ ์ฒ์ฒํ ์๊ฐ๋ฝ์ ๋ฐ์ด๋, ํ๋ฉด์์ ์๊ฐ๋ฝ์ ๋ผ์ง ์์ผ๋ฉด ์ผ์ ๊ฑฐ๋ฆฌ๋ฅผ ์์ง์ด์๋ง์ ์ค์์ดํ ์ธํ์ด ๋ค์ด๊ฐ.
->์ด๊ฑด ๋ํธ ์ฐ๋๋ผ ์์ง ๋ชป ๊ณ ์นจ, ์๋ง ํฐ์น ๋ค์ด์์ Timestamp๋ฅผ ๊ธฐ๋กํด๋๊ณ (์ค์์ดํ ๊ฑฐ๋ฆฌ / ์ค์์ดํ ์๊ฐ = ์ค์์ดํ ์๋) ๋ฅผ ๊ณ์ฐํด์ ์ผ์ ์คํผ๋ ์ด์์ผ๋ก ์ค์์ดํํ์ผ๋ฉด ์ ๋ ฅ์ผ๋ก ์ฒ๋ฆฌํด์ฃผ๋ฉด ๋๊ฒ ์ง? ์๋๋ฉด ์ฝ๋ฃจํด ๋ฑ์ผ๋ก 0.5~1์ด๋ง๋ค touch down pos๊ฐ ๋ฆฌ์ ๋๋๋ก ํด ์ค๋ ๋ ๊ฒ ๊ฐ๋ค.ย
1๋ฒ์ ํด๊ฒฐํ๊ณ 2๋ฒ์ ์์ง ๋ฏธ๋ฃจ๊ณ ์๋๋ฐ, 1๋ฒ์ ๊ทธ๋ฅ boolean ํ๋๊ทธ๋ฅผ ์ฃผ๋ฉด ํด๊ฒฐ๋๋ ๋ฌธ์ ๋๊น ๋ค์๊ณผ ๊ฐ์ด ์ง๋ฉด ๋ ๊ฑฐ์ผ
bool swiped = false;
void processMobileInput()
{
if (Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
//save began touch 2d point
touchDownPos = new Vector2(t.position.x, t.position.y);
swiped = false; //์๊ฐ๋ฝ์ ๋ ํ ๋ค์ ํ๋ฉด์ ํฐ์นํ๊ธฐ ์ ์๋ ์ ๋ ฅ์ ๋ง๋๋ค
}
else if (t.phase == TouchPhase.Moved)
{
Vector2 currentTouchPos = new Vector2(t.position.x, t.position.y);
bool swipeDetected = checkSwipe(touchDownPos, currentTouchPos);
swipeDirection = (currentTouchPos - touchDownPos).normalized;
if (swipeDetected)
onSwipeDetected(swipeDirection);
}
else if (t.phase == TouchPhase.Ended)
{
Debug.Log("Swipe End!");
swiped = true;
}
}
}
void processInput()
{
if (Input.GetMouseButtonDown(0) == true)
{
mouseDownPos = Input.mousePosition;
swiped = false;
}
else if (Input.GetMouseButton(0) == true)
{
bool swipeDetected = checkSwipe(mouseDownPos, Input.mousePosition);
swipeDirection = (Input.mousePosition - mouseDownPos).normalized;
if (swipeDetected)
onSwipeDetected(swipeDirection);
}
else if (Input.GetMouseButtonUp(0) == true)
{
Debug.Log("Swipe End!");
swiped = true;
}
}
bool checkSwipe(Vector3 downPos, Vector3 currentPos)
{
//์๊ฐ๋ฝ์ ์์ง ์ ๋์ง๋ง ์ธํ์ ์ ๋ ฅ๋ฐ์๋ค.
if (swiped == true)
return false;
//๋๋ ์ด ๋ฑ์ผ๋ก ์ธํด ์ธํ์ ๋ง๊ณ ์๋ค.
if (isInputBlocked == true)
return false;
Vector2 currentSwipe = currentPos - downPos;
if (currentSwipe.magnitude >= minSwipeDist)
{
//Debug.Log("Swipe Detected : " + currentSwipe);
return true;
}
return false;
}
void onSwipeDetected(Vector2 swipeDirection)
{
//Swipe, ๊ฐ์ข ๊ฒ์ ๋ก์ง์ ์ด ์์ ๊ตฌํ
swiped = true;
}
์ด์ swipeDirection์ ๋ฐ๋ผ ์ฌ์ฉ์๊ฐ ์ํ๋ ๋ก์ง์ onSwipeDetected์ ๋ฃ๊ณ ์ฒ๋ฆฌํ๋ฉด ๋ ๊ฑฐ์ผ.
---------------------------------------------
**๋ณด๋์ค - Action์ ์ด์ฉํ ์ด๋ฒคํธ ๊ธฐ๋ฐ์ ์ธํ ์์คํ
์กฐ๊ธ ์ด๋ ค์ธ ์๋ ์๋ ๋ถ๋ถ์ธ๋ฐ, ์์คํ ๊ตฌํ ์์ ๋งค์ฐ ์ ์ฉํ ๋ถ๋ถ์ด๋ผ์ย ์กฐ๊ธ ์ฒจ๊ฐํจ.
์์ ์ ์๋๋ก์ด๋ ๋ ํํ๋ฉด์ ๋๋ ๊ฑด๋ฐ On~~~ ๋ฑ์ ์ด๋ฒคํธ ๊ธฐ๋ฐ ํจ์ ํธ์ถ ์์คํ ์ ์์ฒญ ํธ๋ฆฌํ๊ณ ์ข์.ย
ํจ์ ์ด๋ฆ์์๋ถํฐ ์ด๋ ์์ ์ ์ด ํจ์๊ฐ ํธ์ถ๋๋์ง ์ ์ ์๊ณ , ๊ฐ์ฒด์งํฅ์ ๋ ผ๋ฆฌ์๋ ์ ๋ค์ด๋ง๋ ํธ์ธ ๊ฒ ๊ฐ๊ณ ...
์ด์จ๋ c#์ delegate ์์คํ ์ ์ฐ๋ฉด ์ด๊ฑธ ์์ฒญ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋๋ฐ, ๋ธ๋ฆฌ๊ฒ์ดํธ๋ย ์ฝ๊ฒ ๋งํ์๋ฉด ํจ์๋ฅผ ๋ณ์์ฒ๋ผ ๋ค๋ฃจ๋ ๊ฑฐ์ผ.
์๋ฅผ ๋ค์๋ฉด add() ํจ์์ subtract() ํจ์๊ฐ ์๊ณ , delegate๋ก ์ ์ธํ addAndSubtract ๋ผ๋ ๋ณ์๊ฐ ์๋ค๊ณ ํ์ ๋,
addAndSubtract += add;
addAndSubtract += subtract;
//add();
//subtract();
//์๋ ์ฝ๋๋ ์ 2์ค๊ณผ ๊ฐ์ ํจ๊ณผ๋ฅผ ๋ณด์
addAndSubtract();
์์ ์ฝ๋๋ ์์ฒญ ๋์ถฉ ์ค๋ช ํ ๊ฑฐ๋๊น ๊ทธ๋ฅ ๋์ถฉ ๊ทธ๋ฐ ๊ฑฐ๊ตฌ๋ง~ ํ๊ณ ๊ฐ๋ ๋ง ์ก๊ณ ๋์ด๊ฐ์
C#์์๋ Action์ด๋ Func ํด๋์ค๋ฅผ ์ด์ฉํด์ ํจ์ฌ ์ฝ๊ฒ ๋ง๋ค ์ ์๊ฒ ๋์์ฃผ๋๋ฐ, ์ ๋ชจ๋ฅธ๋ค๋ฉด ์ด ๋ถ๋ถ์ ์คํตํ๊ฑฐ๋ ๊ด๋ จ ์๋ฃ๋ฅผย ๊ตฌ๊ธ์์ ๊ฒ์ํด์ ๊ณต๋ถํด๋ณด๊ณ ๊ณ์ํ๋ ๊ฑธ ์ถ์ฒํจ. ์์ธํ ์ค๋ช ํ๊ธฐ์ ์ฌ์ค ๋๋ ๊ทธ๋ฆฌ ์ ํํ๊ฒ ์๋ ๊ฑด ์๋ ๊ฒ ๊ฐ๊ณ , ํจ์ฌ ์ ์ฉํ ์๋ฃ๋ ๋ง๊ณ ...
์ด์จ๋ ๊ณ์ํ์๋ฉด, ์ ์ฝ๋์์๋ OnSwipe~~~ ํจ์์ Action์ย ์์ฉํ ๊ฑฐ์ผ.
์?
OnSwipe~~ ํจ์์์ swipeDirection ๋ฒกํฐ๋ฅผ ์์ฉํด์ ์ฝ๋๋ฅผ ๊ตฌํํ๋ฉด, ํ์ฐ์ ์ผ๋ก ๊ฒ์ ์์คํ ๋ก์ง๊ณผ ์ธํ ์์คํ ๋ก์ง์ด ์์ฌ๋ฒ๋ฆฌ๊ฒ ๋ผ.
๊ฒ์ ๋ก์ง์ด ๊ฐ๋จํ ๊ฒฝ์ฐ์๋ ๋ณ ๋ฌธ์ ๊ฐ ์ ๋๊ฒ ์ง๋ง, ์ ์ ๋ณต์กํด์ง์๋ก ์ธํ ๋ก์ง๊ณผ ์์คํ ๋ก์ง์ด ๊ผฌ์ด๋ฉด์ ์ฝ๋๋ ๊ทธ์ง๊ฐ์์ง๊ณ ๊ฐ๋ฐ ์๋๋ ๋๋ ค์ง๋ค๊ฐ ์ ์๋ฐ ์ ๋ง๋ค์ด, ํ๋ฉด์ ๋๋ ค์ณ๋ฒ๋ฆฌ๊ฒ ์ง?
๊ทธ๋์ ๊ฐ๋ฅํ๋ฉด ๋ฏธ๋ฆฌ๋ฏธ๋ฆฌ ๋ก์ง์ ๋ถ๋ฆฌํด ๋๋ ๊ฑฐ์ผ. (์ฌ์ค ์ด ๋ฉด์์ ๋ณด์๋ฉด, ์์ processInput()๊ณผ processMobileInput() ๋ก์ง๋, ์ค๋ณต ๋ก์ง์ด 2๋ฒ ๋ค์ด๊ฐ๊ณ ์๊ธฐ ๋๋ฌธ์ ๋ฆฌํฉํ ๋ง์ด ํ์๊ฒ ์ง? ์ข์ด๋ฐ ํด์ผ์ง...)
์ด ๋ ๋งค์ฐ ์ ์ฉํ๊ฒ ์ฐ์ผ ์ ์๋ ๊ฒ delegate ๊ด๋ จ ์์คํ ์ด๊ธฐ ๋๋ฌธ์ ์ ์ด๋๋ ๊ฒ.
์์์ ์ง OnSwipe~~ ๋ก์ง์๋ ์ด๋ ๋ฐฉํฅ์ผ๋ก ํฐ์น ์ธํ์ด ๋ค์ด๊ฐ๋์ง ์๋ ค์ฃผ๋ Vector2 ํ๋ผ๋ฏธํฐ๊ฐ ์์ด.ย
๋ฆฌํด๊ฐ์ด ์๋ ํจ์๋ Action์ ์ฐ๋ฉด ํธ๋ฆฌํ๋๊น, ๋ค์๊ณผ ๊ฐ์ด ์ ์ธํด.
Action<Vector2> actionOnSwipeDetected;
๊ทธ๋ฆฌ๊ณ ์์์ ์ง OnSwipe ๋ก์ง ์์๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ๋ฉด :ย
void onSwipeDetected(Vector2 swipeDirection)
{
swiped = true;
actionOnSwipeDetected(swipeDirection);
}
๋ด๊ฐ ์ํ๋ ๊ฒ์ ์์คํ ๋ก์ง์ actionOnSwipeDetected ์ก์ ์ ํ ๋นํ์ฌ ๋ถ๋ฆฌํ๊ณ , ์ธํ ๊ด๋ จ ์ฝ์ด ๋ก์ง(swiped = true;) ์ ์ธํ ์ฒ๋ฆฌ ํด๋์ค ์์ ์ง์ด๋ฃ์ ์ ์๋๊ฑฐ์ง.
์ก์ ์ ๋ณ์๋ ๋น์ทํ๊ฒ ๊ด๋ฆฌํ ์ ์์ด. ๋ค์๊ณผ ๊ฐ์ด public์ผ๋ก ์ธ๋ถ ์ปดํฌ๋ํธ๊ฐ ์ก์ ์ ์ค์ ํ ์ ์๋๋ก ํ๋ฉด :ย
public void setOnSwipeDetected(Action<Vector2> onSwipeDetected)
{
actionOnSwipeDetected = onSwipeDetected;
}
์ธํ ํด๋์ค์ ๊ฒ์ ์์คํ ํด๋์ค๋ฅผ ์์ ํ ๋ถ๋ฆฌํด์ ๊น๋ํ๊ฒ ๊ตฌํํ ์ ์๊ฒ ๋์์ง?
์๋ฅผ ๋ค์ด ABC๋ผ๋ ํด๋์ค์์ myOnSwipeDetected() ํจ์๋ฅผ ๊ตฌํํ๋ค๊ณ ํด ๋ณด์. ๋ค์๊ณผ ๊ฐ์ด ์ธํ ํด๋์ค์ ํ ๋นํ๋ฉด ๋ผ.
public class ABC : MonoBehaviour
{
GarlicInputManager inputManager;
// Use this for initialization
void Awake ()ย
{
inputManager = GetComponent<GarlicInputManager>();
inputManager.setOnSwipeDetected(myOnSwipeDetected);
}
void myOnSwipeDetected(Vector2 swipeDirection)
{
Debug.DrawLine(transform.position, transform.position + swipeDirection, Color.red, 2f);
}
์ด์ ์ค์์ดํ๋ฅผ ํ ๋๋ง๋ค ABC ํด๋์ค ์์ myOnSwipeDetected๊ฐ ํธ์ถ๋์ด ์ค์์ดํ ํ ๋ฐฉํฅ์ผ๋ก ๋๋ฒ๊ทธ ๋ผ์ธ์ด ๊ทธ๋ ค์ง ๊ฑฐ์ผ.
์ฒ์์๋ ๋ค์ ์ดํดํ๊ธฐ ์ด๋ ต๊ฒ ์ง๋ง, ์ด ๋ถ๋ถ์ ์ดํดํ๊ณ ๋๋ฉด ํจ์ฌ ๊น๋ํ ์ฝ๋๋ฅผ ์งค ์ ์๊ฒ ๋๋ ์ฒซ ๋ฐ์ ๋ด๋๋ ๊ฑฐ๋๊น ์ด์ฌํ ๋ ธ๋ ฅํ์!
๋ง๋ฌด๋ฆฌ๋ก, ์ ์ฒด ์ต์ข ์ฝ๋ ์ฒจ๋ถ :ย
using UnityEngine;
using System.Collections;
using System;
public class GarlicInputManager : MonoBehaviour
{
Vector2 screenSize;
float minSwipeDist;
Vector2 swipeDirection;
Action<Vector2> actionOnSwipeDetected;
void Awake()
{
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
minSwipeDist = Mathf.Max(screenSize.x, screenSize.y) / 14f;
Debug.Log("MinSwipeDist : " + minSwipeDist);
}
void Update()
{
#if UNITY_ANDROID
processMobileInput();
#else
processInput();
#endif
}
Vector2 touchDownPos;
bool swiped = false;
void processMobileInput()
{
if (Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
//save began touch 2d point
touchDownPos = new Vector2(t.position.x, t.position.y);
swiped = false;
}
else if (t.phase == TouchPhase.Moved)
{
Vector2 currentTouchPos = new Vector2(t.position.x, t.position.y);
bool swipeDetected = checkSwipe(touchDownPos, currentTouchPos);
swipeDirection = (currentTouchPos - touchDownPos).normalized;
if (swipeDetected)
onSwipeDetected(swipeDirection);
}
else if (t.phase == TouchPhase.Ended)
{
Debug.Log("Swipe End!");
swiped = true;
}
}
}
Vector3 mouseDownPos;
void processInput()
{
if (Input.GetMouseButtonDown(0) == true)
{
mouseDownPos = Input.mousePosition;
swiped = false;
}
else if (Input.GetMouseButton(0) == true)
{
bool swipeDetected = checkSwipe(mouseDownPos, Input.mousePosition);
swipeDirection = (Input.mousePosition - mouseDownPos).normalized;
if (swipeDetected)
onSwipeDetected(swipeDirection);
}
else if (Input.GetMouseButtonUp(0) == true)
{
Debug.Log("Swipe End!");
swiped = true;
}
}
bool checkSwipe(Vector3 downPos, Vector3 currentPos)
{
//์๊ฐ๋ฝ์ ์์ง ์ ๋์ง๋ง ์ธํ์ ์ ๋ ฅ๋ฐ์๋ค.
if (swiped == true)
return false;
//๋๋ ์ด ๋ฑ์ผ๋ก ์ธํด ์ธํ์ ๋ง๊ณ ์๋ค.
if (isInputBlocked == true)
return false;
Vector2 currentSwipe = currentPos - downPos;
if (currentSwipe.magnitude >= minSwipeDist)
{
//Debug.Log("Swipe Detected : " + currentSwipe);
return true;
}
return false;
}
//public float speed = 5f;
//Vector3 vel;
public void setOnSwipeDetected(Action<Vector2> onSwipeDetected)
{
actionOnSwipeDetected = onSwipeDetected;
}
void onSwipeDetected(Vector2 swipeDirection)
{
swiped = true;
actionOnSwipeDetected(swipeDirection);
}
private bool isInputBlocked = false;
public void blockInput()
{
isInputBlocked = true;
}
public void unBlockInput()
{
isInputBlocked = false;
}
}
์ ์ฝ๋๋ฅผ ์ด์ฉํ ๊ฒ์ ํ ์คํธ :ย
๋จผ์ง๋ชฐ๋ผ๋ ์ง์ง๊ธธ๋ค - dc App
์ฝ๋๋ฅผ ์ธ๋ฐ์์ด ๋ง์ด ์ฒจ๋ถํด์ ๊ทธ๋...์ค๋ ์ง์ค์ด ์ ์ ๋์ ํ๋ฒ ์จ ๋ดค๋๋ฐ ๊ธฐ์ ๋ฌธ์ ๋ณ๋ก๋ค ์ ์ข์ํ๋ฉด ๊ทธ๋ฅ ๋ํธ ์ฐ์๊ฑฐ๋ ๊ณ์ ์ฌ๋ฆฌ๋ ค๊ตฌ...
์ด๋ฐ ๊ฑฐ ์์ฃผ ์ข์๋๋ค
์ด๋ฐ๊ฑฐ ๋๋ฌด ์ข์
ใ ใ ํ๋ฒ์ฉ ์ฌ์ ๋๋ฉด ๊ธฐ์ ๋ฌธ์๋ ์ฌ๋ ค๋ณผ๊ฒ