initial commit

This commit is contained in:
Jo 2025-01-07 02:06:59 +01:00
parent 6715289efe
commit 788c3389af
37645 changed files with 2526849 additions and 80 deletions

View file

@ -0,0 +1,4 @@
{
"displayName": "Custom Build and Playmode Scripts",
"description": "Example custom build and play mode scripts provided. Along with a README discussing how to add them to the Addressables system."
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bf14725523681894dbac667caaa0316b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,20 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f66315e0705fa82438547ab07bbe65e8, type: 3}
m_Name: CustomBuild
m_EditorClassIdentifier: Unity.Addressables.SamplesFolder:UnityEditor.AddressableAssets.Build.DataBuilders:BuildScriptPackedMode
instanceProviderType:
m_AssemblyName: Unity.ResourceManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
m_ClassName: UnityEngine.ResourceManagement.ResourceProviders.InstanceProvider
sceneProviderType:
m_AssemblyName: Unity.ResourceManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
m_ClassName: UnityEngine.ResourceManagement.ResourceProviders.SceneProvider

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3c8ea4c2ec27a354f9b5c8d162aff2c4
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f66315e0705fa82438547ab07bbe65e8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,20 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5796c4ece8a8f054dbd288aa4e18acff, type: 3}
m_Name: CustomPlayMode
m_EditorClassIdentifier:
instanceProviderType:
m_AssemblyName: Unity.ResourceManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
m_ClassName: UnityEngine.ResourceManagement.ResourceProviders.InstanceProvider
sceneProviderType:
m_AssemblyName: Unity.ResourceManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
m_ClassName: UnityEngine.ResourceManagement.ResourceProviders.SceneProvider

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ef55377076f186e4fb78d6f117bb52ac
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,92 @@
#if UNITY_EDITOR
using System;
using System.IO;
using UnityEditor;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Build.DataBuilders;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.Initialization;
/// <summary>
/// Uses data built by BuildScriptPacked class. This script just sets up the correct variables and runs.
/// </summary>
[CreateAssetMenu(fileName = "CustomPlayMode.asset", menuName = "Addressables/Content Builders/Use CustomPlayMode Script")]
public class CustomPlayModeScript : BuildScriptBase
{
/// <inheritdoc />
public override string Name
{
get { return "Use Custom Build (requires built groups)"; }
}
private bool m_DataBuilt;
/// <inheritdoc />
public override void ClearCachedData()
{
m_DataBuilt = false;
}
/// <inheritdoc />
public override bool IsDataBuilt()
{
return m_DataBuilt;
}
/// <inheritdoc />
public override bool CanBuildData<T>()
{
return typeof(T).IsAssignableFrom(typeof(AddressablesPlayModeBuildResult));
}
/// <inheritdoc />
protected override TResult BuildDataImplementation<TResult>(AddressablesDataBuilderInput builderInput)
{
var timer = new System.Diagnostics.Stopwatch();
timer.Start();
var settingsPath = Addressables.BuildPath + "/settings.json";
var buildLogsPath = Addressables.BuildPath + "/buildLogs.json";
if (!File.Exists(settingsPath))
{
IDataBuilderResult resE = new AddressablesPlayModeBuildResult()
{Error = "Player content must be built before entering play mode with packed data. This can be done from the Addressables window in the Build->Build Player Content menu command."};
return (TResult)resE;
}
var rtd = JsonUtility.FromJson<ResourceManagerRuntimeData>(File.ReadAllText(settingsPath));
if (rtd == null)
{
IDataBuilderResult resE = new AddressablesPlayModeBuildResult()
{
Error = string.Format("Unable to load initialization data from path {0}. This can be done from the Addressables window in the Build->Build Player Content menu command.", settingsPath)
};
return (TResult)resE;
}
PackedPlayModeBuildLogs buildLogs = new PackedPlayModeBuildLogs();
BuildTarget dataBuildTarget = BuildTarget.NoTarget;
if (!Enum.TryParse(rtd.BuildTarget, out dataBuildTarget))
{
buildLogs.RuntimeBuildLogs.Add(new PackedPlayModeBuildLogs.RuntimeBuildLog(LogType.Warning,
$"Unable to parse build target from initialization data: '{rtd.BuildTarget}'."));
}
else if (BuildPipeline.GetBuildTargetGroup(dataBuildTarget) != BuildTargetGroup.Standalone)
{
buildLogs.RuntimeBuildLogs.Add(new PackedPlayModeBuildLogs.RuntimeBuildLog(LogType.Warning,
$"Asset bundles built with build target {dataBuildTarget} may not be compatible with running in the Editor."));
}
if (buildLogs.RuntimeBuildLogs.Count > 0)
File.WriteAllText(buildLogsPath, JsonUtility.ToJson(buildLogs));
var runtimeSettingsPath = "{UnityEngine.AddressableAssets.Addressables.RuntimePath}/settings.json";
PlayerPrefs.SetString(Addressables.kAddressablesRuntimeDataPath, runtimeSettingsPath);
PlayerPrefs.SetString(Addressables.kAddressablesRuntimeBuildLogPath, buildLogsPath);
IDataBuilderResult res = new AddressablesPlayModeBuildResult() {OutputPath = settingsPath, Duration = timer.Elapsed.TotalSeconds};
m_DataBuilt = true;
return (TResult)res;
}
}
#endif

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5796c4ece8a8f054dbd288aa4e18acff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
/// <summary>
/// Script added to bootstrap scene loading for the scene built by the <see cref="CustomBuildScript"/> Sample.
/// </summary>
public class LoadSceneForCustomBuild : MonoBehaviour
{
/// <summary>
/// Assigned the address of an AddressableAsset to bootstrap dynamically loading a scene at runtime.
/// </summary>
public string SceneKey;
/// <summary>
/// Start is called before the first frame update.
/// </summary>
void Start()
{
Addressables.LoadSceneAsync(SceneKey);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 69be213651d80fa4e91e08f167430e1a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,14 @@
Custom Play Mode and Build Scripts
A custom play mode script and build script have been provided in this sample. This custom build script creates a build that only includes the currently open scene. A bootstrap scene is automatically created and a script is added that loads the built scene on startup. The custom play mode script works similarly to the Use Existing Build (requires built groups) play mode script already included. The methods added to accomplish this are CreateCurrentSceneOnlyBuildSetup and RevertCurrentSceneSetup on the CustomBuildScript.
For this examples, the build and load paths used by default are [UnityEngine.AddressableAssets.Addressables.BuildPath]/[BuildTarget] and {UnityEngine.AddressableAssets.Addressables.RuntimePath}/[BuildTarget] respectively.
Custom play mode scripts inherit from BuildScriptBase. There are several overridable methods, such as ClearCachedData, IsDataBuilt, and CanBuildData<T>. However, the most noteable method to override is BuildDataImplementation<TResult>. This is the method that is used to setup or build content.
The CanBuildData<T> determines if the customs script shows up in the Build/New Build/ menu or the Play Mode Scripts menu. If the data type being built is AddressablesPlayModeBuildResult, the script shows up in the Play Mode Scripts menu. If the type is AddressablesPlayerBuildResult, the script shows up in the Build/New Build/ menu.
The ScriptableObject of the class has already been created, but the Create menu can be used to make another ScriptableObject if you desire. For this CustomPlayModeScript the create menu path is Addressables/Content Builders/Use CustomPlayMode Script. By default, this creates a CustomPlayMode.asset ScriptableObject. The same goes for the CustomBuildScript.
When creating custom scripts, you need to specify the CreateAssetMenu tag on your class in order to create the ScriptableObject.
Once the ScriptableObject is created, add it to the list of ScriptableObjects called Build and Play Mode Scripts on the AddressableAssetSettings object.

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b092ee038e51419478aad6a1e0bb611c
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: