This project integrates TorchSharp (PyTorch for .NET) into Unity, enabling machine learning capabilities directly within the Unity engine. It includes TorchSharp version 0.105.2 with libtorch-cpu 2.7.1 and all required dependencies as DLLs imported into the Assets/TorchSharp/Plugins folder.
Note: The Unity integration code in this project is released to the public domain (CC0). No attribution required. See License section below.
Key Features:
- Complete ML training pipeline with the classic Iris dataset example
- Interactive step-by-step training demo
- Training loss curve visualization with SkiaPlot
- Runtime neural network training and inference
- Windows x64 and CPU only (no GPU/CUDA support)
Tested with Unity 6000.3.5f1.
For more information on TorchSharp, visit the TorchSharp GitHub repository. For examples and tutorials, visit the TorchSharp Examples repository.
Unity Integration Code (this project): Released under CC0 1.0 Universal (Public Domain) You are free to use, modify, and distribute this Unity integration without any attribution or restrictions.
TorchSharp Library: Released under MIT License by the .NET Foundation The TorchSharp library and its dependencies retain their original licenses.
Third-Party Components: TorchSharp incorporates PyTorch, Caffe2, and other third-party software. See THIRD-PARTY-NOTICES-TORCHSHARP for complete attribution and license information.
Option A: Download Release (Easiest)
Download the complete project ZIP from the Releases page - no Git LFS required.
Option B: Clone with Git LFS
- Install Git LFS: https://git-lfs.github.com/
- Clone the repository:
git lfs install
git clone https://github.com/LudicWorlds/TorchSharpForUnity.gitOption C: Unity Package Only
If you just want to add TorchSharp to an existing Unity project, download TorchSharpForUnity.unitypackage from the latest Release.
This project contains:
- TorchSharp 0.105.2 - Core deep learning library with libtorch-cpu 2.7.1 binaries
- SkiaSharp 2.88.6 - 2D graphics library with native binaries (Windows x64)
- SkiaPlot - Lightweight plotting library for training loss curve visualization
- Iris Classification Demo - Complete example of training a neural network on the Iris dataset
- All Dependencies - Google.Protobuf, SharpZipLib, System.Memory, and other required libraries
- Example Scripts -
IrisModel.cs(neural network) andIrisTraining.cs(training pipeline) demonstrate the Iris Classification example
For package distribution, a TorchSharpForUnity.unitypackage can be exported using the included PackageExporter script.
- Open the project in Unity 6000.3.5f1 (or later)
- The Code Analysis package should already be installed (check
Packages/manifest.json) - Open the scene: Assets/Scenes/IrisTrainingScene.unity
- Enter Play Mode
Scene: Assets/Scenes/IrisTrainingScene.unity
This interactive demo trains a neural network to classify Iris flowers using the classic Iris dataset.
The demo is adapted from Pytorch Iris Classification Tutorial by John Elder (Codemy.com), and converted to TorchSharp.
How to use:
- Enter Play Mode
- Press SPACEBAR to advance through each training stage:
- Stage 1: Load CSV data (150 samples)
- Stage 2: Split into train/test sets (120/30 split)
- Stage 3: Initialize 3-layer neural network (4→9→9→3)
- Stage 4: Train for 300 epochs with Adam optimizer
- Stage 5: Display training loss curve (using SkiaPlot)
- Stage 6: Evaluate accuracy on test set
- Stage 7: Make predictions on new data
- Watch the Console for detailed output at each stage
Neural Network Architecture:
- Input: 4 features (sepal length, sepal width, petal length, petal width)
- Hidden layers: 9 neurons each with ReLU activation
- Output: 3 classes (Setosa, Versicolor, Virginica)
- Loss: CrossEntropyLoss
- Optimizer: Adam (learning rate: 0.01)
Scene: Assets/TorchSharp/Scenes/TorchSharpScene.unity
A simple scene demonstrating basic tensor operations. Enter Play Mode to see tensor creation and manipulation logged to the Console.
Dependencies:
- TorchSharp 0.105.2
- libtorch-cpu 2.7.1 (Windows x64)
- SkiaSharp 2.88.6 (with native binaries for Windows x64)
- Google.Protobuf 3.21.9
- SharpZipLib 1.4.0
- System.Memory 4.5.5
System Requirements:
- Windows 64-bit
- Unity 6000.3.5f1 (or later)
- CPU only (no GPU/CUDA support)
This project is provided "as is" without warranty of any kind. Use at your own risk. The authors are not responsible for any damage or data loss resulting from the use of this software. Compatibility and performance may vary depending on your system configuration.