com.cosminb.unity-internals
is a high-performance Unity library that exposes internal bindings to enable zero-allocation operations using Span<byte>
and NativeArray<byte>
. This package allows direct interaction with Unity's native code methods, eliminating managed array overhead for critical performance-sensitive operations.
This library uses a sophisticated build pipeline to safely expose Unity's internal methods:
Runtime~/ (Source Code) → ProcessAssembly.sh → Compilation → Cecil IL Injection → ExposedBindings.dll
- Source Code (
Runtime~/
): Contains C# extension methods and stub implementations - ProcessAssembly.sh: Orchestrates the build process
- Compilation: Creates a standard .NET assembly
- Cecil Processing: Injects IL code to call Unity's internal
_Injected
methods - Final DLL: The processed assembly with direct native bindings
The Runtime~
folder (with ~
suffix) prevents Unity from compiling the source directly, as the code requires Cecil processing to function properly.
- Zero-Allocation Image Loading: Load textures directly from
Span<byte>
orNativeArray<byte>
- Optimized AssetBundle Loading: Load asset bundles without unnecessary memory allocations
- Cross-Platform Compatibility: Tested on Android, WebGL, macOS, Windows, Linux, and iOS
- IL2CPP Support: Seamless integration with development and release builds
- Minimal Performance Overhead: Utilizes Mono.Cecil for efficient method exposure
- Unity 6000.0.31f1 or later
- .NET 6.0 SDK (for Cecil processing)
- Mono.Cecil NuGet package
Unity Package Manager supports installing packages from Git subfolders using the ?path=
parameter:
- Open Unity Package Manager (Window > Package Manager)
- Click the
+
button and select "Add package from git URL" - Enter:
https://github.com/Cosmin-B/unity-native-exposed-bindings.git?path=Assets/Packages/com.cosminb.unity-internals
Alternatively, add directly to your Packages/manifest.json
:
{
"dependencies": {
"com.cosminb.unity-internals": "https://github.com/Cosmin-B/unity-native-exposed-bindings.git?path=Assets/Packages/com.cosminb.unity-internals"
}
}
- Clone or download this repository
- Open Unity Package Manager
- Click
+
→ "Add package from disk" - Navigate to
Assets/Packages/com.cosminb.unity-internals/package.json
- Clone or download this repository
- Copy
Assets/Packages/com.cosminb.unity-internals
to either:- Your project's
Packages/
folder (recommended) - Your project's
Assets/
folder
- Your project's
Note: The package includes a pre-built ExposedBindings.dll
that works with Unity 6000.0.31f1+.
If you need to rebuild for a different Unity version:
- Copy
ProcessAssembly.sh
andCecilProcessor/
from this repository to your project root - Run:
chmod +x ProcessAssembly.sh ./ProcessAssembly.sh
using Unity.Collections;
using ExposedBindings;
// Load texture from NativeArray
NativeArray<byte> imageData = ...;
Texture2D texture = new Texture2D(2, 2);
bool success = texture.LoadImageNative(imageData);
// Load AssetBundle from Span
Span<byte> bundleData = ...;
var request = AssetBundleExtensions.LoadFromMemoryAsyncSpan(bundleData);
The library uses Mono.Cecil to:
- Recreate Unity's internal
ManagedSpanWrapper
- Replace stub method bodies
- Enable direct access to Unity's internal methods
- Minimize runtime performance impact
- Platforms: Android, WebGL, macOS, Windows, Linux, iOS
- Build Types: Mono and IL2CPP
- Unity Version: 6000.0.31f1 and later
- Reduced GC pressure
- Eliminated array copying overhead
- Direct NativeArray usage in Jobs
- Potential Burst compatibility
Unlike reflection-based approaches, this library uses Mono.Cecil for IL injection because:
- Zero Runtime Overhead: IL injection happens at build time, not runtime
- No Allocations: Direct method calls without boxing/unboxing
- Burst Compatible: Potential for Burst compilation (reflection blocks this)
- IL2CPP Safe: Works seamlessly with IL2CPP builds
- Performance: Direct native calls without reflection overhead
- Unity Version Dependency: Internal method signatures can change between Unity versions
- Cecil Processing Required: Must run ProcessAssembly.sh after package updates
- Blittable Types Only: Only supports types that can be directly mapped to memory
- No Multi-dimensional Arrays: Limited to single-dimensional arrays
- Platform Testing Required: Test on each target platform before shipping
- Always test thoroughly after Unity updates
- Rebuild the assembly when upgrading Unity versions
- The CecilProcessor may need adjustments for new Unity versions
- Consider maintaining version-specific branches for different Unity releases
If you want to modify the library or rebuild for a different Unity version:
- Clone this repository
- Ensure you have .NET 6.0 SDK installed
- Unity 6000.0.31f1 or your target version installed
# Make the script executable
chmod +x ProcessAssembly.sh
# Run the processor
./ProcessAssembly.sh
This will:
- Build the CecilProcessor from source
- Compile the source code from
Runtime~/
- Process the assembly with Cecil to inject IL code
- Output
ExposedBindings.dll
to thePlugins/
folder
- Update Unity path in
ProcessAssembly.sh
if needed - Modify
CecilProcessor/Program.cs
if internal signatures changed - Test thoroughly on your target Unity version
- Process assembly with Cecil
- Include processed assembly in
Plugins/
- Update
package.json
version - Create GitHub release
- Publish to UPM registry (optional)
This project is inspired by Sebastian Schöner's excellent blog post: Unmanaging Unity
Key insights from Sebastian's work:
- Unity's internal
ManagedSpanWrapper
structure for zero-copy operations - The existence of
_Injected
methods that accept spans directly - How Unity's internal marshalling works with native code
Our improvements:
- Using Mono.Cecil for compile-time IL injection instead of runtime reflection
- Full IL2CPP compatibility without runtime overhead
- Cleaner API with extension methods
- Comprehensive platform testing
Want to expose more Unity internal methods? Check out our guide on extending internals which covers:
- Finding Unity's internal methods
- Understanding IL injection with Cecil
- Adding new exposed methods
- Testing and compatibility
MIT License - See LICENSE file for details
Cosmin Bararu