using UnityEngine; using UnityEngine.Networking; using System; using System.Collections; using System.Collections.Generic; using System.IO; [Serializable] public class AssetList { public List planePacks; } public class PlaneManager : MonoBehaviour { [SerializeField] string m_S3BucketUrl; [SerializeField] string m_assetlist; static int DEFAULT_PLANEID = 0; static string DEFAULT_BUNDLE = "baseplanepack"; int m_currentPlaneId = DEFAULT_PLANEID; string m_currentPlaneBundle = DEFAULT_BUNDLE; Dictionary m_prefabdata = new Dictionary(); void Awake() { LoadBundle(DEFAULT_BUNDLE); } void Start() { StartCoroutine(RetrieveAssetBundles()); } public int GetCurrentPlaneId() { return m_currentPlaneId; } public void SetCurrentPlaneId(string bundleName, int id) { m_currentPlaneId = id; m_currentPlaneBundle = bundleName; } public Dictionary GetPrefabData() { return m_prefabdata; } public TappyPlayer InstantiatePlayer() { AssetBundle bundle = m_prefabdata[m_currentPlaneBundle].Bundle; string planeFile = GetPlanePrefab(); GameObject prefab = bundle.LoadAsset(planeFile); if(prefab == null) { Debug.LogError("The bundle " + m_currentPlaneBundle + " does not contain " + planeFile); return null; } return GameObject.Instantiate(prefab.GetComponent()); } string GetPlanePrefab() { PlayerPrefabItemList prefabData = m_prefabdata[m_currentPlaneBundle]; for(int i = 0; i < prefabData.playerPrefabItems.Count; ++i) { if(prefabData.playerPrefabItems[i].id == m_currentPlaneId) { return prefabData.playerPrefabItems[i].prefabFile; } } return null; } void LoadBundle(string bundleName) { AssetBundle bndl = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, bundleName)); if(bndl == null) { Debug.Log("Failed to load AssetBundle: " + bundleName); return; } bndl.LoadAllAssets(); PlayerPrefabItemList list = bndl.LoadAsset("PlaneList"); list.Bundle = bndl; m_prefabdata[bundleName] = list; } IEnumerator RetrieveAssetBundles() { //verify the bucket and asset list have been specified if(string.IsNullOrEmpty(m_S3BucketUrl) || string.IsNullOrEmpty(m_assetlist)) { Debug.LogWarning("S3 data not specified, new planes will not be downloaded from the cloud."); yield break; } //retrieve the asset list string url = m_S3BucketUrl + m_assetlist; UnityWebRequest wrq = UnityWebRequest.Get(url); yield return wrq.Send(); if(wrq.isError) { Debug.LogError(wrq.error); yield break; } //download the bundles AssetList list = JsonUtility.FromJson(wrq.downloadHandler.text); for(int i=0; i("PlaneList"); itemList.Bundle = bundle; m_prefabdata[bundleName] = itemList; } } }