• 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 /
avatar image
0
Question by Rustam-Ganeyev · Oct 17, 2011 at 12:10 PM · assetbundleresources.load

Loading assetbundle from resources

The problem is following: I have an assetbundle named "imgs.unity3d" in resources folder. But the following code isn't working:

 Object tmp = Resources.Load ("imgs"); //my assetbundle
 Debug.Log (tmp == null);

Debug returns me true, which means it didn't load. Is it allowed to load assetbundle from resources?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Graham-Dunnett · Oct 17, 2011 at 08:13 PM

An assetbundle is not an object. The Resouces folder can store any asset, just not asset bundles.

Comment
Add comment · Show 3 · 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 Rustam-Ganeyev · Oct 18, 2011 at 10:46 AM 0
Share

ok, but how to use my assetbundle in project? i need asynchronous texture loading from resources.

avatar image hypergotcha · Dec 05, 2011 at 09:32 AM 0
Share

It doesn't seem like you answered the question. I too would like to know how to load an asset bundle from The Resources folder.

avatar image Rustam-Ganeyev · Dec 05, 2011 at 10:02 AM 0
Share

the answer is no, u can't load assetbundles from resources. and this is logically correct - asset bundles are designed for loading from web.

avatar image
3

Answer by Afonso · Apr 17, 2012 at 12:12 PM

This is something that has puzzled me too, since if you expose a AssetBundle variable it seems like you can drag stuff to it but it doesn't really work.

However, there is an ugly workaround.

It works for dragging references to an AssetBundle, so it MAY also work for loading from the Resources folder.

When you create your AssetBundle, save it as a .txt file. Then load it as a TextAsset, and create the AssetBundle with AssetBundle.CreateFromMemory( textAssetRef.bytes ).,

Comment
Add comment · Show 7 · 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 ellioman · Jun 29, 2013 at 09:36 AM 0
Share

Great! Worked like a charm!

avatar image jainam · Jan 23, 2014 at 10:38 AM 0
Share

I tried as @Afonso mentioned,

 TextAsset textToBundle = (TextAsset)Resources.Load("$$anonymous$$ultiplayerTrophyAndroid");
         byte[] txtBytes = textToBundle.bytes;
         assetBundle = AssetBundle.CreateFrom$$anonymous$$emory(txtBytes.bytes);

but I get the following error in unity,

error CS1061: Type byte[]' does not contain a definition for bytes' and no extension method bytes' of type byte[]' could be found (are you missing a using directive or an assembly reference?)

error CS1502: The best overloaded method match for UnityEngine.AssetBundle.CreateFrom$$anonymous$$emory(byte[])' has some invalid arguments error CS1503: Argument #1' cannot convert object' expression to type byte[]'

can you please share your code..

avatar image ellioman · Jan 23, 2014 at 09:46 PM 0
Share

@Jainam To get rid of that error you should just remove the ".bytes" in the last line.

Here is the code I made after seeing Afonso's suggestion:

 TextAsset fileAsText = Resources.Load( GetPackageResourcePath( filename ) ) as TextAsset;
 if ( fileAsText != null )
 {
     byte[] bundleData  = fileAsText.bytes.Clone() as byte[];
     AssetBundleCreateRequest bundleRequest = AssetBundle.CreateFrom$$anonymous$$emory( bundleData );
     GameObject main = Instantiate( bundleRequest.assetBundle.mainAsset ) as GameObject; 
 }
avatar image jainam · Jan 24, 2014 at 09:51 AM 0
Share

@ellioman thanks for sharing the code. I don't get errors now, but don't get the asset bundle also. Following is my entire C# script,

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class assetBundleTest : $$anonymous$$onoBehaviour
 {
     TextAsset fileAsText;
 
     void Start ()
     {
         fileAsText  = Resources.Load("/test.txt") as TextAsset;
         if ( fileAsText != null )
         {
             byte[] bundleData  = fileAsText.bytes.Clone() as byte[];
             AssetBundleCreateRequest bundleRequest = AssetBundle.CreateFrom$$anonymous$$emory( bundleData );
             GameObject ob = bundleRequest.assetBundle.mainAsset as GameObject;
             GameObject GO = Instantiate( ob ) as GameObject;
             GO.transform.parent = GameObject.Find("Target").transform;
             GO.SetActive(true);
         }
     }
 }

Any idea where am I making a mistake??

avatar image ellioman · Feb 01, 2014 at 11:26 PM 0
Share

Sorry for the late response. Don't know if you've managed to solve this or not.

If not, have you checked if "ob" and "go" are nulls? I'm thinking that maybe your asset bundle doesn't have a mainAsset. If you don't have one, try loading the prefab you have in the bundle using the AssetBundle.Load() call ins$$anonymous$$d.

For example, try this:

 void Start ()
 {
     fileAsText  = Resources.Load( "/test.txt" ) as TextAsset;
     if ( fileAsText != null )
     {
         byte[] bundleData  = fileAsText.bytes.Clone() as byte[];
         AssetBundleCreateRequest bundleRequest = AssetBundle.CreateFrom$$anonymous$$emory( bundleData );
         
         GameObject targetGameObject = bundleRequest.assetBundle.Load( "Target" ) as GameObject;
         if ( targetGameObject == null )
         {
             Debug.Log( "Nope, this isn't working" );
         }
         else
         {
             Debug.Log( "YAY! Found the gameobject \"" + targetGameObject.name + "\"" );
         }
     }
 }


Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to import the object from server to unity 2 Answers

Sprites randomly display as horizontal lines 2 Answers

Unity 5 Resources.LoadAsync on Android slower than iOS? 0 Answers

How to load a file during runtime? (Resources folder doesn't work) 0 Answers

Copy the file from assets and paste it out side the assets folder 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