OpenSesameCompilerForUnity 使用教程
1、项目介绍
OpenSesameCompilerForUnity 是一个基于 Roslyn 的自定义编译器和编辑器扩展,用于在 Unity 中访问内部和私有成员。通过这个工具,你可以在不使用反射的情况下访问其他程序集的内部和私有成员。简而言之,它允许你像说“芝麻开门”一样轻松访问这些受限的成员。
2、项目快速启动
安装
你可以通过 UPM(Unity Package Manager)或直接从 GitHub 安装 OpenSesameCompilerForUnity。
通过 UPM 安装
在 Packages/manifest.json
文件中添加以下依赖:
"dependencies": {
"com.coffee.open-sesame-compiler": "https://github.com/mob-sakai/OpenSesameCompilerForUnity.git#1.0.0"
}
通过 GitHub 安装
克隆仓库到你的项目中:
git clone -b upm https://github.com/mob-sakai/OpenSesameCompilerForUnity.git
使用
- 在 Unity 项目视图中选择一个
AssemblyDefinitionFile
。 - 在检查器视图中配置设置:
- 启用
Open Sesame
:使用 OpenSesameCompiler 代替默认的 csc 编译器。 - 设置修改符号:在编译时添加或移除分号分隔的符号。
- 启用
示例代码:
// 启用 Open Sesame 后,可以直接访问内部或私有成员
EditorApplication.CallDelayed(() => Debug.Log("delayed"), 1);
3、应用案例和最佳实践
应用案例
假设你有一个包含内部方法的库,通常情况下,这些方法对外部是不可见的。使用 OpenSesameCompilerForUnity,你可以轻松访问这些方法。
// 假设 UnityEditor 中的 EditorApplication.CallDelayed 是内部静态方法
EditorApplication.CallDelayed(() => Debug.Log("delayed"), 1);
最佳实践
- 谨慎使用:虽然 OpenSesameCompilerForUnity 提供了强大的功能,但应谨慎使用,避免滥用内部和私有访问权限。
- 版本管理:确保你使用的版本是最新的,以获得最新的功能和修复。
4、典型生态项目
asmdefScriptingDefines
asmdefScriptingDefines 是一个与 OpenSesameCompilerForUnity 配合使用的工具,用于管理 Assembly Definition 文件的脚本定义。
GitHub 链接:asmdefScriptingDefines
Csc-Manager
Csc-Manager 是一个用于管理 C# 编译器的工具,可以与 OpenSesameCompilerForUnity 一起使用,提供更高级的编译器设置。
GitHub 链接:Csc-Manager
通过这些工具的配合使用,可以进一步增强 Unity 项目的开发效率和灵活性。