You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity运行时如何获取启动画面背景色(编辑器专属变量)?

嘿,这个问题我之前做项目时也遇到过!确实,Unity的PlayerSettings.SplashScreen.backgroundColor属于UnityEditor命名空间,只能在编辑器环境下访问,运行时根本拿不到值。不过有两个实用的办法可以解决这个问题,帮你在运行时获取到启动画面的背景色:

方案一:用ScriptableObject在编辑器自动同步颜色

这个思路是在编辑器里把启动画面的背景色同步到一个可序列化的资源文件(ScriptableObject)里,运行时直接读取这个资源文件就能拿到颜色值,而且编辑器里修改启动画面颜色时会自动同步,非常方便。

步骤1:创建存储颜色的ScriptableObject

先写一个简单的ScriptableObject类,用来保存启动画面的背景色:

using UnityEngine;

// 右键菜单可以快速创建这个资源文件
[CreateAssetMenu(fileName = "SplashScreenSettings", menuName = "Settings/Splash Screen Settings")]
public class SplashScreenSettings : ScriptableObject
{
    public Color backgroundColor;
}

创建好这个脚本后,右键Project窗口 → Settings → Splash Screen Settings,生成一个实例,最好把它放到Resources文件夹下,方便运行时加载。

步骤2:写编辑器脚本自动同步颜色

接下来写一个编辑器脚本,监听PlayerSettings的变化,自动把启动画面的背景色同步到刚才创建的ScriptableObject里:

using UnityEditor;
using UnityEditor.PlayerSettings;
using UnityEngine;

// 编辑器启动时自动初始化
[InitializeOnLoad]
public static class SplashScreenSettingsSync
{
    static SplashScreenSettingsSync()
    {
        // 监听PlayerSettings的变化事件,只要设置改了就同步
        PlayerSettings.projectSettingsChanged += SyncBackgroundColor;
        // 第一次启动编辑器时先同步一次
        SyncBackgroundColor();
    }

    private static void SyncBackgroundColor()
    {
        // 加载我们创建的Settings资源
        var settings = Resources.Load<SplashScreenSettings>("SplashScreenSettings");
        if (settings == null)
        {
            Debug.LogWarning("找不到SplashScreenSettings资源,请先创建!");
            return;
        }

        // 同步启动画面背景色
        settings.backgroundColor = PlayerSettings.SplashScreen.backgroundColor;
        // 标记资源为脏数据,确保修改被保存
        EditorUtility.SetDirty(settings);
        AssetDatabase.SaveAssets();
    }
}

步骤3:运行时读取颜色

在你的运行时脚本里,直接加载这个ScriptableObject就能拿到颜色了:

using UnityEngine;

public class SplashFadeController : MonoBehaviour
{
    private void Start()
    {
        var splashSettings = Resources.Load<SplashScreenSettings>("SplashScreenSettings");
        if (splashSettings != null)
        {
            Color splashBgColor = splashSettings.backgroundColor;
            // 这里就可以用这个颜色做淡入效果了,比如设置相机背景色或者UI面板的初始颜色
            Debug.Log("启动画面背景色:" + splashBgColor);
        }
    }
}
方案二:构建时写入配置文件,运行时读取

如果你的项目不需要频繁修改启动画面颜色,或者想避免编辑器脚本的依赖,可以用构建回调,在构建完成时把颜色写入一个配置文件,运行时读取这个文件。

步骤1:写构建回调脚本

这个脚本会在每次构建完成后,把启动画面背景色写入StreamingAssets目录下的JSON文件:

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.IO;

public class SplashScreenBuildProcessor : IPostprocessBuildWithReport
{
    // 回调顺序,0表示最早执行
    public int callbackOrder => 0;

    public void OnPostprocessBuild(BuildReport report)
    {
        // 获取启动画面背景色
        Color bgColor = PlayerSettings.SplashScreen.backgroundColor;
        // 把Color拆成可序列化的数值
        var colorData = new SplashColorData 
        { 
            r = bgColor.r, 
            g = bgColor.g, 
            b = bgColor.b, 
            a = bgColor.a 
        };
        // 序列化为JSON字符串
        string json = JsonUtility.ToJson(colorData);
        // 写入StreamingAssets目录
        string savePath = Path.Combine(Application.streamingAssetsPath, "SplashScreenColor.json");
        // 确保目录存在
        Directory.CreateDirectory(Path.GetDirectoryName(savePath));
        File.WriteAllText(savePath, json);
    }

    // 内部序列化类,用来存颜色的RGBA值
    [System.Serializable]
    private class SplashColorData
    {
        public float r;
        public float g;
        public float b;
        public float a;
    }
}

步骤2:运行时读取配置文件

在运行时脚本里读取这个JSON文件,还原成Color:

using UnityEngine;
using System.IO;

public class SplashColorLoader : MonoBehaviour
{
    private void Start()
    {
        string filePath = Path.Combine(Application.streamingAssetsPath, "SplashScreenColor.json");
        if (File.Exists(filePath))
        {
            string jsonContent = File.ReadAllText(filePath);
            var colorData = JsonUtility.FromJson<SplashColorData>(jsonContent);
            Color splashBgColor = new Color(colorData.r, colorData.g, colorData.b, colorData.a);
            // 拿到颜色后就可以处理淡入效果了
            Debug.Log("加载到的启动画面背景色:" + splashBgColor);
        }
        else
        {
            Debug.LogWarning("找不到SplashScreenColor.json文件,请先构建项目!");
        }
    }

    [System.Serializable]
    private class SplashColorData
    {
        public float r;
        public float g;
        public float b;
        public float a;
    }
}

小提示

  • 方案一适合开发阶段频繁调整启动画面颜色的场景,修改后立刻同步,不用重新构建。
  • 方案二更适合发布阶段,颜色在构建时固定,运行时直接读取,没有编辑器依赖。
  • 不管用哪种方案,都要确保资源文件(ScriptableObject或JSON)被正确打包到构建产物里,比如ScriptableObject放在Resources,JSON放在StreamingAssets。

内容的提问来源于stack exchange,提问作者Ian H.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:12:06