ãUnityãä½ãã¬ã¼ã ã¬ã¼ãã§ããããããªå ¥åãåãåããã
InputManagerã§ã¯å
¥åæ
å ±ã¯ãã¬ã¼ã ã¬ã¼ãã«å¼·ãä¾åãã¦ããããã®ä¸éã§åå¾ããæ
å ±ã®å¤§æµã¯ç ´æ£ããã¦ãã¾ããã
InputSystemã§ã¯ä¸éã®æ
å ±ããããã¡ã¨ãã¦ç¢ºä¿ã»ä½¿ç¨ã§ããããã«ãªã£ãã®ã§ãããã使ç¨ãã¦ãã¬ã¼ã ãä½ãã¦ããã¬ã¤ãªç·ãå¼ããæ¹æ³ãèãã¦ã¿ã¾ãã
ãªãåä½ã¯OSã®åä½ã«å¼·ãä¾åãã¾ãã
åä½ç°å¢ï¼Unity 2018.3 b12
ã Input System 1.0 preview
å®éã®åä½
ã¾ãæ®éã«ä½ãã¬ã¼ã ã¬ã¼ãç°å¢ä¸ã§InputManagerã使ç¨ãã¦ãã¦ã¹ã®ä½ç½®ã追跡ãã¦ç·ãå¼ããããªã³ã¼ããå®è£
ããå ´åãä¸ã®ãããªå½¢ã«ãªãã¾ãã
åä½ã§ã¯ããã¦ã¹ã¯æ»ããã«åãæãå½¢ã§åãã¦ãã¾ãããç·ã¯é常ã«è§å¼µã£ãå½¢ã§æç»ããã¦ãã¾ããããã¯ãã¦ã¹ã®åº§æ¨ãåå¾ããééãåºãããã«èµ·ããã¾ãã
InputSystemã使ç¨ããå ´åã¯ã³ãã©ããã¡ãããã¬ã¼ã ã¬ã¼ããè½ã¨ãã¦ãã¾ããå ¥åæ å ±ã¯ãã¬ã¤ã«è£å®ããã¦ãããã¡ããã¨æ²ç·ãæãã¦ããäºã確èªåºæ¥ã¾ããï¼ãã¬ã¼ã ã«è¤æ°åã®å ¥åãåãåããã¨ããèªèãä¸çªèªèããããæ¦å¿µã§ãã
ã³ã¼ãå ¨æ
InputManagerã®å ´å
ã¾ãæåã«ãInputManagerã使ç¨ããã³ã¼ãã§ããæ®éã«ä½ç½®æ å ±ãåå¾ããæç»ãä¾é ¼ããã³ã³ãã¼ãã³ãã«æ å ±ã渡ãã¾ããç´°ããã³ã¼ãã¯ä¸ã®å ¨æãè¦ã¦ãã ããã
// GetInput3.cs void Update() { var pos = Input.mousePosition; line.AddPosition(pos); // 座æ¨ãæ³¨å ¥ }
InputSystemã§ãã¦ã¹ã®ä½ç½®ãåå¾ãã
InputSystemã§mousePositionãåå¾ããã³ã¼ãã§ããããã¯ã¾ã InputManagerã¨åããããªåãããã¾ãã
void Update() { var pos = Mouse.current.position.ReadValue(); // ãã¦ã¹ã®ä½ç½®ãåå¾ line.AddPosition(pos); }
InputSystemã§ãããã¡ã使ç¨ãã¦åå¾ãã
ãããã¡ã使ç¨ãã¦åå¾ãã¾ããããã§ã¯ InputStateHistory
ã使ç¨ãã¾ãã
ãã®ã¢ããã¼ãã§ã¯ä¸éãããã¡ãè£å®ãã¦ããã®ã§ãæ»ãããªç·ãå¼ãã¾ãã
ãªããããã¡ã¯newããå°ç¹ã§ç¢ºä¿ãããã®ã§ãèªåã§éæ¾ãè¡ããªãã¨ã¡ã¢ãªãªã¼ã¯ãèµ·ããã¾ãã
InputStateHistory history; void Awake() { history = new InputStateHistory<Vector2>(Mouse.current.position); //ãã¦ã¹ã®ä½ç½®ã観測 } void OnDestroy() { history.Dispose(); // ç¡å¹ãããã¯ç ´æ£ã®ã¿ã¤ãã³ã°ã§å¿ ãéæ¾ } void OnEnable() => history.StartRecording(); // ãããã¡ãã¬ã³ã¼ãéå§ void OnDisable() => history.StopRecording(); // ãããã¡ãã¬ã³ã¼ãçµäº void Update() { foreach( var record in history) { var pos = record.ReadValue<Vector2>(); line.AddPosition(pos); } history.Clear(); // ä»åã®åã®ãããã¡ã¯éæ¾ }
InputActionã使ç¨ããï¼ãã¼ãªã³ã°ï¼
InputActionã®ãã¼ãªã³ã°ã使ç¨ãã¦ã¿ã¾ããã¤ã¾ã InputActionTrace
ã使ç¨ãã¾ãã
äºåæºåã¨ãã¦InputActionã«Position
ããã®Controlã«ã¯MousePositionãå
¥ãã¾ããããã¯Touchãå§ãã¨ããä»ã®Controlã§ãè¯ãã§ãã
ãããPlayerInputã«è¨å®ãã»ããã¢ãããã¦ããããGetComponent<PlayerInput>().currentActionMap["Position"];
ã§InputActionãåå¾ã»ä½¿ç¨ãã¾ãã
private InputAction inputAction; private InputActionTrace trace; void Awake() { inputAction = GetComponent<PlayerInput>().currentActionMap["Position"]; trace = new InputActionTrace(); } void OnDestroy() { trace.Dispose(); } void OnEnable() => trace.SubscribeTo(inputAction); // 観測éå§ void OnDisable() => trace.UnsubscribeFrom(inputAction); // 観測çµäº void Update() { foreach (var record in trace) // 観測å 容ããä¸ã¤ãã¤åãåºãã¦å¦ç { var v = record.ReadValue<Vector2>(); line.AddPosition(v); } trace.Clear(); }
ã¤ãã³ãé§å
ä¸ã®ã¤ãã³ãé§åããå ´åã§ããUpdateãæ¸ããªãã¦ãè¯ãã¨ããåé¢ãï¼ãã¬ã¼ã ã«åãå¦çãè¤æ°åå¼ã°ããæã®å¯¾çãå°ãé¢åã¨ããã°é¢åããããã¾ããã
private DrawLine line; private InputAction inputAction; void Awake() { inputAction = GetComponent<PlayerInput>().currentActionMap["Position"]; } void OnEnable() => inputAction.performed += Input_performed; void OnDisable() => inputAction.performed -= Input_performed; void Input_performed(InputAction.CallbackContext c) { var v = c.ReadValue<Vector2>(); line.AddPosition(v); }
ãã¼ãªã³ã°ã®é »åº¦
å
¥åã¤ãã³ãããã¼ãªã³ã°ããã¿ã¤ãã®ããã¤ã¹ã®å ´åããã¼ãªã³ã°ã®é »åº¦ãInputSystem.pollingFrequency
ã§è¨å®ã§ãã¾ããåæå¤ã¯60Hzï¼1ç§éã«60åãã§ãã¯ï¼ã§ããããä¸ãããä¸ããããããã¨ã§å
¥åã¤ãã³ãã®ãµã³ããªã³ã°ç²¾åº¦ãä¸ãããåºæ¥ã¾ãã
ãã ä¸ããããã¨ï¼1000ã¨ãï¼ã¨ãã£ã¿ã¼ãã¨è½ã¡ãäºããã£ãã®ã§æ³¨æãå¿ è¦ããããã¾ãããã¾ãå¹¾ã¤ãã®ããã¤ã¹ã¯ãã¼ãªã³ã°ããå½¢ã§ã¯ãªãOSããã®å ¥åãåãåãã¿ã¤ããªã®ã§ããã®APIã¯åä½ãã¾ããã