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 /
avatar image
3
Question by Asse83 · Nov 29, 2013 at 07:41 PM · spriteassetbundletexture2d4.3

Load Unity 4.3 Sprites with AssetBundles

Hey,

I want to load the new 2D Sprites by asset bundle. The problem is that Unity handles and exports Sprites as Texture2D objects.

My question is, how do I get a Sprite out of this loaded Texture2D object?

Comment
Add comment · Show 3
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 Jaro · Mar 20, 2014 at 09:54 AM 0
Share

I'm facing the same problem. Any solution ?

avatar image Jaro · Mar 20, 2014 at 03:22 PM 0
Share

I noticed that when I use BuildPipeline.BuildAssetBundle() Sprite data are correct, but when I use BuildPipeline.BuildAssetBundleExplicitAssetNames() data are missing. Any idea ?

avatar image guykogus · Mar 20, 2014 at 03:45 PM 0
Share

Yeah, don't use the 2D system. Go for 2D Toolkit. I gave up on the in-built 2D system a while ago. Save yourselves the time and hassle.

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by lync · Apr 14, 2014 at 01:48 PM

I've been poking around with this recently and was able to get the sprite back from the asset bundle, but it does appear like it was not well thought through.

I create the asset bundle using the following

 UnityEngine.Object[] assets = new UnityEngine.Object[] { AssetDatabase.LoadAssetAtPath( MySpritePath, typeof(UnityEngine.Object) ) };
 UnityEngine.Object mainAsset = assets[0];
 BuildPipeline.BuildAssetBundle( mainAsset, assets, bundlePath, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, buildTarget );

Then load it up with

 AssetBundle bundle = AssetBundle.CreateFromFile("bundlePath");
 spriteRenderer.sprite = bundle.Load( "MySpriteName", typeof(Sprite) ) as Sprite;

and I get a sprite back with all the right settings. Looking into it further I found that even though during the bundle building step it appears as if only one asset is added (the assets array has length 1), when you load the bundle it actually contains two assets: the Texture2D and the Sprite. This also appears to hold true when you have multiple sprites per texture, it looks like you're only adding one asset, but somewhere along the way they all end up in the bundle.

If you a experimenting and want to see what is actually in the bundle you can do a LoadAll and enumerate the contents

 Object[] assets = bundle.LoadAll();
 foreach ( Object asset in assets ) {
   Debug.Log( "loaded asset " + asset.name + " of type " + asset.GetType()  );
 }
Comment
Add comment · 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
1

Answer by Mark-Davis · Sep 15, 2014 at 11:27 PM

As mentioned above, Sprite.Create(...) should be able to get the job done depending on what your use-case is. The following code snippet worked for me. Hope this helps.

 if (_iconsBundle.Contains(iconName))
 {
     var iconTexture = _iconsBundle.Load(iconName) as Texture2D;
     var iconRect = new Rect(0, 0, iconTexture.width, iconTexture.height);
     var iconSprite = Sprite.Create(iconTexture, iconRect, new Vector2(.5f, .5f));
 
     worldIcon.sprite = iconSprite;
 }
 else
 {
     worldIcon.sprite = defaultIcon;
 }


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

Answer by see · Jan 24, 2015 at 06:47 AM

You can use Resources.LoadAssetAtPath to load Sprite object instead of AssetDatabase.LoadAssetAtPath:

Resources.LoadAssetAtPath(path, typeof(Sprite));

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

Answer by guykogus · Dec 09, 2013 at 02:43 PM

The textures are treated as sprites. E.g. If you have in your code something like

public Sprite mySprite;

You'll be able to drag the sprite texture to fill that spot.

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

Answer by benhumphreys · Mar 13, 2014 at 08:12 AM

As far as I can tell, as of 4.3.4, you cannot.

When Texture2D assets and their sprites are saved to an AssetBundle, the Sprite information is dropped.

If you only have a single sprite within a texture, you could recreate the sprite at runtime with `Sprite.Create`, but obviously this isn't ideal.

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

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

21 People are following this question.

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

Related Questions

Sprite in Asset Bundles returned a Texture2D 3 Answers

Assetbundle for multiple-mode sprite 2 Answers

SetPixel on a sprite texture without changing it globally 1 Answer

Can I generate a sprite for a specific collider? 0 Answers

Convert texture 2d to sprite and save 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