Utilities - AtaTrkgl/Unity-DartCore Wiki

1.1. MathUtilities.Average()

This function returns the arithmetic mean of a given int[] ,float[], List<int>, List<float> as a float.

Example usage with an array:

int[] nums = new int[]{1,6,9,12,7};
float average = MathUtilities.Average(nums);
Debug.Log(nums); // prints out 7

Example usage with a list:

List<float> nums = new List<float>(){1f, 6f, 9f, 12f, 7f};
float average = MathUtilities.Average(nums);
Debug.Log(nums); // prints out 7

1.2. MathUtilities.UnitCirclePositionRadians()

This function takes two arguments, first one is the angle in radians and the second one is the radius with the default value of 1. Returns the position as a Vector2.

Example Usage:

Vector2 position = MathUtilities.UnitCirclePositionRadians(Mathf.PI/3, 1f);
Debug.Log(position); // prints out (0.86602540378, 0.5)

1.3. MathUtilities.UnitCirclePositionDegrees()

This function functions same as the MathUtilities.UnitCirclePositionRadians(), however, it uses degrees instead of radians

Example Usage:

Vector2 position = MathUtilities.UnitCirclePositionRadians(60f, 1f);
Debug.Log(position); // prints out (0.86602540378, 0.5)

1.4. MathUtilities.RandomChance()

This function takes two arguments, a numerator and a denominator. It will return true with a chance of numerator/denominator. The denominators default value is 100.

Example Usage 1:

if (MathUtilities.RandomChance(30f))
    Debug.Log("You'll get this output 30% of the time");
else
    Debug.Log("You'll get this output 70% of the time");

Example Usage 2:

if (MathUtilities.RandomChance(1f, 3f))
    Debug.Log("You'll get this output one thirds of the time");
else
    Debug.Log("You'll get this output two thirds of the time");

1.5. MathUtilities.RandomVector2()

This function will return a random normalized Vector2.

Example Usage:

Vector2 randVec2 = MathUtilities.RandomVector2();

1.6. MathUtilities.RandomVector3()

This function will return a random normalized Vector3.

Example Usage:

Vector3 randVec3 = MathUtilities.RandomVector3();

1.7. MathUtilities.RandomVector4()

This function will return a random normalized Vector4.

Example Usage:

Vector4 randVec4 = MathUtilities.RandomVector4();

1.8. MathUtilities.IsInRange()

This function will check if the given number is inside the provided bounds. It takes three arguments, the first one being the number and the other being the bounds. Order of the bounds does not matter, if they are equal to each other function will check if the number is equal to the bounds.

Example Usage:

if(MathUtilities.IsInRange(5,0,7))
    Debug.Log("5 is inside the given range");
else
    Debug.Log("5 is not inside the given range");
// Out put is "5 is inside the given range"

1.9. MathUtilities.SumList

This function will return the sum of the elements of the given list. It has two overrides, it can take in both a List<int> & List<float>. Will return a float

Example Usage:

List<int> killsPerSession = new List<int>(){3, 5, 15, 1, 0, 5};
float totalKills = MathUtilities.SumList(killsPerSession);
Debug.Log("Total Kills: " + totalKills.ToString()); 

1.10. MathUtilities.SumArray

Same as the 1.9 but takes an array, has two overrides which are float[] & int[].

Example Usage:

int[] killsPerSession = new int[]{3, 5, 15, 1, 0, 5};
float totalKills = MathUtilities.SumArray(killsPerSession);
Debug.Log("Total Kills: " + totalKills.ToString()); 

1.11. MathUtilities.AbsVec

Uses Mathf.Abs on every axis of the given Vector. Can be used with Vector2, Vector3 and Vector4.

Example Usage:

var vec = new Vector3(-3, 7, 0);
Debug.Log(MathUtilities.AbsVec(vec));

// Output: (3.0, 7.0, 0.0)

1.12. MathUtilities.Direction

Returns the direction, as a normalized Vector3.

var start = new Vector3(0, 0, 0);
var target = new Vector3(3, 4, 0);

Debug.Log(MathUtilities.Direction(start, target));

// Output: (0.6, 0.8, 0.0)

2.1. InputUtilities.IsUsingController

This function returns true if there is atleast on controller connected.

Example Usage:

if(InputUtilities.IsUsingController())
    Debug.Log("Atleast 1 controller is connected!");

2.2. InputUtilities.GetControllers

This function returns a Dictionary<ControllerType, int> where each key is a controller type and the values are how many of the coresponding controller is connected.

Example Usage:

Debug.Log($"there are {InputUtilities.GetControllers()[ControllerType.Dualshock4]} ps4 controlleres connected.");

2.3. InputUtilities.GetMainController

This function will return a ControllerType object, which is the main controller.

Example Usage:

ControllerType currentController = InputUtilities.GetMainController();
Debug.Log("Current Controller is: " + currentController.ToString());

2.4. InputUtilities.ControllerType

This enum contains all the controllers this class will use.

Example Usage:

ControllerType currentController = InputUtilities.GetMainController();
Debug.Log("Current Controller is: " + currentController.ToString());

2.5. InputUtilities.ControllerKey

This enum contains all the controller keys in a unified fashion. Like ControllerKey.TopButton which is Y on a XBox Controller and Triangle on a Dualshock.

3.1. EditorScriptingUtils.HorizontalLine

This method will draw a horizontal line to the inspector, it takes two arguments, first one being the height with the default value of 1f and the second one being the color with the default value of Color(.7f, .7f, .7f, 1f).

3.2. EditorScriptingUtils.BeginCenter & EditorScriptingUtils.EndCenter

These two methods need each other to work. any GUILayout inbetween will be centered horizontally.

Example Usage:

EditorScriptingUtils.BeginCenter();
GUILayout.Label("This label is centered horizontally");
EditorScriptingUtils.EndCenter();

4.1. SaveUtilities.SaveValue

This method allows you to save any serializable value using the BinaryFormater class. It takes the file name as a string and the value as a generic type.

Example Usage 1:

int highScore = 136;
SaveUtilities.SaveValue<int>("high_score", highScore);

Even though you can use this function for basic values, what makes it useful is how good it can store bigger data like a struct.

Example Usage 2:

[System.Serializable]
struct PlayerData
{
   public float playerHealth;
   public string playerName;
}

private void Start()
{
   PlayerData playerData = new PlayerData
   {
     playerHealth = 70f,
     playerName = "Player 1"
   };

   SaveUtilities.SaveValue<PlayerData>("player_data", playerData);
}

4.2. SaveUtilities.ReadValue

This method allows you to access a value saved via the SaveValue() method. It returns a generic type object and takes the fileName as a string and the default value as a generic.

Example Usage 1:

// These values were saved via the SaveValue() method
int highScore = SaveUtilities.GetValue<int>("high_score", 10);

PlayerData playerData = SaveUtilities.GetValue<PlayerData>("high_score", new PlayerData());

4.3. SaveUtilities.ClearValue

This method is used the delete a save file. You only need to provide the file name.

SaveUtilities.ClearValue("high_score");