Unity3D Support
SolarSharp is designed to work seamlessly with Unity3D:- Full compatibility with Unity3D runtime
- Runs on Ahead-of-time platforms like iOS
- Runs on IL2CPP converted code
- No external dependencies
- Easy and performant interop with Unity objects
Setting Up SolarSharp in Unity
Basic Installation
- Add the SolarSharp assembly to your Unity project
- Ensure you’re using .NET Standard 2.1 or higher
- Import the namespace in your scripts:
Basic Usage
Here’s a simple example of using SolarSharp in Unity:IL2CPP Configuration
What is IL2CPP?
IL2CPP is Unity’s ahead-of-time (AOT) compilation technology that converts .NET code to C++ for better performance and broader platform support.Platform Detection
SolarSharp automatically detects when running on IL2CPP:InteropAccessMode for IL2CPP
When using IL2CPP, you may need to configure the interop access mode:Loading Scripts from Unity Assets
SolarSharp provides a special script loader for Unity assets:Asset Organization
- Create a
Resourcesfolder in your Unity project - Create a subfolder for your Lua scripts (e.g.,
LuaScripts) - Add your Lua scripts as
.txtfiles - The loader will automatically find and load them
Coroutines Integration
SolarSharp coroutines can be converted to Unity coroutines:Module Configuration for Unity
Some modules may not be available or needed in Unity:Best Practices
Performance
- Cache
Scriptinstances rather than creating new ones frequently - Reuse
LuaFunctionreferences for frequently called functions - Consider using
Script.Call()instead of dynamic invocation
Memory Management
Error Handling
Always wrap Lua calls in try-catch blocks:Platform-Specific Considerations
iOS / IL2CPP
- Ensure all Unity types you want to expose to Lua are properly registered
- Use
InteropAccessMode.Reflectionfor better compatibility - Test thoroughly on the target platform
WebGL
- SolarSharp works on WebGL builds
- File system access is limited
- Use
UnityAssetsScriptLoaderfor script loading
Debugging in Unity
Enable debug output:Next Steps
- Learn about Error Handling patterns
- Explore the API Reference for more details
- Check out REPL Usage for interactive development