[파일]
LunarConsoleAssist.cs
0.00MB
using UnityEngine;
#if UNITY_ANDROID || UNITY_IOS
using LunarConsolePlugin;
#endif
/// <summary>
/// 모바일 기기에서 멀티터치를 통해 LunarConsole을 활성화하는 도우미 클래스
/// </summary>
public class LunarConsoleAssist : MonoBehaviour
{
[Header("Touch Settings")]
[SerializeField, Range(2, 5)]
private int requiredFingers = 3;
[SerializeField, Range(0.1f, 2.0f)]
private float requiredHoldTime = 0.3f;
[Header("Debug")]
[SerializeField]
private bool enableDebugLog = false;
private float currentHoldTimer;
private bool isConsoleShown;
// 성능 최적화를 위한 캐싱
private Touch[] cachedTouches;
private void Awake()
{
// 필요한 경우에만 초기화
#if !(UNITY_ANDROID || UNITY_IOS) || UNITY_EDITOR
if (enableDebugLog)
Debug.LogWarning("[LunarConsoleAssist] This component only works on Android/iOS devices.");
#endif
}
private void Update()
{
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
HandleTouchInput();
#endif
}
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
private void HandleTouchInput()
{
int touchCount = Input.touchCount;
// 필요한 손가락 수가 맞는지 확인
if (requiredFingers <= touchCount)
{
if (AreRequiredTouchesValid(touchCount))
{
currentHoldTimer += Time.unscaledDeltaTime;
if (requiredHoldTime <= currentHoldTimer && !isConsoleShown)
{
ShowLunarConsole();
}
}
else
{
ResetTimer();
}
}
else
{
ResetTimer();
}
}
private bool AreRequiredTouchesValid(int touchCount)
{
// 성능 최적화: 필요한 터치만 확인
int validTouchCount = 0;
for (int i = 0; i < touchCount && validTouchCount < requiredFingers; i++)
{
TouchPhase phase = Input.touches[i].phase;
if (IsTouchPhaseValid(phase))
{
validTouchCount++;
}
}
return requiredFingers <= validTouchCount;
}
private bool IsTouchPhaseValid(TouchPhase phase)
{
return phase == TouchPhase.Began ||
phase == TouchPhase.Stationary ||
phase == TouchPhase.Moved;
}
private void ShowLunarConsole()
{
try
{
LunarConsole.Show();
isConsoleShown = true;
if (enableDebugLog)
Debug.Log("[LunarConsoleAssist] Console activated!");
ResetTimer();
}
catch (System.Exception e)
{
if (enableDebugLog)
Debug.LogError($"[LunarConsoleAssist] Failed to show console: {e.Message}");
}
}
private void ResetTimer()
{
currentHoldTimer = 0f;
isConsoleShown = false;
}
#endif
#if UNITY_EDITOR
private void OnValidate()
{
// 에디터에서 값 검증
requiredFingers = Mathf.Clamp(requiredFingers, 2, 5);
requiredHoldTime = Mathf.Clamp(requiredHoldTime, 0.1f, 2.0f);
}
#endif
}
[출처] 지인