Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 medinaline · Dec 14, 2019 at 06:36 PM · spritetexture2dspritesspritesheetrect

"Could not create sprite" exception - copying and manipulating texture from SpriteAtlas

I have a bunch of 8bit car sprites, with black outlines and white paint. I am using the method called CopyTexture2D() from post #8 in this thread to change the color of the car to a random RGB value, generating a bunch of traffic.

That solution works brilliantly when I have a folder full of individual sprites. However, when pulling a sprite from a SpriteAtlas (and also when loading sprites directly from a multiple sprite sheet into Sprite[]) it errors. The following code:

 Sprite.Create(carTexture, carSprite.rect, new Vector2(0f, 1f));

returns the following error:

 ArgumentException: Could not create sprite (0.000000, 200.000000, 101.000000, 50.000000) from a 101x50 texture.

0, 200 is the position of the selected sprite within the sprite atlas. I can avoid the error by changing it to -

 Sprite.Create(carTexture, new Rect(0f, 0f, 101f, 50f), new Vector2(0f, 1f));

  • but that pulls the sprite from position 0, 0 in the sprite atlas, rather than the sprite I originally loaded.

The full code (related to this problem, anyway) is-

 public Transform GenerateCar(Vector2 position, Quaternion rotation)
 {
     string spriteName = "8bit-Car-Full-Sprite-Sheet_" + carNo.ToString();
     carNo++;
     if (carNo > 49)
     {
         carNo = 0;
     }
     Sprite carSprite = sa.GetSprite(spriteName);
     Transform car = Instantiate(prefab, position, rotation);
     
     // here is the call (CopyTexture2D() from the referenced post, here renamed to PaintCar())
     Texture2D carTexture = PaintCar(carSprite.texture);
     
     // here is the error
     Sprite sprite = Sprite.Create(carTexture, carSprite.rect, new Vector2(0f, 1f));
     
     car.GetComponent<SpriteRenderer>().sprite = sprite;
     return car;
 }

-and-

 private Texture2D PaintCar(Texture2D copy)
 {
     // pick a random RGB color
     byte colorR = (byte)UnityEngine.Random.Range(0, 255);
     byte colorG = (byte)UnityEngine.Random.Range(0, 255);
     byte colorB = (byte)UnityEngine.Random.Range(0, 255);
     Texture2D texture = new Texture2D(101, 50, TextureFormat.ARGB32, false);
     texture.filterMode = FilterMode.Point;
     texture.wrapMode = TextureWrapMode.Clamp;
     // loop through each row of pixels
     int y = 0;
     while (y < texture.height)
     {
         // loop through each pixel in the row
         int x = 0;
         while (x < texture.width)
         {
             if (copy.GetPixel(x, y) == Color.white )
             {
                 texture.SetPixel(x, y, new Color32(colorR, colorG, colorB, 255));
             }
             else
             {
                 texture.SetPixel(x, y, copy.GetPixel(x, y));
             }
             x++;
         }
         y++;
     }
     texture.Apply();
     return texture;
 }

Hopefully, that's enough info to help get to the bottom of this. Let me know if I am missing something you need. Thanks in advance!

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

1 Reply

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

Answer by medinaline · Dec 15, 2019 at 08:01 PM

After giving up and importing individual sprites, I found that I had to delete the now unused SpriteAtlas in the Sprites folder. The SpriteAtlas was still being used behind the scenes when referencing the sprite I had manually loaded with Resources.Load("Sprites/Car01"). The Texture2D was later being swapped with the sprite in that position in the SpriteAtlas.

Lessen learned, I guess.

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

121 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 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 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 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 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 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

2D sprite renders a small part of another one with "Sprite Mode : Multiple" 0 Answers

iOS Sprites in PVRTC? 1 Answer

Is it possible to animate a sprite on a 3d mesh? 0 Answers

add animation frames to existing spritesheet with animations? 0 Answers

Texture2D.LoadImage color/colour problems 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