Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

namespace Game.Preloading
{
Expand All @@ -24,6 +25,8 @@ public sealed class Preloading : MonoBehaviour

[SerializeField] NetworkManager m_NetworkManager;

AsyncOperationHandle<GameObject> m_AsyncOperationHandle;

async void Start()
{
await PreloadDynamicPlayerPrefab();
Expand All @@ -35,9 +38,8 @@ async void Start()
async Task PreloadDynamicPlayerPrefab()
{
Debug.Log($"Started to load addressable with GUID: {m_DynamicPrefabReference.AssetGUID}");
var op = Addressables.LoadAssetAsync<GameObject>(m_DynamicPrefabReference);
var prefab = await op.Task;
Addressables.Release(op);
m_AsyncOperationHandle = Addressables.LoadAssetAsync<GameObject>(m_DynamicPrefabReference);
var prefab = await m_AsyncOperationHandle.Task;

//it's important to actually add the player prefab to the list of network prefabs - it doesn't happen
//automatically
Expand All @@ -51,8 +53,13 @@ async Task PreloadDynamicPlayerPrefab()
// prefabs to NetworkManager's NetworkPrefabs list pre-connection time guarantees that all players will have
// matching NetworkConfigs. This is why NetworkManager.ForceSamePrefabs is set to true. We let Netcode for
// GameObjects validate the matching NetworkConfigs between clients and the server. If this is set to false
// on the server, clients may join with a mismatching NetworkPrefabs list from the server.
// on the server, clients may join with a mismatching NetworkPrefabs list from the server.
m_NetworkManager.NetworkConfig.ForceSamePrefabs = true;
}

void OnDestroy()
{
Addressables.Release(m_AsyncOperationHandle);
}
}
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
- Upgraded to Unity 2022.3.27f1 (#176)
- com.unity.transport upgraded to v1.4.1

#### Fixed
- Releasing an Addressables handle on OnDestroy inside Preloading scene to prevent releasing loaded dynamic prefab from memory (#179)

### Invaders

#### Changed
Expand Down