This repository contains a utility library for Unity, providing helper functions for working with colors, collections, rotations, and more. The aim is to simplify common tasks and improve code reuse across Unity projects.
- Color Utilities: Generate random or complementary colors, adjust hues, and convert to hex.
- Collection Utilities: Shuffle, rotate, and transform collections (lists, arrays) with various helper methods.
- Rotation Utilities: Handle quaternion rotations and alignments in 3D space.
- Large Data Handling: Serialize large data to
PlayerPrefs
or logs. - Time Utilities: Work with time and date formats.
- Download the
HELPER
script. - Import the
Helper
namespace into your Unity project:using HELPER;
Generate a list of unique random colors with ease:
int colorCount = 5;
List<Color> uniqueColors = COLOR.GenerateUniqueColors(colorCount);
Generate a single random color:
Color randomColor = COLOR.RandomColor();
Modify the hue and value of a given color:
Color adjustedColor = COLOR.AdjustColor(Color.red, 0.1f, -0.2f);
Convert any Color
to a hex string for use in UIs or serialization:
string hexColor = COLOR.ToHex(Color.blue);
// Returns "#0000FF"
Shuffle any list of elements randomly:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
List<int> shuffledNumbers = COLLECTION.ShuffleList(numbers);
Select a unique subset of items from a list:
List<int> selectedItems = COLLECTION.GetUniqueRandomItems(numbers, 3);
Perform various transformations on a 2D array:
int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Flip horizontally
int[,] flippedHorizontal = COLLECTION.FlipHorizontal(matrix);
// Rotate 90 degrees to the right
int[,] rotatedRight = COLLECTION.Rotate90ToRight(matrix);
// Log results
Debug.Log($"Original: {COLLECTION.ToString(matrix)}");
Debug.Log($"Flipped Horizontal: {COLLECTION.ToString(flippedHorizontal)}");
Debug.Log($"Rotated Right: {COLLECTION.ToString(rotatedRight)}");
Snap an object’s rotation to the nearest face of a cube:
Quaternion currentRotation = someTransform.rotation;
Quaternion nearestFaceRotation = ROTATION.GetNearestFaceRotation(currentRotation);
Apply a smooth rotation effect with inertia:
StartCoroutine(ROTATION.ApplyInertia(cubeTransform, new Vector2(1f, 2f), 1f));
Keep an imitator object's rotation in sync with another object:
StartCoroutine(ROTATION.SyncImitatorRotationWithCube(cubeTransform, imitatorTransform));
Efficiently save large JSON data by splitting it into chunks and storing it in PlayerPrefs
:
// Save
COLLECTION.SaveLargeDataToPlayerPrefs(largeDataObject, "unique_key");
// Load
var loadedData = COLLECTION.LoadLargeDataFromPlayerPrefs<MyDataClass>("unique_key");
Log large amounts of data to the console in chunks:
COLLECTION.LogLargeData(largeDataObject, "data_key");
Fetch the current date and time in a standard format:
string currentTime = TIME.Now();
// Returns "2024-09-14 12:34:56.789"
Calculate the total time elapsed between two timestamps:
string startTime = TIME.Now();
// Simulate some delay
yield return new WaitForSeconds(2);
string endTime = TIME.Now();
float elapsedTime = TIME.CalculateTotalTime(startTime, endTime);
Debug.Log($"Elapsed Time: {elapsedTime} ms");
This project is licensed under the MIT License. See the LICENSE
file for details.