在运行时获取 Unity Editor-Only 变量(启动画面背景的颜色)
Get Unity Editor-Only Variable (color of splash screen background) at Runtime
为了从 Unity 的启动画面创建动态淡入效果,我试图在运行时获取启动画面背景的颜色。
在编辑器中,颜色可以在 编辑 > 项目设置 > 播放器 > 启动图像 > 背景。
在研究如何获得这种颜色时,我偶然发现
PlayerSettings.SplashScreen.backgroundColor
。但是,PlayerSettings
class 包含在 UnityEditor
命名空间中,无法在运行时访问。
如何在运行时动态获取启动画面背景的颜色?
编辑: 使用 IL 反编译器,我发现 UnityEditor 程序集使用外部方法解决了这个问题。但是,我仍然没有找到从编辑器程序集中获取背景颜色的方法。
public static Color backgroundColor
{
get
{
Color result;
PlayerSettings.SplashScreen.INTERNAL_get_backgroundColor(out result);
return result;
}
// ...
}
private static extern void INTERNAL_get_backgroundColor(out Color value);
Unity 的构建假设您在 运行 时永远不需要这个值,因此您需要采取一些技巧来解决这个问题。为了解决使 editor-only 变量可访问的这个特殊问题,我经常做的事情是编写一个 pre-build 步骤,将任何需要的数据保存到资源文件中,然后可以在 运行 时间内读取.
您唯一需要执行此操作的 API 是 IPreprocessBuild
interface, Resources.Load
, and the MenuItem
属性(MenuItem
是可选的,但会使您的工作流程更轻松)。
首先,实现一个 IPreprocessBuild
(这个进入 "Editor" 目录并引用 UnityEditor
)以将设置保存到资源:
class BackgroundColorSaver : IPreprocessBuild
{
public int callbackOrder { get { return 0; } } // Set this accordingly
public void OnPreprocessBuild(BuildTarget target, string path)
{
SaveBkgColor();
}
[MenuItem("MyMenu/Save Background Splash Color")]
public static void SaveBkgColor()
{
// Save PlayerSettings.SplashScreen.backgroundColor to a
// file somewhere in your "Resources" directory
}
}
这个 class 将在任何时候启动构建时调用 SaveBkgColor()
并且 MenuItem
标签让您可以选择在任何时候调用该函数(使testing/iteration 更容易)。
之后,您需要编写一个 运行 时间脚本,该脚本仅使用 Resources.Load
加载您的资产并将其 it/convert 解析为 Color
对象。
这些是在 运行 时间内使 "editor-only" 资源可用的基础知识。使用 Color
:
的最 straight-forward 方式的一些细节
- 将颜色变量保存为
TextAsset
。
- 加载时使用
Color.ToString()
when saving your Color and ColorUtility.TryParseHtmlString
。
另外请记住,由于您希望这是来自启动画面的混合,因此您需要立即获得此代码 运行(可能在 Awake()
函数中你第一个场景中的一些行为)。
旁注:有更有效的方法 save/load 将颜色作为二进制 blob 而不是原始文本资产,但这是 straight-forward 并且该部分可以很容易地换入和换出保持 IPreprocessBuild
实施不变。
为了从 Unity 的启动画面创建动态淡入效果,我试图在运行时获取启动画面背景的颜色。
在编辑器中,颜色可以在 编辑 > 项目设置 > 播放器 > 启动图像 > 背景。
在研究如何获得这种颜色时,我偶然发现
PlayerSettings.SplashScreen.backgroundColor
。但是,PlayerSettings
class 包含在 UnityEditor
命名空间中,无法在运行时访问。
如何在运行时动态获取启动画面背景的颜色?
编辑: 使用 IL 反编译器,我发现 UnityEditor 程序集使用外部方法解决了这个问题。但是,我仍然没有找到从编辑器程序集中获取背景颜色的方法。
public static Color backgroundColor
{
get
{
Color result;
PlayerSettings.SplashScreen.INTERNAL_get_backgroundColor(out result);
return result;
}
// ...
}
private static extern void INTERNAL_get_backgroundColor(out Color value);
Unity 的构建假设您在 运行 时永远不需要这个值,因此您需要采取一些技巧来解决这个问题。为了解决使 editor-only 变量可访问的这个特殊问题,我经常做的事情是编写一个 pre-build 步骤,将任何需要的数据保存到资源文件中,然后可以在 运行 时间内读取.
您唯一需要执行此操作的 API 是 IPreprocessBuild
interface, Resources.Load
, and the MenuItem
属性(MenuItem
是可选的,但会使您的工作流程更轻松)。
首先,实现一个 IPreprocessBuild
(这个进入 "Editor" 目录并引用 UnityEditor
)以将设置保存到资源:
class BackgroundColorSaver : IPreprocessBuild
{
public int callbackOrder { get { return 0; } } // Set this accordingly
public void OnPreprocessBuild(BuildTarget target, string path)
{
SaveBkgColor();
}
[MenuItem("MyMenu/Save Background Splash Color")]
public static void SaveBkgColor()
{
// Save PlayerSettings.SplashScreen.backgroundColor to a
// file somewhere in your "Resources" directory
}
}
这个 class 将在任何时候启动构建时调用 SaveBkgColor()
并且 MenuItem
标签让您可以选择在任何时候调用该函数(使testing/iteration 更容易)。
之后,您需要编写一个 运行 时间脚本,该脚本仅使用 Resources.Load
加载您的资产并将其 it/convert 解析为 Color
对象。
这些是在 运行 时间内使 "editor-only" 资源可用的基础知识。使用 Color
:
- 将颜色变量保存为
TextAsset
。 - 加载时使用
Color.ToString()
when saving your Color andColorUtility.TryParseHtmlString
。
另外请记住,由于您希望这是来自启动画面的混合,因此您需要立即获得此代码 运行(可能在 Awake()
函数中你第一个场景中的一些行为)。
旁注:有更有效的方法 save/load 将颜色作为二进制 blob 而不是原始文本资产,但这是 straight-forward 并且该部分可以很容易地换入和换出保持 IPreprocessBuild
实施不变。