블로그 이미지
Every unexpected event is a path to learning for you. blueasa

카테고리

분류 전체보기 (2853)
Unity3D (895)
Programming (479)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (189)
협업 (64)
3DS Max (3)
Game (12)
Utility (142)
Etc (99)
Link (34)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (16)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (19)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

[파일]

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
}

 

[출처] 지인

반응형
Posted by blueasa
, |