当项目需求中,后期可能接入多种输入设备的时候,可以借鉴一下以下代码
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using System; 5 6 public abstract class TInputBase : MonoBehaviour{ 7 8 public event InputSchemeEventHandler InputSchemeEvent; 9 10 public abstract InputSchemeEventArgs HandleInput(); 11 12 public delegate void InputSchemeEventHandler(TInputBase sender, TInputBase.InputSchemeEventArgs inputArgs); 13 14 //Packaging Input data 15 protected InputSchemeEventArgs GetInputData(GameObject currentObject,EnumList.InputResultTypes inputStyle) 16 { 17 return new InputSchemeEventArgs 18 { 19 CurrentGazeObject = currentObject, 20 21 Style = inputStyle, 22 }; 23 } 24 25 26 //Notify delegates during the update method 27 protected void NotifyDelegates(InputSchemeEventArgs data) 28 { 29 if (InputSchemeEvent != null) 30 { 31 this.InputSchemeEvent(this, data); 32 } 33 } 34 35 36 void Update() 37 { 38 if (this.HandleInput() != null) 39 { 40 this.NotifyDelegates(this.HandleInput()); 41 } 42 } 43 44 //Args (input args) 45 public class InputSchemeEventArgs : EventArgs 46 { 47 public GameObject CurrentGazeObject; 48 49 public EnumList.InputResultTypes Style; 50 } 51 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using System; 5 6 public abstract class TInputBase : MonoBehaviour{ 7 8 public event InputSchemeEventHandler InputSchemeEvent; 9 10 public abstract InputSchemeEventArgs HandleInput(); 11 12 public delegate void InputSchemeEventHandler(TInputBase sender, TInputBase.InputSchemeEventArgs inputArgs); 13 14 //Packaging Input data 15 protected InputSchemeEventArgs GetInputData(GameObject currentObject,EnumList.InputResultTypes inputStyle) 16 { 17 return new InputSchemeEventArgs 18 { 19 CurrentGazeObject = currentObject, 20 21 Style = inputStyle, 22 }; 23 } 24 25 26 //Notify delegates during the update method 27 protected void NotifyDelegates(InputSchemeEventArgs data) 28 { 29 if (InputSchemeEvent != null) 30 { 31 this.InputSchemeEvent(this, data); 32 } 33 } 34 35 36 void Update() 37 { 38 if (this.HandleInput() != null) 39 { 40 this.NotifyDelegates(this.HandleInput()); 41 } 42 } 43 44 //Args (input args) 45 public class InputSchemeEventArgs : EventArgs 46 { 47 public GameObject CurrentGazeObject; 48 49 public EnumList.InputResultTypes Style; 50 } 51 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class TKeyboardInput : TInputBase { 6 7 InputSchemeEventArgs inputNoop; 8 public GameObject rayLaunchPoint; 9 10 public override InputSchemeEventArgs HandleInput () 11 { 12 inputNoop = null; 13 Ray ray = new Ray(rayLaunchPoint.transform.position, rayLaunchPoint.transform.forward); 14 Debug.DrawLine(ray.origin, ray.origin + (ray.direction * 800f), Color.yellow); 15 RaycastHit hit; 16 if (Physics.Raycast(ray,out hit,800f)) { 17 18 if (Input.GetKeyDown(KeyCode.Space)){ 19 inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.ClickDown); 20 } 21 if (Input.GetKey(KeyCode.Space)){ 22 inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.Clicking); 23 } 24 if (Input.GetKeyUp(KeyCode.Space)){ 25 inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.ClickUp); 26 } 27 } 28 return inputNoop; 29 } 30 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public abstract class TClickBase : MonoBehaviour { 6 7 public abstract void HandleClick (TInputBase.InputSchemeEventArgs inputArgs); 8 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class TDaydreamInput : TInputBase { 6 7 public override InputSchemeEventArgs HandleInput () 8 { 9 //Debug.Log ("Daydream 手柄输入"); 10 return null; 11 } 12 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class TestFocusEvents : TClickBase { 6 7 public override void HandleClick (TInputBase.InputSchemeEventArgs inputArgs) 8 { 9 if (inputArgs.Style.Equals(EnumList.InputResultTypes.ClickDown)) { 10 if (inputArgs.CurrentGazeObject.name=="Cube") { 11 Debug.Log ("click down cube"); 12 } 13 } 14 if (inputArgs.Style.Equals(EnumList.InputResultTypes.Clicking)) { 15 if (inputArgs.CurrentGazeObject.name=="Cube") { 16 Debug.Log ("clicking cube"); 17 } 18 } 19 if (inputArgs.Style.Equals(EnumList.InputResultTypes.ClickUp)) { 20 if (inputArgs.CurrentGazeObject.name=="Cube") { 21 Debug.Log ("click up cube"); 22 } 23 } 24 } 25 void Start() 26 { 27 TController.register = this; 28 } 29 }
时间: 2024-10-06 07:49:27