I want to store the Textures (and Sprites) in my project externally then apply them when the Apps running. I tested with downloading each from a URL and applying them and that worked in a simple capacity but it was too much for the device when I tried to load all the Textures from their own unique URL.
So I thought the next logical step would be to bundle them together load them from an AssetBundle. So I exported my bundle of Textures then applied the following script to each object that requires a Texture. The url is my Desktop (where the AssetBundle sits) and the assetName is assigned in the Inspector (as each is different):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class LoadTex : MonoBehaviour {
private string url = "MY_DESKTOP_URL";
public string assetName;
IEnumerator Start()
{
WWW www = new WWW(url);
yield return (www);
AssetBundle assetBundle = www.assetBundle;
Texture tex = assetBundle.LoadAsset (assetName);
Renderer renderer = GetComponent ();
renderer.material.mainTexture = tex;
}
}
I don't get an error message in the Editor, but when I go into Play Mode I get the following errors:
You are trying to load data from a www stream which had the following error when downloading.
Error
UnityEngine.WWW:get_assetBundle()
c__Iterator0:MoveNext() (at Assets/Scripts/LoadTex.cs:18)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
NullReferenceException: Object reference not set to an instance of an object
LoadTex+c__Iterator0.MoveNext () (at Assets/Scripts/LoadTex.cs:22)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Can anyone help me out with this?
↧