Created
September 18, 2016 00:58
-
-
Save yoanisgil/90720d28411607f75bf7788ac7c008f3 to your computer and use it in GitHub Desktop.
Load Assets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class AssetsBundleLoader : MonoBehaviour { | |
public GameObject prefabButton; | |
public RectTransform parentPanel; | |
private string url = "http://localhost:8080/tents"; | |
private AssetBundle bundle; | |
private Dictionary<string, GameObject> tents; | |
IEnumerator Start () | |
{ | |
WWW www = WWW.LoadFromCacheOrDownload (url, 1); | |
/// Wait for download to complete | |
yield return www; | |
bundle = www.assetBundle; | |
tents = new Dictionary<string, GameObject> (); | |
string[] assetNames = bundle.GetAllAssetNames (); | |
Vector3 position = transform.position; | |
for (int i = 0; i < assetNames.Length; i++) { | |
string []pathElements = assetNames [i].Split ('/'); | |
string assetName = pathElements [pathElements.Length - 1]; | |
GameObject gameObject = (GameObject)Instantiate(prefabButton); | |
gameObject.transform.SetParent(parentPanel, false); | |
if (i == 0) { | |
position = gameObject.transform.position; | |
} else { | |
position = new Vector3 (position.x, position.y + 50, position.z); | |
gameObject.transform.position = position; | |
} | |
Button button = gameObject.GetComponent<Button> (); | |
button.GetComponentInChildren<Text> ().text = assetName; | |
Debug.Log(assetName); | |
string test = string.Copy (assetName); | |
tents[assetName] = bundle.LoadAsset<GameObject> (assetName); | |
button.onClick.AddListener(() => LoadAsset(assetName)); | |
} | |
} | |
void LoadAsset(int assetName) | |
{ | |
Debug.Log(assetName); | |
//Instantiate (tents [assetName]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment