Skip to content

sator-imaging/Unity-Fundamentals

Repository files navigation

Unity Scripting Fundamentals

๐Ÿ‡ฏ๐Ÿ‡ต ๆ—ฅๆœฌ่ชž็‰ˆ

Fundamental scripting library for Unity designed to be minimal, efficient & dependency-free as possible. Most of scripts are C# / .NET compliant. See using statements in .cs files for details.

Tip

Licensed under the MIT License unless otherwise described.
Supported Unity version: Unity 2021.3+

Installation

Add the following git URL in Unity Package Manager (UPM)

Release version

https://github.com/sator-imaging/Unity-Fundamentals.git#v1.3.1

ย  Append desired version at the end. (#vX.Y.Z)

Latest version

https://github.com/sator-imaging/Unity-Fundamentals.git

ย  very experimental, may have breaking changes without notice.

Features

Plain C# APIs

Non-Alloc String Splitter

Split string without allocation. ๐Ÿ“˜ or ๆ—ฅๆœฌ่ชž็‰ˆ

Observable Action

Transform event Action<T> to IObservable<T>.

WhenEachEnumerator

Task.WhenEach for Unity / .NET Standard 2.1 ๐Ÿ“˜ or ๆ—ฅๆœฌ่ชž็‰ˆ

Defer

Providing function for early-finally pattern. ๐Ÿ“˜

TODO: implement IAsyncDisposable overloads.

  • ex) await using var _ = Defer.New(async () => await ...);

StrictEnum

Unlike other enum utility, this class re-use system cache and also support parsing Flags value. ๐Ÿ“˜

SpanList<T>

List implementation of Span<T> especially designed to work with Span<char>. ๐Ÿ“˜

TODO: add (int, int) GetMinMaxLength()

  • to prevent enumerating repeatedly to get same result.
  • add bool _isFrozen to determine recalculate is required. Write turns it off when changed.
  • add bool IsFormattable with more strict token check: {one}{two} (currently accepted but must be rejected)
  • one pass replacement for FormatNonAlloc: search for { then perform .Slice(foundIndex, fromTokenMaxLength), check which one is match.

MiniXXHash

Minimal xxHash32 / xxHash64 implementation. ๐Ÿ“˜ or ๆ—ฅๆœฌ่ชž็‰ˆ

License: BSD 2-Clause

xxHash Clean C Reference Implementation

xxHash Library
Copyright (c) 2012-2020 Yann Collet
Copyright (c) 2019-2020 Devin Hussey (easyaspi314)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

UString

Lightning-fast non-alloc string builder faster than DefaultInterpolatedStringHandler ๐Ÿ“˜ or ๆŠ€่ก“็š„ใช่งฃ่ชฌ

NOTE: depending on StrictEnum TODO: benchmark

Poolable<T>

Self-contained singly linked list based object pool. ๐Ÿ“˜ or ๆ—ฅๆœฌ่ชž็‰ˆ

TODO: write tests

Sentinel

Fast & efficient exclusive or concurrent thread/event manager. ๐Ÿ“˜

Run

Job runner for Unity providing reliable un-async-ing functions and more.

Note

InitializeMainThreadContext may be required to being called on Unity startup. (it called automatically by default)

TODO: documentation for OnMainThread, InThreadPool, SetExceptionHandler, GetTimerToken, GetElapsedTime, Shutdown, CreateNewScheduler, SetConcurrentThreadCount

ThreadSafeSingleton

Thread! safe!! singleton!!! ๆ—ฅๆœฌ่ชž็‰ˆ

HalfUlid

ๅฎŸ่ฃ…ใฎ่ฉณ็ดฐ

https://github.com/sator-imaging/Half-Ulid

NUnit-compatible Framework for Unity Editor

Crazy stuff. No need to use this anymore as unity asset store now accepts package.json. ๐Ÿ“˜

Unity Runtime APIs

Observable UnityEvent

Transform UnityEvent to IObservable<T>. ๐Ÿ“˜

Nullable support for UnityEngine.Object

Reliable nullable (?? ?. ??=) support for UnityEngine.Object. ๐Ÿ“˜

ManagedShell

Provides functions that avoid creating leaked managed shell. ๐Ÿ“˜ or ่งฃ่ชฌใใฎ๏ผ‘ ใใฎ๏ผ’

PoolableBehaviour<T>

Self-contained singly linked list based MonoBehaviour pool. ๐Ÿ“˜ or ๆ—ฅๆœฌ่ชž็‰ˆ

UI Toolkit Core Extensions

  • ExecuteAfter extension method
    • unlike builtin ExecuteLater method, this method runs action right after specified number of repaint events.
  • DisableStyleTransitionScope extension method
    • this method temporarily turns off all transitions and turns them back on when leaving the IDisposable scope. useful for immediate style update without transition animation.

UI Toolkit Event Subscription

Use extension method RegisterCallbackAsSubscription to register event as IDisposable interface. It allows easily unregister event later.

UI Toolkit Dropdown Helper

Extension method RegisterCallbackAsEnum allows implement typed callback. And also there is option to delay event to correctly handle dropdown event. (if no delay, dropdown in Unity editor behaves like 'stop-the-world' and some actions are not work as expected)

NOTE: depending on StrictEnum

Unity Editor Scripts

C# API Documentation for IDE

Download C# API Documentation from Nuget.org. ๐Ÿ“˜

License: Apache License version 2.0

UnityEditorMainToolbar

Provide access to VisualElement in Unity main toolbar. ๐Ÿ“˜ or ๆ—ฅๆœฌ่ชž็‰ˆ

Leaked Managed Shell Detector

Not perfect. Just for reference. ๐Ÿ“˜

Assembly Definitions Manager for Project Settings Panel

TODO: documentation

WIP: sealed-able Type Finder

As IL2CPP bloats resulting C++ code if C# class is not marked with sealed modifier.

Obsolete

CancellationToken based Lifecycle Manager for Unity Editor

Obsolete features still exist as .txt files. ๐Ÿ“˜ or ๆ—ฅๆœฌ่ชž็‰ˆ

API Reference

https://sator-imaging.github.io/Unity-Fundamentals/api/index.html

ย 
ย 

Devnote

DefineConstants

How to set DefineConstants in .csproj file from dotnet command, see Directory.Build.Props and Program.cs for details.

Source: dotnet/sdk#9562 (comment)

Gemini Code Assist for GitHub

Commands

Post as a comment for pull request.

  • /gemini summary
  • /gemini review
  • /gemini
  • /gemini help

How to Customize

https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github#style-guide