AceLand Unity Packages
  • Home
  • Getting Started
    • Installation
    • Project Settings
    • Architecture Graph
    • Development Level
  • Tutorial
    • Create Your Package
    • Create Project Settings
  • Packages
    • Library
      • Change Log
      • Editor Tools
      • Mono
        • Follow Object
        • Singleton
      • Attributes
        • Conditional Show
        • Inspector Button
        • ReadOnly Field
      • Build Leveling
      • CVS
      • DataTools
      • Disposable Object
      • Extensions
      • Json
      • Kalman Filter
      • Optional
      • Project Setting
      • Serialization Surrogate
      • Utils
    • Event Driven
      • Change Log
      • Event Bus
      • Signal
    • Input
      • Change Log
    • Memento Service
      • Change Log
    • Node Framework
      • Change Log
      • Mono Node
    • Node For Mono (dev)
    • Player Loop Hack
      • Change Log
    • Pool
      • Change Log
    • Security (dev)
    • States
      • Change Log
    • Task Utils
      • Change Log
    • WebRequest
      • Change Log
Powered by GitBook
On this page
  • Uncategorized
  • Array
  • Bounds
  • byte
  • Color
  • DateTime
  • Delegate
  • Dictionary
  • float
  • IEnumerable
  • int
  • LayerMask
  • long
  • Mathmatics.float2
  • Mathmatics.float3
  • Mathmatics.float4
  • Mathematics.quaternion
  • Quaternion
  • RectTransform
  • short
  • string
  • Texture2D
  • Transform
  • Vector2
  • Vector2Int
  • Vector3
  • Vector3Int
  • Vector4
  1. Packages
  2. Library

Extensions

An extension methods for Unity

Last updated 3 months ago

Extension Method is a very important function in c#. This is a start point of Functional Programming in c#.

please read this Microsoft .


Uncategorized

GameData data;

// Deep Clone any class object
GameData cloneData = data.DeepClone();

Array

int[] data = { 0, 1, 2, 3, 4, 5 };

// swap 2 elements in array
data.SwapElements(2, 5);    // output 0, 1, 5, 3, 4, 2

if given index is out of range, it will clamp to the range.


Bounds

// get random point in Bounds object
Vector3 randomPoint = myBound.RandomPoint();

byte

byte b = 16;

// convert to volume formatted string
var size = b.SizeSuffix(2)    // size = "16 bytes"

// convert to Currency formatted string
var amount = b.CurrencySuffix(2)    // amount = "16"

Color

Color myColor = new Color(0.5894f, 0.1398f, 0.648f, 1);

// get brightness of Color
float brightness = myColor.Brightness();

// convert to mono
Color mono = myColor.Mono();

// change r / g / b / a
Color result = myColor.R(0.5f);    // (0.5, 0.1398, 0.648, 1)
Color result = myColor.G(0.5f);    // (0.5894, 0.5, 0.648, 1)
Color result = myColor.B(0.5f);    // (0.5894, 0.1398, 0.5, 1)
Color result = myColor.A(0.5f);    // (0.5894, 0.1398, 0.648, 0.5)
Color result = myColor.RG(0.5f, 0.5f);    // (0.5, 0.5, 0.648, 1)
Color result = myColor.RB(0.5f, 0.5f);    // (0.5, 0.1398, 0.5, 1)
Color result = myColor.GB(0.5f, 0.5f);    // (0.5894, 0.5, 0.5, 1)
Color result = myColor.RGA(0.5f, 0.5f, 0.5f);    // (0.5, 0.5, 0.648, 0.5)
Color result = myColor.RBA(0.5f, 0.5f, 0.5f);    // (0.5, 0.1398, 0.5, 0.5)
Color result = myColor.GBA(0.5f, 0.5f, 0.5f);    // (0.5894, 0.5, 0.5, 0.5)

// Resize Color[]
// used by Texture2D.Resize()
// Color[].ResizePixels(oriWidth, oriHeight, newWidth, newHeight);
Color[] result = colors.ResizePixels(512, 512, 128, 128);

DateTime

DateTime now = DateTime.Now;
float secondFloat = 2686954.156f;
double secondDouble = 8932156246.1246516d;

