Socket
Book a DemoInstallSign in
Socket

com.wallstop-studios.unity-helpers

Package Overview
Dependencies
Maintainers
1
Versions
163
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

com.wallstop-studios.unity-helpers

Various Unity Helper Library

2.0.0-rc58
latest
Source
npmnpm
Version published
Weekly downloads
287
-14.33%
Maintainers
1
Weekly downloads
 
Created
Source

A Grab-Bag

Various Unity Helpers. Includes many deterministic, seedable random number generators.

CI/CD Status

Npm Publish

Compatibility

PlatformCompatible
Unity 2021Likely, but untested
Unity 2022
Unity 2023
Unity 6
URP
HDRP

Installation

To Install as Unity Package

  • Open Unity Package Manager
  • (Optional) Enable Pre-release packages to get the latest, cutting-edge builds
  • Open the Advanced Package Settings
  • Add an entry for a new "Scoped Registry"
    • Name: NPM
    • URL: https://registry.npmjs.org
    • Scope(s): com.wallstop-studios.unity-helpers
  • Resolve the latest com.wallstop-studios.unity-helpers

From Source

Grab a copy of this repo (either git clone or download a zip of the source) and copy the contents to your project's Assets folder.

From Releases

Check out the latest Releases to grab the Unity Package and import to your project.

Package Contents

  • Random Number Generators
  • Spatial Trees
  • Protobuf, Binary, and JSON formatting
  • A resizable CyclicBuffer
  • Simple single-threaded thread pool
  • A LayeredImage for use with Unity's UI Toolkit
  • Geometry Helpers
  • Child/Parent/Sibling Attributes to automatically get components
  • ReadOnly attribute to disable editing of serialized properties in the inspector
  • An extensive collection of helpers
  • Simple math functions including a generic Range
  • Common buffers to reduce allocations
  • A RuntimeSingleton implementation for automatic creation/accessing of singletons
  • String helpers, like converting to PascalCase, like Unity does for variable names in the inspector
  • A randomizable PerlinNoise implementation
  • And more!

Auto Get(Parent/Sibling/Child)Component

Are you tired of constantly writing GetComponent() all over the place? Waste time no more!

public sealed class MyCoolScript : MonoBehaviour
{
    [SiblingComponent] // If it doesn't exist, will log an error
    private SpriteRenderer _spriteRenderer;

    [SiblingComponent(optional = true)] // Ok if it doesn't exist, no errors logged
    private BoxCollider2D _boxCollider;

    [ParentComponent] // Similar to GetComponentInParent<AIController>(includeInactive: true)
    private AIController _parentAIController;

    // Only include components in parents, Unity by default includes sibling components in the GetComponentsInParent call
    [ParentComponent(onlyAncestors = true)] 
    private Transform [] _allParentTransforms; // Works with arrays!

    [ParentComponent(includeInactive = false)] // Don't include inactive components
    private List<PolygonCollider2> _parentColliders; // Works with lists!

    [ChildComponent(onlyDescendents = true)] // Similar to GetComponentInChildren<EdgeCollider2D>(includeInactive: true)
    private EdgeCollider2D _childEdgeCollider;

    private void Awake()
    {
        /*
            Make sure this is called somewhere, usually in Awake, OnEnable, or Start - wherever this is called,
             values will be injected into the annotated fields and errors will be logged
        */
        this.AssignRelationalComponents();
    }
}

Random Number Generators

This package implements several high quality, seedable, and serializable random number generators. The best one (currently) is the PCG Random. This has been hidden behind the PRNG.Instance class, which is thread-safe. As the package evolves, the exact implementation of this may change.

All of these implement a custom IRandom interface with a significantly richer suite of methods than the standard .Net and Unity randoms offer.

To use:

IRandom random = PRNG.Instance;

random.NextFloat(); // Something between 0.0f and 1.0f
random.NextDouble(); // Something between 0.0d and 1.0d
random.Next(); // Something between 0 and int.MaxValue
random.NextUint(); // Something between 0 and uint.MaxValue

