Code Tuning

With Code Tuning, branch your code and tune any variable for live debugging in Unity editor!

What does it do?

Code Tuning is a singleton class that provides a number of parameters of various types that you can access from any script. Those parameters can be tuned at any time from a dedicated editor window. This means you can temporary turn hard-coded values into tunable parameters for debug purposes. You can also test different blocks of code by tuning the parameter of an if of switch condition.

How do I use it?

  • Download CodeTuning.cs and CodeTuningEditor.cs and import them in your Unity project (make sure CodeTuningEditor.cs is in an Editor folder).
  • Open the Code Tuning editor window with Debug > Code Tuning.
  • Check Activate to start using the values in the window.
  • Insert code where you want to sue tunable values, following the API:
	// singleton method call
	CodeTuning.Instance.Get...()

	// API
	int GetBranchIndex(int defaultValue)
	bool GetBoolX(bool defaultValue), X = 1..2
	float GetFloatX(float defaultValue), X = 1..2
  • Start playing the game in the editor.
  • Tune the boolean, branching index or float values in the editor window to see the result live.
  • When you have found satisfying values, replace the CodeTuning values with the new values in the code (values are stored in the Editor preferences may some may be lost on Play/Stop and recompiling).
  • If you think the new value is fine but want to continue tuning, replace the default value argument in the Get…  methods with the new value, and repeat the process.

The passed value in the Get… methods is the default value, used when Activate is unchecked and when building outside the editor. To make sure your game always use meaningful values when CodeTuning is inactive and in actual builds where CodeTuning is still referenced, make sure the default values are meaningful. However, I recommend you always strip out any reference to CodeTuning once you have finished debugging, before building.

Parameters

Active Should we use CodeTuning values? If false, default values (passed in the Get… methods) are used instead.
Branch Index An integer to use for tunable branching (can also be used as a simple integer).
Bool 1 Tunable bool value returned by GetBool1.
Bool 2 Tunable bool value returned by GetBool2.
Float 1 Tunable float value returned by GetFloat1.
Float 2 Tunable float value returned by GetFloat2.
 Repaint Scene on Change Should the editor Scene View be repainted when a tunable value is changed? Check it if you are tuning something in an editor drawing method such as Editor.OnSceneGUI or MonoBehaviour.OnDrawGizmos

Example

	// Case 1: you want to test a new method only when you check some toggle
	// -> use an optional branch

	NewExperimentalMethod();

	// becomes...

	if (CodeTuning.Instance.GetBool1(false)) {
		// only executed when CodeTuning is active and Bool 1 toggle is checked
		NewExperimentalMethod();
	}

	// Case 2: you want to test 3 different lines or blocks of code without commenting them
	// in and out and recompiling every time
	// -> use tunable branching

	Color c = Color.red;
	// Color c = Color.blue;
	// Color c = Color.yellow;

	// becomes...

	Color c;
	switch (CodeTuning.Instance.GetBranchIndex(0)) {
	case 0:
		c = Color.red;
		break;
	case 1:
		c = Color.blue
		break;
	default:
		c = Color.yellow;
		break;
	}

	// Case 3: you want to tune a hard-coded value, but you don't want to promote it to
	// a MonoBehaviour serialized attribute right now
	// -> use a tunable float
	
	float motionDelta = 2.5f * Time.deltaTime;

	// becomes...

	float motionDelta = CodeTuning.Instance.GetFloat1(speed) * Time.deltaTime;
}

Updates

You can also find the two scripts on my Bitbucket repository Unity Commons – Debug. I use it for all my personal projects so it gets updated regularly (although CodeTuning is not changed frequently). You can check it out in case I haven’t uploaded the last version for the two links to the scripts above.

Limitations

  • CodeTuning won’t affect variables initialized on game start or scene load. More exactly, it will only affect such variables if you activated CodeTuning before the variable was set, and even so, they won’t change later even if the CodeTuning parameters are changed. CodeTuning works the best when the Get methods are used in loops.
  • There is a limited number of parameters of each type, although you can easily extend the class with more parameters by copying and adapting existing code. Currently, the code is not flexible enough to allow an indefinite number of parameters.

Comments are closed