Version: 2021.1

PlayerSettings.GetPreloadedAssets

切换到手册
public static Object[] GetPreloadedAssets ();

返回

Object[] 要预先加载的资源。

描述

返回将在播放器启动时加载的资源,并保持活动状态直到播放器终止。

此示例显示了如何使用 ScriptableObject 存储可在播放器的整个生命周期内随时访问的数据。

using System.Linq;
using UnityEngine;

// We use this class to store general config data that can be used in the player public class ConfigObject : ScriptableObject { public string text;

public static ConfigObject configInstance;

#if UNITY_EDITOR [UnityEditor.MenuItem("Assets/Create/Config Object")] public static void CreateAsset() { var path = UnityEditor.EditorUtility.SaveFilePanelInProject("Save Config", "config", "asset", string.Empty); if (string.IsNullOrEmpty(path)) return;

var configObject = CreateInstance<ConfigObject>(); UnityEditor.AssetDatabase.CreateAsset(configObject, path);

// Add the config asset to the build var preloadedAssets = UnityEditor.PlayerSettings.GetPreloadedAssets().ToList(); preloadedAssets.Add(configObject); UnityEditor.PlayerSettings.SetPreloadedAssets(preloadedAssets.ToArray()); } #endif

void OnEnable() { configInstance = this; } }
using UnityEngine;

public class UseConfigObject : MonoBehaviour { void OnGUI() { if (ConfigObject.configInstance != null) { GUILayout.Label("Found the config asset. The message was: " + ConfigObject.configInstance.text); } } }