// convert between DateTime and TimeSpan
TimeSpan timeSpan = now.ToTimeSpan();
DateTime dateTime = timeSpan.ToDateTime();

// convert seconds to DateTime
DateTime dateTimeFromFloat = secondFloat.ToDateTime();
DateTime dateTimeFromDouble = secondDouble.ToDateTime();

Delegate

Action action = someMethod;

// who is caller
string caller = action.GetOwnerName();

Dictionary

Dictionary<int, string> myDictionary = new()
{
    { 0, "Apple" },
    { 1, "Banana" },
    { 2, "Orange" },
    { 3, "Pineapple" },
    { 4, "Orange" },
};

// result = true, key = 2
bool result = myDictionary.TryGetFirstKeyByValue("Orange", out int key);

// key = 3
int key = myDictionary.GetKeyByValue("Pineapple");

// keys = { 2, 4 }
IEnumerable<int> keys = myDictionary.GetKeysByValue("Orange");

// swap values of key 1 and key 4
myDictionary.SwapValues(1, 4);

Log Warning if given index of SwapValues is out of range


float

float f = 5f;
float f2 = 9.5f;

// snap a float to value
int result = f2.SnapToCeil(5);     // 10
int result = f2.SnapToFloor(5);    // 5

// check a float is between given range
bool result = f.IsBetween(1, 5);    // false
bool result = f.IsBetween(1, 5, true);    // true: includingMax

// remap a float from first range to second range
float result = f.Remap(1, 10, 1, 5);    // 2.5

// remap a float from given range in linear scaling
float result = f.RemapUnclamped(1, 2, 1, 10);    // 37

// normalize a float from given range
float result = f.Remap01(1, 10);    // 0.5

IEnumerable

// foreach Range
foreach (var i in 10..20)
    Debug.Log(i);    // print 10 to 20

// foreach from 1 to int
foreach (var i in 10)
    Debug.Log(i);    // print 1 to 10

// efficient way to loop a LinkedList
var list = new LinkedList<int>();
list.AddLast(1);
list.AddLast(2);
list.AddLast(3);
list.AddLast(4);

// efficient way to loop a LinkedList
foreach (var item in list.EnumerateNodes())
    Debug.Log(item.Value);    // print 1 to 4

int

int i = 5;
int i2 = 7;

// check a integer is between given range
bool result = i.IsBetween(1, 5);    // false
bool result = i.IsBetween(1, 5, true);    // true: includingMax

// snap a int to value
int result = i2.SnapToCeil(5);     // 10
int result = i2.SnapToFloor(5);    // 5

// remap a integer from first range to second range
int result = i.RemapInt(1, 10, 1, 5);  // 2
float result = i.Remap(1, 10, 1, 5);    // 2.5

// normalize a integer from given range
float result = i.Remap01(1, 10);    // 0.5

// normalize a float from given range in linear scaling
int result = i.RemapIntUnclamped(2, 4, 1, 8);  // 11
float result = i.RemapUnclamped(2, 4, 1, 8);    // 11.5

// convert to volume formatted string
string result = i.SizeSuffix(2);    // 5 bytes
string result = 1_024.SizeSuffix(3); // 1.000 MB

// convert to Currency formatted string
string result = i.CurrencySuffix(2);    // 5
string result = 1_024.CurrencySuffix(0); // 1 K

LayerMask

LayerMask requiredLayers = 1 << 6;

// check Physics Layer is contains given layer
bool result = requiredLayers.Contains(gameObject.layer);

long

long l = 1_024_786;

// convert to volume formatted string
string result = l.SizeSuffix(2);    // 1.00 MB


// convert to Currency formatted string
string result = l.CurrencySuffix(2);    // 1.02 M

Mathmatics.float2

float2 f2 = new float2(-1.5f, 2.5f);

// check isNan
bool result = f2.IsNan();    // false

// scale a float2 value
float2 result = f2.Multiply(2);       // (-3, 5)
float2 result = f2.Multiply(0.5f);    // (-0.75, 1.25)

// Average of points (center point)
float2[] points = new float2[] {};
float2 result = points.Average();

// set element(s)
float2 result = f2.X(1f);    // (1, 2.5)
float2 result = f2.Y(1f);    // (-1.5, 1)

// adj element(s) by add
float2 result = f2.AdjX(1f);         // (-0.5, 2.5)
float2 result = f2.AdjY(1f);         // (-1.5, 3.5)
float2 result = f2.AdjXY(1f, 1f);    // (-0.5, 3.5)
float2 result = f2.AdjAll(1f);       // (-0.5, 3.5)

// convertion
float3 result = f2.Z(4.5f);          // (-1.5f, 2.5f, 4.5f)
float3 result = f2.YXZ(4.5f);        // (2.5f, -1.5f, 4.5f)
float4 result = f2.ZW(4.5f, 5.5f);   // (-1.5f, 2.5f, 4.5f, 5.5f)
float4 result = f2.YXZW(4.5f, 5.5f); // (2.5f, -1.5f, 4.5f, 5.5f)

Mathmatics.float3

float3 f3 = new float3(-1.5f, 2.5f, 0.8f);

// check isNan
bool result = f3.IsNan();    // false

// scale a float3 value
float3 result = f3.Multiply(2);       // (-3, 5, 1.6)
float3 result = f3.Multiply(0.5f);    // (-0.75, 1.25, 0.4)

// Average of points (center point)
float3[] points = new float3[] {};
float3 result = points.Average();

// set element(s)
float3 result = f3.X(1f);    // (1, 2.5, 0.8)
float3 result = f3.Y(1f);    // (-1.5, 1, 0.8)
float3 result = f3.Z(1f);    // (-1.5, 2.5, 1)
float3 result = f3.XY(1f, 1F);    // (1, 1, 0.8)
float3 result = f3.XZ(1f, 1F);    // (1, 2.5, 1)
float3 result = f3.YZ(1f, 1F);    // (-1.5, 1, 1)

// adj element(s) both by add
float3 result = f3.AdjX(1f);              // (-0.5, 2.5, 0.8)
float3 result = f3.AdjY(1f);              // (-1.5, 3.5, 0.8)
float3 result = f3.AdjZ(1f);              // (-1.5, 2.5, 1.8)
float3 result = f3.AdjXY(1f);             // (-0.5, 3.5, 0.8)
float3 result = f3.AdjYZ(1f);             // (-1.5, 3.5, 1.8)
float3 result = f3.AdjXZ(1f);             // (-0.5, 2.5, 1.8)
float3 result = f3.AdjXYZ(1f, 1f, 1f);    // (-0.5, 3.5, 1.8)
float3 result = f3.AdjAll(1f);            // (-0.5, 3.5, 1.8)

Mathmatics.float4

float4 f4 = new float4(-1.5f, 2.5f, 0.8f, -1); // x, y, z, w

// check isNan
bool result = f4.IsNan();    // false

// set element(s)
float4 result = f4.X(1f);    // (1, 2.5, 0.8, -1)
float4 result = f4.Y(1f);    // (-1.5, 1, 0.8, -1)
float4 result = f4.Z(1f);    // (-1.5, 2.5, 1, -1)
float4 result = f4.W(1f);    // (-1.5, 2.5, 0.8, 1)
float4 result = f4.XY(1f, 1f);    // (1, 1, 0.8, -1)
float4 result = f4.XZ(1f, 1f);    // (1, 2.5, 1, -1)
float4 result = f4.XW(1f, 1f);    // (1, 2.5, 0.8, 1)
float4 result = f4.YZ(1f, 1f);    // (-1.5, 1, 1, -1)
float4 result = f4.YW(1f, 1f);    // (-1.5, 1, 0.8, 1)
float4 result = f4.ZW(1f, 1f);    // (-1.5, 2.5, 1, 1)
float4 result = f4.XYZ(1f, 1f, 1f);    // (1, 1, 1, -1)
float4 result = f4.XYW(1f, 1f, 1f);    // (1, 1, 0.8, 1)
float4 result = f4.XZW(1f, 1f, 1f);    // (1, 2.5, 1, 1)
float4 result = f4.YZW(1f, 1f, 1f);    // (-1.5, 1, 1, 1)

// adj element(s) both by add
float4 result = f4.AdjX(1f);    // (-0.5f, 2.5, 0.8, -1)
float4 result = f4.AdjY(1f);    // (-1.5f, 3.5, 0.8, -1)
float4 result = f4.AdjZ(1f);    // (-1.5f, 2.5, 1.8, -1)
float4 result = f4.AdjW(1f);    // (-1.5f, 2.5, 0.8, 0)
float4 result = f4.AdjXY(1f, 1f);    // (-0.5f, 2.5, 0.8, -1)
float4 result = f4.AdjXZ(1f, 1f);    // (-0.5f, 2.5, 1.8, -1)
float4 result = f4.AdjXW(1f, 1f);    // (-0.5f, 2.5, 0.8, 0)
float4 result = f4.AdjYZ(1f, 1f);    // (-1.5f, 3.5, 1.8, -1)
float4 result = f4.AdjYW(1f, 1f);    // (-1.5f, 3.5, 0.8, 0)
float4 result = f4.AdjZW(1f, 1f);    // (-1.5f, 2.5, 1.8, 0)
float4 result = f4.AdjXYZ(1f, 1f, 1f);    // (-0.5f, 3.5, 1.8, -1)
float4 result = f4.AdjXYW(1f, 1f, 1f);    // (-0.5f, 3.5, 0.8, 0)
float4 result = f4.AdjYZW(1f, 1f, 1f);    // (-1.5f, 3.5, 1.8, 0)
float4 result = f4.AdjXYZW(1f, 1f, 1f, 1f);    // (-0.5f, 3.5, 1.8, 0)
float4 result = f4.AdjAll(1f);                 // (-0.5f, 3.5, 1.8, 0)

Mathematics.quaternion

quaternion rotation = (quaternion)transform.rotation;

// check isNan
quaternion result = rotation.IsNan();

// negative quaternion
quaternion result = rotation.Negative();

Quaternion

Quaternion rotation = transform.rotation;

// negative quaternion
Quaternion result = rotation.Negative();

RectTransform

RectTransform rectTransform;

// get bounds with RectTransfrom local corners
Bounds result = rectTransform.ToBounds();

// bet bounds with RectTransfrom world corners
Bounds result = rectTransform.ToWorldBounds();

short

short s = 1_024;

// convert to volume formatted string
string result = s.SizeSuffix(2);    // 1.00 KB

// convert to Currency formatted string
string result = s.CurrencySuffix(2);    // 1.02 K

string

string json = GetJson();

// check null or empty or white space
bool result = json.IsNullOrEmptyOrWhiteSpace();

// save string to a file
Task result = json.SaveToFile(fullPathFilename, cancellationToken);

// load string from a file
Task result = fullPathFilename.LoadFromFile(cancellationToken);

Texture2D

// Resize to specified size
texture.Resize(1280, 720);

// Resize to maxSize (width or height)
texture.Resize(720);

// Crop and Resize to Square
// image will place on center
texture.CropAndSizeToSquare(512);

Transform

Transfrom transform = transform;
YourComponent mono = GetComponent<YourComponent>();

// destroy all children
transform.DestroyAllChildren();
mono.DestroyAllChildren();

// destroy children with specified component
transform.DestroyAllChildren<Rigidbody>();
mono.DestroyAllChildren<Rigidbody>();

// copy transform data from given transform
transform.CopyDataFrom(sourceTransform);
transform.CopyDataFrom(sourceTransform, true);    // copy scale

CopyDataFrom with scale may cause error because scale is local space.

It's recommended to set the localscale if scale is not 1.


Vector2

Vector2 v2 = new Vector2(-1.5f, 2.5f);

// remap a Vector2 from first range to second range
Vector2 result = v2.Remap(from1, to1, from2, to2);

// remap a Vector2 from given range in linear scaling
Vector2 result = v2.RemapUnclamped(from1, to1, from2, to2);

// normalize a Vector2 from given range
Vector2 result = v2.Remap01(from1, to1);

// scale a float2 value
Vector2 result = v2.Multiply(2);       // (-3, 5)
Vector2 result = v2.Multiply(0.5f);    // (-0.75, 1.25)

