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
6
Question by gnp89 · Jan 14, 2014 at 12:50 AM · spritematerialtexture2druntimespriterenderer

Change SpriteRenderers Texture2D in runtime

I created a character using unity 2d tools. I sliced a Texture2D to create the sprites. Now I want him to change skins during runtime. So I have two Texture2D with differences in its clothes but the same UV coords. I thought that changing the spriteRenderers material _MainTex would do it, but it keeps using the original Sprites.

 SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
 if(use skin 1){
     skin = texture2D_1;
 }else{
     skin = texture2D_2;
 }
 for (int i = 0; i < renderers.Length; i++) {
     renderers[i].material.SetTexture("_MainTex",skin);
 }

I also tried renderers[i].material.mainTexture = skin; And I tried with sharedMaterial. I would like to change the Texture2d from where Sprites are taken, in runtime of course. How can I do this?

Comment
Add comment · Show 7
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 ziv03 · Jan 14, 2014 at 01:01 AM 0
Share

try SpriteRenderer.sprite

http://docs.unity3d.com/Documentation/ScriptReference/SpriteRenderer-sprite.html

avatar image gnp89 · Jan 14, 2014 at 01:06 AM 0
Share

thats not what I want, I want the sprite renderers to use the UV coords defined in the import settings, but change the Texture2D they use.

avatar image gnp89 · Jan 14, 2014 at 01:39 AM 0
Share

I think thats not the way its done to change the skin of a 3d model. Besides, with this approach I could easily add new skins, without major changes nor editing the textures I already have. It would be so easy if I could change the texture in the material and reuse the UV coords in each quad...

avatar image gnp89 · Jan 14, 2014 at 01:57 AM 0
Share

I'm using a 2D character and I'm using SpriteRenderers and Sprites. But it works in the exact same way as 3d because the SpriteRenderer renders a quad, and the quad has UV coords.

avatar image CarterG81 · Feb 11, 2014 at 03:01 AM 1
Share

Gnp89, please just ignore Invertex. He obviously does not understand what you're talking about. He should just be ignored. It was an interesting read, as any outside person can clearly see Invertex was wrong from start to finish, and had no idea what you were trying to do. He didn't even seem to understand anything you said, at any time you said it.

If he didn't insist so much that he was right, and didn't have some sort of odd view that he HAS to be right, then he could have understood. Ins$$anonymous$$d, he chose to ignore everything you said, never attempted communication, and just wanted to rant on a soap box. I see that attitude all the time. Humility is not common on the internet.

Show more comments

1 Reply

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

Answer by gnp89 · Jan 14, 2014 at 01:40 PM

Found the answer reading the Sprites/Default shader. The _MainTex property has the [PerRendererData] attribute which means the texture is provided by the renderers MaterialPropertyBlock and not by the material. So instead of setting the materials texture, I had to set a new property block to the renderer like this:

 SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
 if(use skin 1){
     skin = texture2D_1;
 }else{
     skin = texture2D_2;
 }
 for (int i = 0; i < renderers.Length; i++) {
     MaterialPropertyBlock block = new MaterialPropertyBlock();
     block.AddTexture("_MainTex",skin);
     renderers[i].SetPropertyBlock(block);
 }

Now it works :D

alt text alt text


screen shot 2014-01-14 at 11.36.30 am.png (65.0 kB)
screen shot 2014-01-14 at 11.38.47 am.png (78.1 kB)
Comment
Add comment · Show 6 · 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 v2-Ton-Studios · Mar 27, 2014 at 07:08 AM 0
Share

Ingenious solution, sadly it only kind of worked for me :|.

Basically, it swaps the sprites on load, for the visible sprites, but, when I move the character (and as a result) transition between animator states or swap to new poses (e.g. left, right, forward, backward) the old "original" sprites are loaded. I hacked for a while and came up with a solution that does seem to work. Feel free to use it.

http://2tonstudios.com/ideas.htm#Skins2DSprites

Cheers!

avatar image v2-Ton-Studios · Mar 28, 2014 at 12:05 AM 0
Share

$$anonymous$$y suggested solution of course doesn't work because AssetDatabase is part of UnityEditor, which not available in any build outside the editor... :|.

avatar image v2-Ton-Studios · Mar 28, 2014 at 03:23 PM 1
Share

Okay, hacked a bit more. Came up with a true solution. For commentary see my website http://2tonstudios.com/ideas.htm

 // new stuff
 using System;
 
 // standard
 using UnityEngine;
 using System.Collections;
 
 public class SkinController : $$anonymous$$onoBehaviour 
 {
     public string activeSkin;
 
     // Use this for initialization
     void Awake () 
     {
         // default loaded sprites
         SpriteRenderer[] loadedRenderers = GetComponentsInChildren <SpriteRenderer>(true);
 
         Texture2D atlas = Resources.Load (activeSkin, typeof(Texture2D)) as Texture2D;
 
         for (int i = 0; i < loadedRenderers.Length; i++)
         {
             try
             {
                 // replace the current sprite with the desired sprite, but using the loaded sprite as a cut out reference via 'rect'
                 loadedRenderers[i].sprite = Sprite.Create (atlas, loadedRenderers[i].sprite.rect, new Vector2 (0.5f, 0.5f));
 
                 // update name, main texture and shader, these all seem to be required... even thou you'd think it already has a shader :|
                 loadedRenderers[i].sprite.name = loadedRenderers[i].name + "_sprite";
                 loadedRenderers[i].material.mainTexture = atlas as Texture;
                 loadedRenderers[i].material.shader = Shader.Find ("Sprites/Default");
             }
             catch (Exception e) {}
         }
     }
 }
avatar image gnp89 · Mar 28, 2014 at 05:54 PM 0
Share

Oh I hadn't noticed that because my characters just had a walking animation and there were no transitions. Perfect!

avatar image smoggach · Sep 03, 2014 at 06:04 PM 0
Share

Hey thanks for this answer, spent way too long searching for it.You can avoid having to create a new sprite every time by creating it with a Sprite$$anonymous$$eshType.FullRect.

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Can I create a sprite at runtime? 3 Answers

Change sprite's color without making an extra drawcall 0 Answers

Using Texture2D created at runtime to create a Sprite decreases Sprite quality. (Android) 1 Answer

Sprite resized to whole screen 3 Answers

How to get Sprite color palette 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