Sunday, October 19, 2014

Input Event resolve Update input handling overcrowd in Unity

I think writing whole input solving logic in Update (even other logic) its not good solution when code become long and Update got overcrowded.
  // Update is called once per frame
        void Update()
        {
		


          

            //Input.GetInput allows combos (combined input actions)
            if (InputManager.GetInputDown((int)States.Wave))// || InputManager.GetInput((int)States.Wave,true))
            // if (InputManager.GetInput((int)States.Wave,false))
            {
                Debug.Log("Wave Down");
               // animator.Play((int)States.Wave);
				animator.Play(Animator.StringToHash("Wave"));
            }


            if (InputManager.GetInputUp((int)States.MyCustomState))
            {
                Debug.Log(States.MyCustomState + "-Up");
                // animator.Play((int)States.Wave);
            }


....
Event based system, Signals or Reactions are much more better solution. Define InputEvent for the "state". Attach Handlers that would be triggered if InputCombination binded to that state is achieved
 InputEvent ev = new InputEvent("ManualAddedSTATE");
            //InputEvent ev = new InputEvent((int)States.SomeState);

            ev.INPUT += new EventHandler(Handle1);
            ev.INPUT += new EventHandler(Handle2);
            ev.UP += new EventHandler(onUp);//this wouldn't fire for combo inputs(single only)
            ev.DOWN += new EventHandler(onDown);//this wouldn't fire for combo inputs(single only)

InputEvent
    
public class InputEvent
    {



        protected int _stateNameHash;
        protected static GameObject _container;
        //public static delegate bool GetInputDelegate(int stateNameHash,bool atOnce);
		protected static Func _action;

        protected enum EventType:uint
        {
            CONT=0,
            UP,
            DOWN
        }


        public InputEvent(int stateNameHash)
        {
            _stateNameHash = stateNameHash;

           
        }

...
Create Behaviour Checker Dispatcher
 void Update()
            {
                Delegate[] delegates;

			    if(Events!=null)
                foreach (KeyValuePair pair in Events)
                {
                    
                    if(pair.Value[0]!=null && InputManager.GetInput(pair.Key,false)){
                        delegates= pair.Value[0].GetInvocationList();
                        foreach(Delegate d in delegates)
                            ((EventHandler)d).BeginInvoke(this, args, EndAsyncEvent, null);
                    }

                    if (pair.Value[1] != null && InputManager.GetInputUp(pair.Key))
                    {
                        delegates = pair.Value[1].GetInvocationList();
                        foreach (Delegate d in delegates)
                            ((EventHandler)d).BeginInvoke(this, args, EndAsyncEvent, null);
                    }

                    if (pair.Value[2] != null && InputManager.GetInputDown(pair.Key))
                    {
                        delegates = pair.Value[2].GetInvocationList();
                        foreach (Delegate d in delegates)
                            ((EventHandler)d).BeginInvoke(this, args, EndAsyncEvent, null);
                    }

                  

                }
            }

No comments:

Post a Comment