int [] values = {1, 2, 3};
random.NextOf(values); // 1, 2, or 3
random.NextOf(Enumerable.Range(0, 3)); // 1, 2, or 3
HashSet<int> setValues = new() {1, 2, 3};
random.NextOf(setValues); // 1, 2, or 3

random.NextGuid(); // A valid UUIDv4
random.NextGaussian(); // A value sampled from a gaussian curve around mean=0, stdDev=1 (configurable via parameters)
random.NextEnum<T>(); // A randomly selected enum of type T

int width = 100;
int height = 100;
random.NextNoiseMap(width, height); // A configurable noise map generated using random octave offsets

Implemented Random Number Generators

  • PCG
  • DotNet (uses the currently implemented .Net Random)
  • RomoDuo
  • SplitMix64
  • Squirrel
  • System (uses a port of the Windows .Net Random)
  • Unity (uses Unity's random under the hood)
  • Wy
  • XorShift
  • XorShiro

Performance (Number of Operations / Second)

RandomNextBoolNextNextUIntNextFloatNextDoubleNextUint - RangeNextInt - Range
PcgRandom34,260,47834,303,86635,148,24624,818,43830,034,63222,436,47420,801,797
SystemRandom28,698,80630,040,78220,435,09224,079,39927,284,14720,735,76919,861,780
SquirrelRandom33,285,78433,439,90934,611,89323,885,73128,958,72521,520,27920,311,372
XorShiftRandom34,695,98935,147,32035,997,71825,248,23831,582,99122,679,92821,255,319
DotNetRandom18,097,48918,191,04218,783,06314,061,44415,730,43913,284,05012,596,913
WyRandom28,490,85229,086,05229,724,90720,252,06524,542,20118,474,79017,404,641
SplitMix6434,301,84334,343,40435,512,28424,289,41630,586,23122,470,33020,850,965
RomuDuo32,969,88933,413,21234,339,22723,755,05929,483,13621,671,64120,253,479
XorShiroRandom31,529,02531,717,70932,536,27722,421,71527,619,41720,843,99319,270,063
UnityRandom24,925,26824,830,59426,429,28317,864,52821,206,38416,376,62615,528,972
LinearCongruentialGenerator35,013,81835,025,18235,843,53325,093,40131,553,48722,579,79821,211,175

Spatial Trees

There are three implemented 2D immutable spatial trees that can store generic objects, as long as there is some resolution function that can convert them into Vector2 spatial positions.

  • QuadTree (easiest to use)
  • KDTree
  • RTree

Spatial trees, after construction, allow for O(log(n)) spatial query time instead of O(n). They are extremely useful if you need repeated spatial queries, or if you have relatively static spatial data.

Usage

GameObject [] spatialStorage = { myCoolGameObject };
QuadTree<GameObject> quadTree = new(spatialStorage, go => go.transform.position);

// Might return your object, might not
GameObject [] inBounds = quadTree.GetElementsInBounds(new Bounds(0, 0, 100, 100));

// Uses a "good-enough" nearest-neighbor approximately for cheap neighbors
List<GameObject> nearestNeighbors = new();
quadTree.GetApproximateNearestNeighbors(myCoolGameObject.transform.position, 1, nearestNeighbors);
Assert.AreEqual(1, nearestNeighbors.Count);
Assert.AreEqual(myCoolGameObject, nearestNeighbors[0]);

Note

All spatial trees expect the positional data to be immutable. It is very important that the positions do not change. If they do, you will need to reconstruct the tree.

Contributing

This project uses CSharpier with the default configuration to enable an enforced, consistent style. If you would like to contribute, recommendation is to ensure that changed files are ran through CSharpier prior to merge. This can be done automatically through editor plugins, or, minimally, by installing a pre-commit hook.

Keywords

helper

FAQs

Package last updated on 11 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.