- Home /
Execution order vs script insurance
I've got a custom input manager. I already know that the optimal solution would be to configure it in the execution order. But I'm asking whether the overhead penalty is huge enough to be considered problematic.
Alternatively, is there a way to "auto-add" a script to the begining of the execution order via the script iteself?
Ideally I want this to be a drag and drop script and work 100% script.
Here is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// DO NOT USE INPUTS IN LATEUPDATE WITH THIS INPUT MANAGER
/// Only handles buttons - use default Input Manager for mouse movement and joystick movement
/// </summary>
public class MyInputManager : MonoBehaviour
{
#region SingletonMethod
private static MyInputManager _instance;
//This is the public reference that other classes will use
public static MyInputManager instance
{
get
{
//If _instance hasn't been set yet, we grab it from the scene!
//This will only happen the first time this reference is used.
if(_instance == null)
_instance = GameObject.FindObjectOfType<MyInputManager>();
return _instance;
}
}
#endregion
public AxisElement[] Axes;
private Dictionary<string, float> Axis = new Dictionary<string, float>();
private Dictionary<string, float> AxisPrevValue = new Dictionary<string, float>();
private bool ranUpdate;
private void Start()
{
foreach( AxisElement ae in Axes )
{
Axis.Add(ae.name, ae.axisValue );
}
}
private void Update()
{
if(!ranUpdate)
{
foreach( AxisElement ae in Axes )
{
AxisPrevValue[ae.name] = Axis[ae.name];
Axis[ae.name] = 0f;
foreach(KeyCode kc in ae.PositiveKC)
if( Input.GetKey( kc ) )
{
Axis[ae.name] = 1f;
break;
}
foreach(KeyCode kc in ae.NegativeKC)
if( Input.GetKey( kc ) )
{
Axis[ae.name] = -1f;
break;
}
}
ranUpdate = true;
}
}
private void LateUpdate()
{
ranUpdate = false;
}
public float GetAxis(string identifier)
{
Update();
if( Input.GetAxis( identifier ) != 0f )
return Input.GetAxis( identifier );
else
return Axis[identifier];
}
public float GetAxisNoUniMan(string identifier)
{
Update();
return Axis[identifier];
}
public bool GetButton(string identifier)
{
Update();
return Axis[identifier] != 0 || Input.GetButton( identifier );
}
public bool GetButtonDown(string identifier)
{
Update();
return ( Axis[identifier] != 0 && AxisPrevValue[identifier] == 0 ) || Input.GetButtonDown( identifier );
}
public bool GetButtonUp(string identifier)
{
Update();
return ( Axis[identifier] == 0 && AxisPrevValue[identifier] != 0 ) || Input.GetButtonDown( identifier );
}
}
The script works 100%, does not require any adjustment and inputs can be changed on the fly.
Answer by Landern · Mar 02, 2015 at 02:51 PM
You can script the execution order, an answer by FlyingOstriche on the forums solves with an with added attribute. You probably won't get bend out of shape by the small "hit" you take by customizing the order of execution.
Thanks for the quick response. Exactly what I wanted, I can use this for a lot more as well.
Your answer
Follow this Question
Related Questions
Can I force unity to "weld" or "attach" a vertex of common faces, just like 3ds max? 1 Answer
what is more important for performance in Unity? UV count or the Vertice count 1 Answer
Code Performance: When to use "new" on Vector3 3 Answers
Reading Profiler Results 1 Answer
Help with Optimizing Voxel Code? 1 Answer