// Average of points (center point)
Vector2[] points = new Vector2[] {};
Vector2 result = points.Average();

// get arranged element(s)
Vector2 yx = v2.YX();

// set element(s)
Vector2 result = v2.X(3.5f);
Vector2 result = v2.Y(-4.5f);

// adj element(s) by add
Vector2 result = v2.AdjX(1.2f);
Vector2 result = v2.AdjY(-0.8f);
Vector2 result = v2.AdjXY(1.2f, -0.8f);
Vector2 result = v2.AdjAll(5.8f);

// convertion
Vector3 result = v2.Z(4.5f);          // (-1.5f, 2.5f, 4.5f)
Vector3 result = v2.YXZ(4.5f);        // (2.5f, -1.5f, 4.5f)
Vector4 result = v2.ZW(4.5f, 5.5f);   // (-1.5f, 2.5f, 4.5f, 5.5f)
Vector4 result = v2.YXZW(4.5f, 5.5f); // (2.5f, -1.5f, 4.5f, 5.5f)

Vector2Int

Vector2Int v2 = new Vector2Int(2, -3);

// CompareTo
int result = v2.CompareTo(value);

// get arranged value
Vector2Int yx = v2.YX();

// set element(s)
Vector2Int result = v2.X(3);
Vector2Int result = v2.Y(-4);

// adj element(s) by add
Vector2Int result = v2.AdjX(1);
Vector2Int result = v2.AdjY(4);
Vector2Int result = v2.AdjXY(1, 4);
Vector2Int result = v2.AdjAll(5);

Vector3

Vector3 v3 = new Vector3(-1.5f, 2.5f, 0.8f);

// remap a Vector3 from first range to second range
Vector3 result = v3.Remap(from1, to1, from2, to2);

// remap a Vector3 from given range in linear scaling
Vector3 result = v3.RemapUnclamped(from1, to1, from2, to2);

// normalize a Vector3 from given range
Vector3 result = v3.Remap01(from1, to1);

// scale a Vector3 value
Vector3 result = v3.Multiply(2);        // (-1.5, 2.5, 0.8)
Vector3 result = v3.Multiply(0.5f);     // (-1.5, 2.5, 0.8)

// Average of points (center point)
Vector3[] points = new Vector3[] {};
Vector3 result = points.Average();

// get arranged value
Vector2 xy = v3.XY;       // (-1.5, 2.5)
Vector2 xz = v3.XZ;       // (-1.5, 0.8)
Vector2 yx = v3.YX;       // (-1.5, -1.5)
Vector2 zx = v3.ZX;       // (0.8, -1.5)
Vector3 yxz = v3.YXZ;     // (2.5, -1.5, 0.8)

// set element(s)
Vector3 result = v3.X(1f);         // (1, 2.5, 0.8)
Vector3 result = v3.Y(1f);         // (-1.5, 1, 0.8)
Vector3 result = v3.Z(1f);         // (-0.5, 2.5, 0.8)
Vector3 result = v3.XY(1f, 1F);    // (1, 1, 0.8)
Vector3 result = v3.XZ(1f, 1F);    // (1, 2.5, 1)
Vector3 result = v3.YZ(1f, 1F);    // (-1.5, 1, 1)

// adj element(s) both by add
Vector3 result = v3.AdjX(1f);              // (-0.5, 2.5, 0.8)
Vector3 result = v3.AdjY(1f);              // (-1.5, 3.5, 0.8)
Vector3 result = v3.AdjZ(1f);              // (-1.5, 2.5, 1.8)
Vector3 result = v3.AdjXY(1f);             // (-0.5, 3.5, 0.8)
Vector3 result = v3.AdjYZ(1f);             // (-1.5, 3.5, 1.8)
Vector3 result = v3.AdjXZ(1f);             // (-0.5, 2.5, 1.8)
Vector3 result = v3.AdjXYZ(1f, 1f, 1f);    // (-0.5, 3.5, 1.8)
Vector3 result = v3.AdjAll(1f);            // (-0.5, 3.5, 1.8)

Vector3Int

Vector3Int v3 = new Vector3Int(-1, 2, 5);

// get arranged value
Vector2Int xy = v3.XY;
Vector2Int xz = v3.XZ;
Vector2Int yx = v3.YX;
Vector2Int zx = v3.ZX;)
Vector3Int yxz = v3.YXZ;8)

// set element(s)
Vector3Int result = v3.x(3);
Vector3Int result = v3.yz(-4, 1);
Vector3Int result = v3.y(-4);
Vector3Int result = v3.xz(3, 1);
Vector3Int result = v3.z(1);
Vector3Int result = v3.xy(3, -4);

// adj element(s) by add
Vector3Int result = v3.AdjX(1);
Vector3Int result = v3.AdjY(-4);
Vector3Int result = v3.AdjZ(1);
Vector3Int result = v3.AdjXY(-4);
Vector3Int result = v3.AdjYZ(-4);
Vector3Int result = v3.AdjXZ(-4);
Vector3Int result = v3.AdjXYZ(1, -4, -2);
Vector3Int result = v3.AdjAll(5);

Vector4

Vector4 v4 = new Vector4(-1.5f, 2.5f, 0.8f, 1f);

// set element(s)
float4 result = v4.X(1f);    // (1, 2.5, 0.8, -1)
float4 result = v4.Y(1f);    // (-1.5, 1, 0.8, -1)
float4 result = v4.Z(1f);    // (-1.5, 2.5, 1, -1)
float4 result = v4.W(1f);    // (-1.5, 2.5, 0.8, 1)
float4 result = v4.XY(1f, 1f);    // (1, 1, 0.8, -1)
float4 result = v4.XZ(1f, 1f);    // (1, 2.5, 1, -1)
float4 result = v4.XW(1f, 1f);    // (1, 2.5, 0.8, 1)
float4 result = v4.YZ(1f, 1f);    // (-1.5, 1, 1, -1)
float4 result = v4.YW(1f, 1f);    // (-1.5, 1, 0.8, 1)
float4 result = v4.ZW(1f, 1f);    // (-1.5, 2.5, 1, 1)
float4 result = v4.XYZ(1f, 1f, 1f);    // (1, 1, 1, -1)
float4 result = v4.XYW(1f, 1f, 1f);    // (1, 1, 0.8, 1)
float4 result = v4.XZW(1f, 1f, 1f);    // (1, 2.5, 1, 1)
float4 result = v4.YZW(1f, 1f, 1f);    // (-1.5, 1, 1, 1)

// adj element(s) both by add
float4 result = v4.AdjX(1f);    // (-0.5f, 2.5, 0.8, -1)
float4 result = v4.AdjY(1f);    // (-1.5f, 3.5, 0.8, -1)
float4 result = v4.AdjZ(1f);    // (-1.5f, 2.5, 1.8, -1)
float4 result = v4.AdjW(1f);    // (-1.5f, 2.5, 0.8, 0)
float4 result = v4.AdjXY(1f, 1f);    // (-0.5f, 2.5, 0.8, -1)
float4 result = v4.AdjXZ(1f, 1f);    // (-0.5f, 2.5, 1.8, -1)
float4 result = v4.AdjXW(1f, 1f);    // (-0.5f, 2.5, 0.8, 0)
float4 result = v4.AdjYZ(1f, 1f);    // (-1.5f, 3.5, 1.8, -1)
float4 result = v4.AdjYW(1f, 1f);    // (-1.5f, 3.5, 0.8, 0)
float4 result = v4.AdjZW(1f, 1f);    // (-1.5f, 2.5, 1.8, 0)
float4 result = v4.AdjXYZ(1f, 1f, 1f);    // (-0.5f, 3.5, 1.8, -1)
float4 result = v4.AdjXYW(1f, 1f, 1f);    // (-0.5f, 3.5, 0.8, 0)
float4 result = v4.AdjYZW(1f, 1f, 1f);    // (-1.5f, 3.5, 1.8, 0)
float4 result = v4.AdjXYZW(1f, 1f, 1f, 1f);    // (-0.5f, 3.5, 1.8, 0)
float4 result = v4.AdjAll(1f);                 // (-0.5f, 3.5, 1.8, 0)

please see package .

official document
Aceland Task Utils