Utils
A helper function for Unity
Utils is an important part in library. A short brief is Helper function.
Platform
providing platform infomation
string name = Platform.CurrentPlatform
bool result = Platform.IsPlaying
bool result = Platform.IsEditor
// Mobile
bool result = Platform.IsMobile
bool result = Platform.IsAndroid
bool result = Platform.IsIphone
// Destop
bool result = Platform.IsDesktop
bool result = Platform.IsPC
bool result = Platform.IsMac
bool result = Platform.IsLinux
// WebGL
bool result = Platform.IsWebGL
// Server
bool result = Platform.IsServer
bool result = Platform.IsWinServer
bool result = Platform.IsOSXServer
bool result = Platform.IsLinuxServer
// Console
bool result = Platform.IsConsole
bool result = Platform.IsPS
bool result = Platform.IsPS4
bool result = Platform.IsPS5
bool result = Platform.IsSwitch
bool result = Platform.IsXboxOne
// Apple TV
bool result = Platform.IsAppleTV
// Windows Store App
bool result = Platform.IsWindowsStoreApp
bool result = Platform.IsWSA_Arm
bool result = Platform.IsWSA_x86
bool result = Platform.IsWSA_x64
BinaryFormatter
// get Binary Formatter with supported Surrogate in library
IFormatter formatter = Helper.GetBinaryFormatter();
Color
// create a random color
Color result = Helper.RandomColor();
// create a random color with given alpha
Color result = Helper.RandomColor(0.5f);
float
// smooth damp
float dampValue = Helper.SmoothDamp(current, target,
ref velocity, smoothTime, maxSpeed, deltaTime);
// inverse lerp result
float result - Helper.InverseLerp(a, b, value)
GameObject
// create a gameobject with given name
GameObject result = Helper.CreateEmptyInHierarchy("My GameObject");
// create a gameobject with given name and parent
GameObject result = Helper.CreateEmptyInHierarchy("My GameObject", parentTransform);
Mathmatics.float2
// check is all close
bool result = Helper.AllClose(a, b, epsilon)
// remap a float2 value from range to range
float2 value = Helper.Remap(value, from1, to1, from2, to2);
// calculate new position after move towards
float2 position = Helper.MoveTowards(current, target, maxDistanceDeta);
// clamp to given length
float2 result = Helper.ClampLength(value, maxLength);
// smooth damp
float2 dampValue = Helper.SmoothDamp(current, target,
ref velocity, smoothTime, maxSpeed, deltaTime);
Mathmatics.float3
// check is all close
bool result = Helper.AllClose(a, b, epsilon)
// remap a float2 value from range to range
float3 value = Helper.Remap(value, from1, to1, from2, to2);
// calculate new position after move towards
float3 position = Helper.MoveTowards(current, target, maxDistanceDeta);
// clamp to given length
float3 result = Helper.ClampLength(value, maxLength);
// smooth damp
float3 dampValue = Helper.SmoothDamp(current, target,
ref velocity, smoothTime, maxSpeed, deltaTime);
string
public enum CharacterType
{
FullCharacters,
Characters,
Letters,
Digits,
}
// create a random string
string result = Helper.GetRandomString(length, CharacterType.Letters);
// encode and decode string in Base64
string encode = Helper.Base64Encode(text, Encoding.Default);
string decode = Helper.Base64Decode(encode, Encoding.Default);
// spilt path and filename
Helper.SplitPathAndFilename(fullPath, out var path, out var filename);
UI
// check a screen position is over an UI element
bool result = Helper.IsOverUIElement(screenPosition);
// check a screen position is over an UI element with displayIndex
bool result = Helper.IsOverUIElement(screenPosition, displayIndex);
Vector3
public enum TransformDirection
{
Left, Right, Up, Down, Forward, Back,
}
// change TransformDirection to Vector3 value in world space
Vector3 result = Helper.GetRotateAxis(transformDirection.Left);
// get a smooth direction when hit on object
Vector3 result = Helper.SmoothDirection(normal, inDirection)
WebCam
this may be separated as a single package.
// get total number of webcam
int result = Helper.WebCamCount;
// check device has webcam
bool result = Helper.HasWebCam;
// try get webcam
bool result = Helper.TryGetWebCam(camId, width, height, fps,
out var webCamDevice, out var webCamTexture);
// try get front webcam (camId = 0)
bool result = Helper.TryGetFrontCam(width, height, fps,
out var webCamDevice, out var webCamTexture);
Last updated