/unity-utils

Useful unity utilities

Primary LanguageC#

Some little pieces of code I find useful enough to have them in several projects.

To install add next line to Packages/manifest.json
"trulden.unity-utils": "https://github.com/kirillsmirnov1/unity-utils.git",

XVariable

XVariable inheritors are used to:

  • store data;
  • pass data between objects;
  • save data;
  • inject object references.

Full readme on XVariable class.

MonoScale

Component to change scale of object with one slider.

VisualEffects

LazyFade component fades in and out all child components with Color. You can pass callback to SetVisibility() to be called when fade animation is finished.

UiFadePanel is helper component for LazyFade. Show() sets LazyFade child active and calls it's SetVisibility(). Hide() does the opposite thing. Accepts finish callbacks similar to LazyFade.

Attributes

NamedArrayAttribute displays array indexes as given enum values.

UI

InvokableButton

Allows to «click» on button from code using Invoke().

Camera

FitCamera fits orthographic camera to chosen object in scene by width/height with chosen coefficient.

Saves

Call DateChecker.CheckSaveDate(DateTime) to check if any amount of days passed since date in question. If that is so, DateChecker.LoadOnNewDateCallback will be invoked. If not — DateChecker.LoadOnSameDateCallback.

With SaveIO you can write (and read) objects as JSON strings to (from) persistentDataPath.

Extensions

MonoBehaviourNullCheck

CheckNullFields(MonoBehaviour o) puts warnings in console, if object in questions has some public or private serialized fields being null.

GameObjectExtensions

InPrefabScene() checks if current object is in prefab scene. I use it in pair with CheckNullFields().

LINQ extensions

IEnumerable<T> Shuffle<T>() shuffles IEnumerable. It does so by calling OrderBy with some random values;

RandomExtensions

RandomValueInRangeSegment(float from, float to, int segmentCount, int segmentIndex)
Separates [from, to] to segmentCount ranges and generates random value in segmentIndex range.

DebugExtensions

DrawCross(Vector3 pos, float time, float length) draws debug cross

EnumExtensions

T Next<T>(this T src) where T : Enum returns next enum element. For last element returns first one.