A lightweight Unity runtime hot reload system that allows you to modify and reload C# scripts without stopping Play mode. Zero dependencies, ready to use!
- π₯ Runtime Hot Reload: Modify code while the game is running, no restart needed
- π MonoBehaviour Auto-Replace: Intelligently replace components on GameObjects
- πΎ Complete State Preservation: Automatically save and restore all field data during reload
- π― Visual Method Execution: Execute methods directly from the editor, supports multiple parameter types
- π οΈ Zero Dependencies: Uses .NET Framework's built-in CSharpCodeProvider, no third-party libraries needed
- π Drag-and-Drop Interface: Simple and intuitive visual editor
- β‘ Lightweight: No NuGet packages required, keeps your project clean
- Unity 2022.3 or higher
- API Compatibility Level must be set to .NET Framework
- No third-party libraries required!
- Open Unity Package Manager (
Window > Package Manager) - Click the
+button in the top-left corner - Select
Add package from git URL... - Enter the following URL:
https://github.com/sliencio/Unity-CodeDom-HotReload.git - Click
Add
- Clone or download this repository
- In Unity Package Manager, click the
+button - Select
Add package from disk... - Navigate to the cloned repository and select
package.json
Important: In Unity menu:
- Open
Edit > Project Settings > Player - Expand
Other Settings - Set
Api Compatibility Levelto.NET Framework
That's it! No third-party dependency packages to install. .NET Framework includes built-in System.CodeDom.dll support.
Menu: Tools > C# Hot Reload Window
- Drag MonoScript or GameObject to the window
- Scripts are automatically added to the hot reload list
- Optional: Configure methods to execute
- Enter Play Mode
- Modify Code (add methods, change logic, etc.)
- Click "π Recompile and Reload"
- Takes Effect Immediately!
The system will automatically:
- β Compile new code
- β Replace components on GameObjects
- β Preserve all field data
- β Make new methods immediately available
- Click "+ Add Method Call" in script configuration
- Enter method name
- Add parameters (supports: String, Int, Float, Bool, Vector3)
- Click "βΆ Execute"
Hot-reloaded code cannot use breakpoints!
Reasons:
- Debugger is attached to the original assembly
- Dynamically compiled code has no debug symbols
- Source code to runtime code mapping is lost
Solutions:
- When debugging is needed: Stop Play mode β Modify code β Run again
- For rapid iteration: Use hot reload
This system uses .NET Framework's built-in CSharpCodeProvider, supporting:
- β All features of C# 7.3 and below
- β Standard class, method, and property definitions
- β Common features like LINQ, generics, delegates
β οΈ Does not support C# 8.0+ new features (like init accessors, record types, switch expressions, etc.)
For most Unity development scenarios, this is completely sufficient!
Due to Unity's MonoBehaviour architecture limitations, hot reloading MonoBehaviour components encounters the following problems:
- β Newly added methods may not be callable
- β Component references are frequently lost
- β GameObject references may become invalid after serialization
- β Complex serialized data cannot be fully restored
- β Component replacement is unstable
π‘ Strong Recommendations:
- β Use traditional workflow for MonoBehaviour (Stop Play mode β Modify code β Run again)
- β Hot reload system focuses on pure C# classes, stable and reliable
- β Extract core logic into pure C# classes, MonoBehaviour only for simple calls
// β
Perfect support! Hot reload works stable and reliable
public class GameLogic
{
public int score;
public void AddScore(int points)
{
score += points;
Debug.Log($"Score: {score}");
}
// Can add new methods anytime, takes effect immediately!
public void ResetScore()
{
score = 0;
}
}Applicable Scenarios:
- β Game logic classes
- β Data management classes
- β Utility and helper classes
- β Algorithm and calculation classes
- β Rapid prototype validation
- β Iterative development
// β οΈ Not recommended for hot reload! Use traditional workflow
public class Player : MonoBehaviour
{
// MonoBehaviour hot reload is unstable
// Recommendation: Stop Play β Modify code β Run again
}Reasons:
- β Newly added methods may not be callable
- β Component references are frequently lost
- β Serialized data may be corrupted
- Breakpoint debugging needed
- Complex bug investigation
- Performance profiling
- Production builds
- MonoBehaviour components
Uses .NET Framework's built-in System.CodeDom.Compiler.CSharpCodeProvider:
- β No third-party dependencies
- β Unity built-in support
- β Stable and reliable
- β Zero configuration
The system automatically configures the following compilation parameters:
compilerParameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true,
IncludeDebugInformation = false
};The system automatically adds all necessary assembly references:
- .NET Framework core libraries
- Unity engine assemblies
- All loaded assemblies in the project
- Breakpoints unavailable (design limitation)
- MonoBehaviour hot reload is unstable (Unity architecture limitation, not recommended)
- C# 8.0+ features not supported (compiler limitation)
About MonoBehaviour: Due to the complexity of Unity's MonoBehaviour component system, hot reloading MonoBehaviour encounters various issues. We strongly recommend:
- Use traditional workflow for MonoBehaviour
- Extract core logic into pure C# classes
- Hot reload system focuses on rapid iteration of pure C# classes
Pull Requests are welcome!
- Fork this repository
- Create feature branch:
git checkout -b feature/AmazingFeature - Commit changes:
git commit -m 'Add some AmazingFeature' - Push branch:
git push origin feature/AmazingFeature - Submit Pull Request
This project is licensed under the MIT License.
This means you can:
- β Commercial use
- β Modify code
- β Distribute
- β Private use
See LICENSE file for details.
- Microsoft .NET Framework - Providing powerful CodeDom compiler
- Unity Technologies - Excellent game engine
- π§ Submit Issue: GitHub Issues
- π¬ Discussions: GitHub Discussions
- Basic hot reload functionality
- MonoBehaviour component replacement
- Visual method execution
- Support more parameter types
- Improve error messages
- Add performance monitoring
- Support Assembly Definition
- Compilation time: Usually 1-2 seconds (depends on code complexity)
- Memory usage: About 1-3MB per hot-reloaded assembly
- Recommendation: Don't include too many scripts in one hot reload
// β
β
β
Highly Recommended: Pure C# class - Perfect hot reload support
public class GameLogic
{
private int score;
public void UpdateScore(int points)
{
score += points;
}
// Can add new methods anytime, hot reload takes effect immediately!
public int GetScore() => score;
}
// βββ Not recommended for hot reload: MonoBehaviour - Use traditional workflow
public class Player : MonoBehaviour
{
private GameLogic gameLogic = new GameLogic(); // Use pure C# class
void Update()
{
// MonoBehaviour only for simple calls
if (Input.GetKeyDown(KeyCode.Space))
{
gameLogic.UpdateScore(10); // Core logic in pure C# class
}
}
}Architecture Recommendations:
- β Extract game logic into pure C# classes
- β MonoBehaviour only for Unity lifecycle and simple calls
- β Hot reload focuses on rapid iteration of pure C# classes
- β Don't hot reload MonoBehaviour components
// β
Simple types are automatically saved and restored
public int health;
public string playerName;
// β οΈ Reference types may be lost
public GameObject target; // May be lost
public Player otherPlayer; // May be lost// β
Recommended: Stateless methods
public int Calculate(int a, int b)
{
return a + b;
}
// β
Recommended: Explicit parameters
public void SetPosition(float x, float y, float z) { }// β
Supported (C# 7.3 and below)
public string Name { get; set; }
if (obj is Type t) { ... }
var result = (a > b) ? a : b;
// β Not supported (C# 8.0+)
public string Name { get; init; } // init accessor
var result = obj switch { ... }; // switch expression
record Person(string Name); // record typeSee CodeDomExample.cs for detailed usage examples.
- π Switched to CodeDom implementation
- β¨ Removed all third-party dependencies
- β¨ Zero configuration, ready to use
- β¨ More lightweight, more stable
- π Initial release
- β¨ Runtime hot reload support
- β¨ MonoBehaviour auto-replacement
- β¨ Visual method execution interface
Star β this project if it helps you!
