63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Fusion;
|
|
using System.Linq;
|
|
|
|
public class AssetBundleDownloaderAndLoader : MonoBehaviour
|
|
{
|
|
public string assetBundleUrl;
|
|
public GameObject loadedMapPrefab;
|
|
|
|
public async Task<bool> DownloadAndPrepareMap()
|
|
{
|
|
string spaceUrl = EN.instance.spaces.FirstOrDefault()?.assetBundleRoomId?.url;
|
|
if (string.IsNullOrEmpty(spaceUrl))
|
|
{
|
|
Debug.LogError("[Loader] No AssetBundle URL found.");
|
|
return false;
|
|
}
|
|
|
|
assetBundleUrl = "http://194.62.43.230:8000/" + spaceUrl;
|
|
Debug.Log("[Loader] Downloading AssetBundle from: " + assetBundleUrl);
|
|
|
|
using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(assetBundleUrl))
|
|
{
|
|
var operation = uwr.SendWebRequest();
|
|
while (!operation.isDone)
|
|
await Task.Yield();
|
|
|
|
if (uwr.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError("[Loader] AssetBundle download failed: " + uwr.error);
|
|
return false;
|
|
}
|
|
|
|
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
|
|
if (bundle == null)
|
|
{
|
|
Debug.LogError("[Loader] Failed to load AssetBundle.");
|
|
return false;
|
|
}
|
|
|
|
string[] assetNames = bundle.GetAllAssetNames();
|
|
if (assetNames.Length == 0)
|
|
{
|
|
Debug.LogError("[Loader] No assets found in AssetBundle.");
|
|
return false;
|
|
}
|
|
|
|
loadedMapPrefab = bundle.LoadAsset<GameObject>(assetNames[0]);
|
|
if (loadedMapPrefab == null || loadedMapPrefab.GetComponent<NetworkObject>() == null)
|
|
{
|
|
Debug.LogError("[Loader] Prefab missing or missing NetworkObject.");
|
|
return false;
|
|
}
|
|
|
|
Debug.Log("[Loader] AssetBundle downloaded and prefab prepared.");
|
|
bundle.Unload(false); // Keep asset in memory
|
|
return true;
|
|
}
|
|
}
|
|
}
|