Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Darkwing984 · Mar 22, 2017 at 07:12 AM · 2dtexture2dimporting assetsspritesheet

Sprite created from texture not displaying

Ok,

So I am basically trying to load sprite sheets textures from the resources folder (as texture 2d) at run time and convert them into sprites.

I have got the textures loading from the resource folder and the sprites being created from the sheet, but when I look in game the sprites are not displaying, even though all the data looks correct. IE the correct sprite is being loaded onto the right object and in the debugger the sprite is linked to the correct texture and the rect dimensions and origin(pivot) are also correct. I also have confirmed that the sorting layers are set fine (I have dynamic sprite layering in my code so have double checked it), but still nothing...

Here are the code sample so hopefully someone can help :)

Here is the code to import the textures from the resource folder and store them in a dictionary:

 resourceLoadStep = "Load textures...";
         foreach (Texture2D texture in Resources.LoadAll<Texture2D>("Sprites"))
         {
             resourceLoadStep = string.Concat("Load texture ", texture.name, "...");            
 
             spriteSheets.Add(texture.name, texture);
 
             DataHandler.control.WriteSystemLog(string.Concat("Spritesheet ", texture.name, "loaded successfully"));
 
             yield return null;
         }
         yield return null;

Here is the code that splits the textures into sprites, it does this by referencing a set of sprite indexes read in earlier from XML:

  resourceLoadStep = "Generate sprites...";
 
         foreach(DataTypes.SpriteData data in spriteDataObjects)
         {
             //we need to create a new sprite from the data object and add it to the sprites
             if (spriteSheets.ContainsKey(data.SheetName))
             {
                 newSprite = Sprite.Create(spriteSheets[data.SheetName],
                     data.SpriteRect,
                     data.Pivot);
 
                 newSprite.name = data.Name;
 
                 switch(data.Type)
                 {
                     case DataTypes.SpriteData.SpriteType.Avatar:
                         if (!avatarSprites.ContainsKey(data.Name))
                         {
                             avatarSprites.Add(data.Name, newSprite);
                         }
                         else
                         {
                             DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Avatar Sprites'"));
                         }
                         break;
                     case DataTypes.SpriteData.SpriteType.Item:
                         if (!itemSprites.ContainsKey(data.Name))
                         {
                             itemSprites.Add(data.Name, newSprite);
                         }
                         else
                         {
                             DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Item Sprites'"));
                         }
                         break;
                     case DataTypes.SpriteData.SpriteType.Planet:
                         if (!planetSprites.ContainsKey(data.Name))
                         {
                             planetSprites.Add(data.Name, newSprite);
                         }
                         else
                         {
                             DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Planet Sprites'"));
                         }
                         break;
                     case DataTypes.SpriteData.SpriteType.Ship:
                         if (!shipSprites.ContainsKey(data.Name))
                         {
                             shipSprites.Add(data.Name, newSprite);
                         }
                         else
                         {
                             DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Ship Sprites'"));
                         }
                         break;
                     case DataTypes.SpriteData.SpriteType.Star:
                         if (!starSprites.ContainsKey(data.Name))
                         {
                             starSprites.Add(data.Name, newSprite);
                         }
                         else
                         {
                             DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Star Sprites'"));
                         }
                         break;
                     case DataTypes.SpriteData.SpriteType.Station:
                         if (!stationSprites.ContainsKey(data.Name))
                         {
                             stationSprites.Add(data.Name, newSprite);
                         }
                         else
                         {
                             DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as a sprite with this name already exists in 'Station Sprites'"));
                         }
                         break;
                     case DataTypes.SpriteData.SpriteType.UI:
                         break;
                     case DataTypes.SpriteData.SpriteType.None:
                         DataHandler.control.WriteSystemLog(string.Concat("Unabel to load sprite '", data.Name, "' as the type is set to 'None'"));
                         break;
                 }
             }
             else
             {
                 DataHandler.control.WriteSystemLog(string.Concat("Unable to load sprite '", data.Name, "' as the Sprite Sheet '", data.SheetName, "' does not exist"));
             }
         }
 
         yield return null;


And finally the code in one of the game objects that references the sprite by name and assigns it to the objects renderer:

  //need to set sprite based on ship id
         //test using the new sprite handler
         thisRenderer.sprite = DataHandler.control.ShipSprites[DataHandler.control.ShipTypes[shipID].SpriteName];


I have looked every where for an answer to this, hopefully someone else has had this issue and can help!

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

0 Replies

· Add your reply
  • Sort: 

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

Sprite Extrude Edges does nothing. 1 Answer

TexturePacker 0 Answers

pixels shown in game view that are not there in the scene 0 Answers

How to change the entire sprite sheet during runtime 1 Answer

Can't create a 2D sprite with Sprite.Create when creating texture in code 1 Answer


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