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
1
Question by JasonB · Feb 20, 2014 at 03:22 AM · 2dspritetexture2d

Is it impossible to swap the Texture2D of a Sprite Renderer at runtime?

I have a sprite sheet that has been duplicated numerous times with various alterations to each sheet.

I created an Animator and about half a dozen animations using one sheet. But now it seems that even if I have identical sprite sheets, I can't simply swap the Texture2D at runtime, meaning I need to painstakingly create a separate animation for every single variation to my texture sheets. Sadly, sprite.texture is read-only.

Is there absolutely no way of doing this?

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
1
Best Answer

Answer by JasonB · Feb 20, 2014 at 07:18 AM

I spent all night coming up with this solution.

It's not elegant, but it achieves what I needed.

Since I'm not allowed to change a sprite's source texture at runtime, I decided to change the texture itself at runtime instead. Here's what I did.

I started with some sprite sheets, I'll rename them for this example.

 Monster_Green
 Monster_Blue
 Monster_Red

Whichever one you based your animations off of, rename it to something more generic, and make a copy of it. I renamed the green one, and made a copy of it. You also need to move everything into the Resources folder, and I additionally moved them into a subfolder under Resources called Sprites. You should now have these.

 Monster
 Monster_Green
 Monster_Blue
 Monster_Red

Make sure these files are given read/write permission by changing them from Sprite to Advanced and checking the appropriate box. "Monster" is now going to chameleon the color data from whichever sprite sheet you want (as long as the sizes match). Here's the code for changing our monster sprite into the blue monster.

 Color[] gotcolors = Resources.Load<Texture2D>("Sprites/Monster_Blue").GetPixels();
 Resources.Load<Texture2D>("Sprites/Monster").SetPixels(gotcolors);
 Resources.Load<Texture2D>("Sprites/Monster").Apply();

This code gets all color data from Monster_Blue and stores it in gotcolors. It then reassigns all color data to Monster to look like Monster_Blue. The reason we created Monster generic is this way we don't destroy any of our source sheets.

And so, using this method, you can do as many animations as your heart desires on just one sprite sheet, and then swap sheets to get different characters (as long as the sheets are of identical size and your characters are placed identically, as they are still sliced according to the original sprite sheet).

Here's what this doesn't solve. I only needed this on a character who (thankfully) exists on a unique basis. There's no way to do this to assign different sheets to different objects, since it alters the texture they all use and every instance would look identical (unless that's what you want). So the Unity team still needs to add texture swapping at runtime in an official capacity for sprite sheets, as my solution is hacky and not for everyone.

Hope this helps someone.

Comment
Add comment · Show 1 · 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 DenisM · Apr 20, 2016 at 05:45 PM 0
Share

is there a new method of doing this, but not so hacky? it is 2016...

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

19 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

Related Questions

How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer

My sprite from sprite.create looks pixelated. 1 Answer

Texture2D Sprite 2 Answers

Save generated Texture2D as Texture type Sprite 1 Answer

Texturing custom 2d sprite 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