Version: 2020.2
LanguageEnglish
  • C#

PlayerSettings.SetPreloadedAssets

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public static void SetPreloadedAssets(Object[] assets);

Description

Assigns the assets that will be loaded at start up in the player and be kept alive until the player terminates.

This example shows how a ScriptableObject can be used to store data that can be accessed at any time throughout the lifetime of the player.

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); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961