SimpleBundleBuilder
Code snippet
using UnityEngine;
using UnityEditor;
using System.Collections;
public class SimpleBundleBuilder
{
[MenuItem("Simple Bundles/Build")]
static void BuildBundles()
{
string path = EditorUtility.SaveFolderPanel ("Save Bundle", "", "");
if(path.Length != 0)
{
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
}
}
SimpleLevelLoader
Code snippet
using UnityEngine;
using System.Collections;
public class SimpleLevelLoader : MonoBehaviour
{
[SerializeField] string path;
[SerializeField] string sceneName;
IEnumerator Start()
{
using(WWW www = WWW.LoadFromCacheOrDownload(path, 1))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
Application.LoadLevel(sceneName);
yield return null;
www.assetBundle.Unload(false);
}
}
}
LevelAndManifestLoader
Code snippet
using UnityEngine;
using System.Collections;
public class LevelAndManifestLoader : MonoBehaviour
{
[SerializeField] string path;
[SerializeField] string bundleName;
[SerializeField] string manifestName;
[SerializeField] string sceneName;
AssetBundleManifest manifest;
IEnumerator Start()
{
using(WWW www = new WWW(path + manifestName))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest");
yield return null;
www.assetBundle.Unload(false);
}
using(WWW www = WWW.LoadFromCacheOrDownload(path + bundleName, manifest.GetAssetBundleHash(bundleName)))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
Application.LoadLevel(sceneName);
yield return null;
www.assetBundle.Unload(false);
}
}
}
LevelVariantLoader
Code snippet
using UnityEngine;
using System.Collections;
public class LevelVariantLoader : MonoBehaviour
{
[SerializeField] string path;
[SerializeField] string bundleName;
[SerializeField] string manifestName;
[SerializeField] string sceneName;
[SerializeField] string variant;
AssetBundleManifest manifest;
IEnumerator Start()
{
using(WWW www = new WWW(path + manifestName))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest");
yield return null;
www.assetBundle.Unload(false);
}
AssetBundle bundle;
using(WWW www = WWW.LoadFromCacheOrDownload(path + "uiassets." + variant, manifest.GetAssetBundleHash("uiassets." + variant)))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
bundle = www.assetBundle;
}
using(WWW www = WWW.LoadFromCacheOrDownload(path + bundleName, manifest.GetAssetBundleHash(bundleName)))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield break;
}
Application.LoadLevel(sceneName);
yield return null;
www.assetBundle.Unload(false);
}
}
}
AssetBundleMenu
Code snippet
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
public class AssetBundleMenu
{
[MenuItem("AssetBundles/Clear Cache")]
static void ClearCache()
{
Caching.CleanCache ();
}
[MenuItem("AssetBundles/Build for PC")]
static void TogglePCBuild ()
{
EditorPrefs.SetBool("buildPC", !EditorPrefs.GetBool("buildPC", false));
}
[MenuItem("AssetBundles/Build for PC", true)]
static bool TogglePCBuildValidate ()
{
Menu.SetChecked("AssetBundles/Build for PC", EditorPrefs.GetBool("buildPC", false));
return true;
}
[MenuItem("AssetBundles/Build for OSX")]
static void ToggleOSXBuild ()
{
EditorPrefs.SetBool("buildOSX", !EditorPrefs.GetBool("buildOSX", false));
}
[MenuItem("AssetBundles/Build for OSX", true)]
static bool ToggleOSXBuildValidate ()
{
Menu.SetChecked("AssetBundles/Build for OSX", EditorPrefs.GetBool("buildOSX", false));
return true;
}
[MenuItem("AssetBundles/Build for Web")]
static void ToggleWebBuild ()
{
EditorPrefs.SetBool("buildWeb", !EditorPrefs.GetBool("buildWeb", false));
}
[MenuItem("AssetBundles/Build for Web", true)]
static bool ToggleWebBuildValidate ()
{
Menu.SetChecked("AssetBundles/Build for Web", EditorPrefs.GetBool("buildWeb", false));
return true;
}
[MenuItem("AssetBundles/Build for iOS")]
static void ToggleiOSBuild ()
{
EditorPrefs.SetBool("buildiOS", !EditorPrefs.GetBool("buildiOS", false));
}
[MenuItem("AssetBundles/Build for iOS", true)]
static bool ToggleiOSBuildValidate ()
{
Menu.SetChecked("AssetBundles/Build for iOS", EditorPrefs.GetBool("buildiOS", false));
return true;
}
[MenuItem("AssetBundles/Build for Android")]
static void ToggleAndroidBuild ()
{
EditorPrefs.SetBool("buildAndroid", !EditorPrefs.GetBool("buildAndroid", false));
}
[MenuItem("AssetBundles/Build for Android", true)]
static bool ToggleAndroidBuildValidate ()
{
Menu.SetChecked("AssetBundles/Build for Android", EditorPrefs.GetBool("buildAndroid", false));
return true;
}
[MenuItem("AssetBundles/Build Asset Bundles")]
static void BuildAssetBundles()
{
// Bring up save panel
string path = EditorUtility.SaveFolderPanel ("Save Bundle", "", "");
if (path.Length != 0)
{
if(EditorPrefs.GetBool("buildPC", false))
BuildBundle(path + "/PC", BuildTarget.StandaloneWindows);
if(EditorPrefs.GetBool("buildOSX", false))
BuildBundle(path + "/OSX", BuildTarget.StandaloneOSXUniversal);
if(EditorPrefs.GetBool("buildWeb", false))
BuildBundle(path + "/Web", BuildTarget.WebPlayer);
if(EditorPrefs.GetBool("buildiOS", false))
BuildBundle(path + "/iOS", BuildTarget.iOS);
if(EditorPrefs.GetBool("buildAndroid", false))
BuildBundle(path + "/Android", BuildTarget.Android);
}
}
static void BuildBundle(string path, BuildTarget target)
{
if (!Directory.Exists (path))
Directory.CreateDirectory (path);
BuildPipeline.BuildAssetBundles (path, BuildAssetBundleOptions.None, target);
}
}
AssetManager
Code snippet
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AssetManager : MonoBehaviour
{
public static AssetManager instance;
public string[] bundleVariants;
[SerializeField] string pathToBundles;
Dictionary<string, AssetBundle> bundles;
AssetBundleManifest manifest = null;
string platform;
public bool isReady
{
get
{
return !object.ReferenceEquals(manifest, null);
}
}
void Awake()
{
if (object.ReferenceEquals (instance, null))
{
instance = this;
}
else if (!object.ReferenceEquals (instance, this))
{
Destroy (gameObject);
return;
}
DontDestroyOnLoad (gameObject);
#if UNITY_IOS
platform = "iOS";
#elif UNITY_ANDROID
platform = "Android";
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
platform = "PC";
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
platform = "OSX";
#elif UNITY_WEBPLAYER
platform = "Web";
#else
platform = "error";
Debug.Log("unsupported platform");
#endif
pathToBundles += platform + "/";
bundles = new Dictionary<string, AssetBundle> ();
StartCoroutine ("LoadManifest");
}
IEnumerator LoadManifest ()
{
Debug.Log( "Loading Manifest");
using(WWW www = new WWW(pathToBundles + platform))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
return false;
}
manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest", typeof(AssetBundleManifest));
yield return null;
www.assetBundle.Unload(false);
}
if (!isReady)
Debug.Log ("There was an error loading manifest");
else
Debug.Log ("Manifest loaded successfully");
}
public bool IsBundleLoaded(string bundleName)
{
return bundles.ContainsKey (bundleName);
}
public Object GetAssetFromBundle(string bundleName, string assetName)
{
if (!IsBundleLoaded (bundleName))
return null;
return bundles [bundleName].LoadAsset (assetName);
}
public bool LoadBundle(string bundleName)
{
//See if bundle is already loaded
if (IsBundleLoaded(bundleName))
return true;
//If bundle isn't loaded, load it
StartCoroutine(LoadBundleCoroutine(bundleName));
return false;
}
IEnumerator LoadBundleCoroutine(string bundleName)
{
//Bundle has already been loaded
if (IsBundleLoaded (bundleName))
yield break;
string[] dependencies = manifest.GetAllDependencies (bundleName);
for (int i = 0; i < dependencies.Length; i++)
yield return StartCoroutine (LoadBundleCoroutine (dependencies [i]));
bundleName = RemapVariantName (bundleName);
string url = pathToBundles + bundleName;
Debug.Log ("Beginning to load " + bundleName + " / " + manifest.GetAssetBundleHash(bundleName));
using(WWW www = WWW.LoadFromCacheOrDownload(url, manifest.GetAssetBundleHash(bundleName)))
{
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
return false;
}
bundles.Add(bundleName, www.assetBundle);
}
Debug.Log ("Finished loading " + bundleName);
}
void OnDisable()
{
Debug.Log ("Unloading Bundles");
foreach(KeyValuePair<string, AssetBundle> entry in bundles)
entry.Value.Unload(false);
bundles.Clear ();
Debug.Log ("Bundles Unloaded");
}
string RemapVariantName(string assetBundleName)
{
string[] bundlesWithVariant = manifest.GetAllAssetBundlesWithVariant();
if (System.Array.IndexOf(bundlesWithVariant, assetBundleName) < 0 )
return assetBundleName;
string[] splitBundleName = assetBundleName.Split('.');
string[] candidateBundles = System.Array.FindAll (bundlesWithVariant, element => element.StartsWith(splitBundleName [0]));
int index = -1;
for(int i = 0; i < bundleVariants.Length; i++)
{
index = System.Array.IndexOf(candidateBundles, splitBundleName[0] + "." + bundleVariants[i]);
if(index != -1)
return candidateBundles[index];
}
return assetBundleName;
}
}
LoadScene
Code snippet
using UnityEngine;
using System.Collections;
using System;
public class LoadScene : MonoBehaviour
{
public string sceneBundle;
public string sceneName;
IEnumerator Start ()
{
while (!AssetManager.instance.isReady)
yield return null;
AssetManager.instance.LoadBundle (sceneBundle);
while (!AssetManager.instance.IsBundleLoaded(sceneBundle))
yield return null;
Application.LoadLevel (sceneName);
}
}
Undefined
Topic:
Sort Order:
15
Unity Version:
5.00
Type:
Lesson
Difficulty:
Intermediate