Skip to content

A collection of utility scripts for Unity, designed to simplify common tasks and enhance development efficiency. Features include color manipulation, list and array operations, rotation and transformation utilities, large data handling, and time utilities. Ideal for Unity developers seeking to streamline their workflows and improve code reuse.

License

Notifications You must be signed in to change notification settings

MohammadMahdi-Abdolhosseini/Helper-Utility-for-Unity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Unity Helper Library

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.

Features

  • 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.

Installation

  1. Download the HELPER script.
  2. Import the Helper namespace into your Unity project:
    using HELPER;

Usage

1. Color Utilities

Generate Random Colors

Generate a list of unique random colors with ease:

int colorCount = 5;
List<Color> uniqueColors = COLOR.GenerateUniqueColors(colorCount);

Get a Random Color

Generate a single random color:

Color randomColor = COLOR.RandomColor();

Adjust a Color

Modify the hue and value of a given color:

Color adjustedColor = COLOR.AdjustColor(Color.red, 0.1f, -0.2f);

Convert Color to Hex

Convert any Color to a hex string for use in UIs or serialization:

string hexColor = COLOR.ToHex(Color.blue);
// Returns "#0000FF"

2. Collection Utilities

Shuffle a List

Shuffle any list of elements randomly:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
List<int> shuffledNumbers = COLLECTION.ShuffleList(numbers);

Get Unique Random Items from a List

Select a unique subset of items from a list:

List<int> selectedItems = COLLECTION.GetUniqueRandomItems(numbers, 3);

Rotate a 2D Array

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)}");

3. Rotation Utilities

Get Nearest Face Rotation

Snap an object’s rotation to the nearest face of a cube:

Quaternion currentRotation = someTransform.rotation;
Quaternion nearestFaceRotation = ROTATION.GetNearestFaceRotation(currentRotation);

Rotate an Object with Inertia

Apply a smooth rotation effect with inertia:

StartCoroutine(ROTATION.ApplyInertia(cubeTransform, new Vector2(1f, 2f), 1f));

Sync Rotation with Imitator

Keep an imitator object's rotation in sync with another object:

StartCoroutine(ROTATION.SyncImitatorRotationWithCube(cubeTransform, imitatorTransform));

4. Large Data Utilities

Save and Load Large Data to PlayerPrefs

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 Data

Log large amounts of data to the console in chunks:

COLLECTION.LogLargeData(largeDataObject, "data_key");

5. Time Utilities

Get the Current Date and Time

Fetch the current date and time in a standard format:

string currentTime = TIME.Now();
// Returns "2024-09-14 12:34:56.789"

Calculate Total Time

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");

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A collection of utility scripts for Unity, designed to simplify common tasks and enhance development efficiency. Features include color manipulation, list and array operations, rotation and transformation utilities, large data handling, and time utilities. Ideal for Unity developers seeking to streamline their workflows and improve code reuse.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages