Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by maximus9600 · Dec 15, 2014 at 10:19 AM · arraytexture2d

Loading Textures from AssetBundle

I am currently working on a Vuforia AR Project. I am working on a part where I have an assetbundle containing png textures. There are around 177 textures. I want to download them and make them play in a sequence on a plane. This is my code for the downloading and playing part:

 public class TextureLoadScript : MonoBehaviour
 {
     Texture2D[] tex = new Texture2D[177];
 
     Material GoMaterial;
 
     int frameCounter = 0;
 
     public TrackableBehaviour trackableBehaviour;
 
     public string imageSequenceName="ImageSequenceName_";
 
     public string FolderName="Resources/ImageSequence";
 
     public int numOfFrames;
 
     AssetBundle bundle;
 
     bool playing;
 
     bool isLoading;
 
     void Awake()
     {
         this.GoMaterial = this.renderer.material;
 
     }
     // Use this for initialization
     void Start () 
     {
         if (trackableBehaviour) {
             trackableBehaviour.RegisterTrackableEventHandler (this);
         }
         tex[0] = (Texture2D)bundle.Load(FolderName+imageSequenceName+"00000",typeof(Texture2D));
     }
     // Update is called once per frame
     void Update () 
     {
         if(isLoading)
         {
             StartCoroutine("Download");
         }
         if(playing)
         {
             StartCoroutine("Play",0.0588f);
             GoMaterial.mainTexture = tex[frameCounter];
         }
     }
     IEnumerator Download()
     {
 
         string url = "mteam@mobilesandbox.cloudapp.net/IntroScene_android_android.unity3d";
         WWW download = WWW.LoadFromCacheOrDownload(url,1);
         yield return download;
         if(download.error!=null)
         {
             Debug.LogError(download.error);
             
         }
         bundle=download.assetBundle;
         Debug.Log ("Bundle Loaded Successfully"+bundle.name);
         isLoading = true;
     }

     IEnumerator Play(float delay)
     {
         yield return new WaitForSeconds(delay);
          if(frameCounter<numOfFrames-1)
         {
             ++frameCounter;
 
             tex[frameCounter] = (Texture2D)bundle.Load(FolderName+imageSequenceName+frameCounter.ToString("D5"),typeof(Texture2D));
         } 
         else
         {
             playing = false;
         }
     }

   

During runtime it shows object reference not set to the instance of an object. I can see I am close enough. But I am not able to proceed further. Kindly help me on this

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SkaredCreations · Dec 15, 2014 at 10:23 AM 0
Share

Why are you doing StopCoroutine at the end of the coroutines? Also you're missing to say what is the line raising the NullReference exception, and please edit your post and adjust the text so that the code is correctly displayed, it's a bit unreadable as it is now. And you're also missing the code that you use to call the two functions.

avatar image maximus9600 · Dec 15, 2014 at 10:56 AM 0
Share

Sorry about the alignment. I hope this is fine. I am getting the null exception errors in lines 72 and 34.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SkaredCreations · Dec 15, 2014 at 11:18 AM

This code has some logic errors:

  • in Start you're accessing "bundle" but it isn't initialized yet, since you're initializing it in Download

  • numOfFrames is never assigned, so it'll be ever 0

  • I think bundle.Load accepts only the name of the resource, not a path

Here is how I would do:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TextureLoadScript : MonoBehaviour {
 
     Material GoMaterial;
     public TrackableBehaviour trackableBehaviour;
     public string imageSequenceName="ImageSequenceName_";
     AssetBundle bundle;
     bool playing;
     bool isLoading;
     
     void Start () 
     {
         if (trackableBehaviour) {
             trackableBehaviour.RegisterTrackableEventHandler (this);
         }
         GoMaterial = renderer.material;
         isLoading = true;
         StartCoroutine(Download());
         StartCoroutine(Play(0.0588f));
     }
 
     IEnumerator Download()
     {
         
         string url = "mteam@mobilesandbox.cloudapp.net/IntroScene_android_android.unity3d";
         WWW download = WWW.LoadFromCacheOrDownload(url,1);
         yield return download;
         if (!string.IsNullOrEmpty(download.error))
         {
             Debug.LogError(download.error);
         }
         else
         {
             bundle = download.assetBundle;
             Debug.Log ("Bundle Loaded Successfully" + bundle.name);
         }
         isLoading = false;
     }
     
     IEnumerator Play(float delay)
     {
         while (isLoading)
             yield return null;
         playing = true;
         if (bundle != null)
         {
             List<Texture2D> textures = new List<Texture2D>((Texture2D[])bundle.LoadAll(typeof(Texture2D)));
             for (int frameCounter = 0; frameCounter < textures.Count; ++frameCounter) {
                 Texture2D tex = textures.Find(t => t.name.Equals(imageSequenceName + frameCounter.ToString("D5")));
                 if (tex != null)
                     GoMaterial.mainTexture = tex;
                 yield return new WaitForSeconds(delay);
             }
         }
         playing = false;
     }
 }
 



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maximus9600 · Dec 16, 2014 at 05:11 AM 0
Share

Thanks a lot SkaredCreations! I tried running this code. It says InvalidCastException at line :52 And I have a question? How will the textures be applied everyframe without the update function?

avatar image maximus9600 · Dec 18, 2014 at 03:04 AM 0
Share

I managed to fix it myself. Now its working. Thanks a lot for your help

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Texture 2D Arrays in Parallax Occlusion Mapping 0 Answers

Why is NativeArray always shorter than the number of pixels in the texture? 1 Answer

Texture 2D array to GUI.Window rows iOS 0 Answers

Array of arrays of Textures 2 Answers

How to use texture2d arrays? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges