142 lines
5.9 KiB
C#
142 lines
5.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.InputSystem.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class CompleteMouseInputActionsCreator : MonoBehaviour
|
|
{
|
|
[ContextMenu("Create Complete Mouse Input Actions")]
|
|
void CreateCompleteMouseInputActions()
|
|
{
|
|
var asset = ScriptableObject.CreateInstance<InputActionAsset>();
|
|
asset.name = "CompleteMouseUIInputActions";
|
|
|
|
// UI Action Map 생성
|
|
var uiMap = asset.AddActionMap("UI");
|
|
|
|
// 1. Point Action (마우스 위치 추적)
|
|
var pointAction = uiMap.AddAction("Point", InputActionType.PassThrough);
|
|
pointAction.expectedControlType = "Vector2"; // 마우스 위치는 Vector2 타입
|
|
pointAction.AddBinding("<Mouse>/position");
|
|
pointAction.AddBinding("<Pen>/position");
|
|
pointAction.AddBinding("<Touchscreen>/touch*/position");
|
|
|
|
// 2. Left Click Action
|
|
var leftClickAction = uiMap.AddAction("LeftClick", InputActionType.Button);
|
|
leftClickAction.AddBinding("<Mouse>/leftButton");
|
|
leftClickAction.AddBinding("<Pen>/tip");
|
|
leftClickAction.AddBinding("<Touchscreen>/touch*/press");
|
|
|
|
// 3. Right Click Action
|
|
var rightClickAction = uiMap.AddAction("RightClick", InputActionType.Button);
|
|
rightClickAction.AddBinding("<Mouse>/rightButton");
|
|
rightClickAction.AddBinding("<Pen>/barrel");
|
|
|
|
// 4. Middle Click Action
|
|
var middleClickAction = uiMap.AddAction("MiddleClick", InputActionType.Button);
|
|
middleClickAction.AddBinding("<Mouse>/middleButton");
|
|
|
|
// 5. Scroll Wheel Action
|
|
var scrollAction = uiMap.AddAction("ScrollWheel", InputActionType.PassThrough);
|
|
pointAction.expectedControlType = "Vector2";
|
|
scrollAction.AddBinding("<Mouse>/scroll");
|
|
|
|
// 6. Mouse Delta (드래그용)
|
|
var mouseDeltaAction = uiMap.AddAction("MouseDelta", InputActionType.PassThrough);
|
|
mouseDeltaAction.expectedControlType = "Vector2"; // 마우스 이동은 Vector2 타입
|
|
mouseDeltaAction.AddBinding("<Mouse>/delta");
|
|
mouseDeltaAction.AddBinding("<Pen>/delta");
|
|
mouseDeltaAction.AddBinding("<Touchscreen>/touch*/delta");
|
|
|
|
// 7. Drag Action (드래그 감지용)
|
|
var dragAction = uiMap.AddAction("Drag", InputActionType.PassThrough);
|
|
dragAction.expectedControlType = "Vector2";
|
|
// 드래그는 마우스 버튼이 눌린 상태에서의 마우스 이동을 감지
|
|
// Composite Binding을 사용하여 복합 입력 생성
|
|
var dragComposite = dragAction.AddCompositeBinding("Vector2");
|
|
dragComposite.With("x", "<Mouse>/delta/x");
|
|
dragComposite.With("y", "<Mouse>/delta/y");
|
|
|
|
// 8. Submit Action (엔터, 스페이스)
|
|
var submitAction = uiMap.AddAction("Submit", InputActionType.Button);
|
|
submitAction.AddBinding("<Keyboard>/enter");
|
|
submitAction.AddBinding("<Keyboard>/space");
|
|
submitAction.AddBinding("<Gamepad>/buttonSouth");
|
|
|
|
// 9. Cancel Action (ESC)
|
|
var cancelAction = uiMap.AddAction("Cancel", InputActionType.Button);
|
|
cancelAction.AddBinding("<Keyboard>/escape");
|
|
cancelAction.AddBinding("<Gamepad>/buttonEast");
|
|
|
|
// 10. Navigate Action (방향키)
|
|
var navigateAction = uiMap.AddAction("Navigate", InputActionType.PassThrough);
|
|
navigateAction.expectedControlType = "Vector2"; // 방향키 입력은 Vector2 타입
|
|
navigateAction.AddBinding("<Keyboard>/arrowKeys");
|
|
navigateAction.AddBinding("<Gamepad>/leftStick");
|
|
navigateAction.AddBinding("<Gamepad>/dpad");
|
|
|
|
Debug.Log("✓ 완전한 마우스 Input Actions Asset 생성 완료");
|
|
|
|
// Event System에 자동 할당
|
|
AutoAssignToInputModule(asset);
|
|
|
|
// 파일로 저장 (에디터에서만)
|
|
#if UNITY_EDITOR
|
|
SaveAssetToFile(asset);
|
|
#endif
|
|
}
|
|
|
|
void AutoAssignToInputModule(InputActionAsset asset)
|
|
{
|
|
var eventSystem = FindObjectOfType<EventSystem>();
|
|
if (eventSystem == null)
|
|
{
|
|
var go = new GameObject("EventSystem");
|
|
eventSystem = go.AddComponent<EventSystem>();
|
|
}
|
|
|
|
var inputModule = eventSystem.GetComponent<InputSystemUIInputModule>();
|
|
if (inputModule == null)
|
|
{
|
|
// 기존 Input Module 제거
|
|
var oldModule = eventSystem.GetComponent<StandaloneInputModule>();
|
|
if (oldModule != null)
|
|
{
|
|
DestroyImmediate(oldModule);
|
|
}
|
|
|
|
inputModule = eventSystem.gameObject.AddComponent<InputSystemUIInputModule>();
|
|
}
|
|
|
|
// Actions Asset 및 Action References 할당
|
|
inputModule.actionsAsset = asset;
|
|
|
|
var uiMap = asset.FindActionMap("UI");
|
|
if (uiMap != null)
|
|
{
|
|
inputModule.point = InputActionReference.Create(uiMap.FindAction("Point"));
|
|
inputModule.leftClick = InputActionReference.Create(uiMap.FindAction("LeftClick"));
|
|
inputModule.rightClick = InputActionReference.Create(uiMap.FindAction("RightClick"));
|
|
inputModule.middleClick = InputActionReference.Create(uiMap.FindAction("MiddleClick"));
|
|
inputModule.scrollWheel = InputActionReference.Create(uiMap.FindAction("ScrollWheel"));
|
|
inputModule.submit = InputActionReference.Create(uiMap.FindAction("Submit"));
|
|
inputModule.cancel = InputActionReference.Create(uiMap.FindAction("Cancel"));
|
|
inputModule.move = InputActionReference.Create(uiMap.FindAction("Navigate"));
|
|
|
|
Debug.Log("✓ InputSystemUIInputModule에 Actions 할당 완료");
|
|
}
|
|
|
|
// Actions 활성화
|
|
asset.Enable();
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
void SaveAssetToFile(InputActionAsset asset)
|
|
{
|
|
string path = "Assets/CompleteMouseUIInputActions.inputactions";
|
|
UnityEditor.AssetDatabase.CreateAsset(asset, path);
|
|
UnityEditor.AssetDatabase.SaveAssets();
|
|
Debug.Log($"✓ Input Actions Asset 저장: {path}");
|
|
}
|
|
#endif
|
|
} |