- Home /
Apply Texture from AssetBundle
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 <Texture>(assetName);
Renderer renderer = GetComponent<Renderer> ();
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()
<Start>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+<Start>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?
Your answer
![](https://koobas.hobune.stream/wayback/20220612135202im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Images don't load after building. 1 Answer
C# IEnumerator Function Inheritance Dilemma 1 Answer
webplayer crash when use www download assetbundle 0 Answers
Asset Server or WWW fetch texture? 0 Answers
how to unload asset when load new scene? 0 Answers