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
0
Question by Razouille · Oct 22, 2014 at 01:01 PM · 2dspriteanimatorcolor

Change dynamicaly a PNG used in a motion

Hi everyone,

I have an enemy with a specific animator and few motions with 2D sprites. I want to have the same enemy with different colors (so duplicated PNGs with other colors), but the only way I see is to duplicate all the motions and set them again and again. So is there a way to switch at runtime the PNGs used (or better the folder), as we can do in HTML5 ? (I can't use a simple tint on the material, cause I don't want to modify the skin color, only the clothes)

Thanks in advance.

PS : sorry for my bad english.

Comment
Add comment · Show 2
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 SarperS · Oct 22, 2014 at 01:37 PM 0
Share

Ins$$anonymous$$d of having a ton of textures for different colors and waste vram like that, you can program a shader which will take a b&w mask to multiply with the tint color. So only the parts you want to color will be colored. Let me know if you need help with this and I can code the shader for you.

avatar image Razouille · Oct 22, 2014 at 01:42 PM 0
Share

Thanks for your help. Sorry I never used mask in Unity, is that a sprite I have to add ? I have many sprites for only 1 enemy, so do I have to make a mask for each sprite ?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SarperS · Oct 22, 2014 at 02:15 PM

It seems I can't attach a unitypackage file here so I've put it up on my dropbox. Please download the file and import it in your project. It contains a shader, 2 sprites and a demo scene. Let me know if you can't figure out how to make it work.

https://dl.dropboxusercontent.com/u/1652836/SpriteMaskedColorTint.unitypackage

Comment
Add comment · Show 5 · 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 Razouille · Oct 22, 2014 at 02:33 PM 0
Share

It works well thank you for the tip, even if with that I still need to make a mask for each sprite, but 2 of each better than 5 (or more) :) I just need to adapt it with the Sprite/Diffuse shader ins$$anonymous$$d of the Sprite/Default one, in order to recieve light.

avatar image SarperS · Oct 22, 2014 at 02:36 PM 0
Share

You are welcome, please let me know if you have problems converting the shader to the diffuse version and I will do it for you. Please mark the answer as the solution.

avatar image Razouille · Oct 22, 2014 at 03:02 PM 0
Share

Yes sorry I'm not used to modify shaders, i got some syntax errors with these pragma, if it doesn't bother you it would be wonderful to help me with it.

avatar image SarperS · Oct 22, 2014 at 03:23 PM 1
Share

Sure, here you go, diffuse version. What do you mean by "modify the mask in realtime"? Like change the mask texture in runtime?

 Shader "Sprites/Diffuse $$anonymous$$asked Tint"
 {
     Properties
     {
         [PerRendererData] _$$anonymous$$ainTex ("Sprite Texture", 2D) = "white" {}
         _$$anonymous$$ask("Color $$anonymous$$ask", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [$$anonymous$$aterialToggle] PixelSnap ("Pixel snap", Float) = 0
     }
 
     SubShader
     {
         Tags
         {
             "Queue"="Transparent"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Fog { $$anonymous$$ode Off }
         Blend One One$$anonymous$$inusSrcAlpha
 
         CGPROGRA$$anonymous$$
         #pragma surface surf Lambert vertex:vert
         #pragma multi_compile DU$$anonymous$$$$anonymous$$Y PIXELSNAP_ON
 
         sampler2D _$$anonymous$$ainTex;
         sampler2D _$$anonymous$$ask;
         fixed4 _Color;
 
         struct Input {
             float2 uv_$$anonymous$$ainTex;
             fixed4 color;
         };
 
         void vert (inout appdata_full v, out Input o) {
             #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
             v.vertex = UnityPixelSnap (v.vertex);
             #endif
             v.normal = float3(0,0,-1);
 
             UNITY_INITIALIZE_OUTPUT(Input, o);
             o.color = v.color * _Color;
         }
 
         void surf (Input IN, inout SurfaceOutput o) {
             fixed4 mask = tex2D(_$$anonymous$$ask, IN.uv_$$anonymous$$ainTex);
             fixed4 color = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex);
             fixed4 finalC = lerp(color, color * IN.color, mask);
             o.Albedo = finalC.rgb * finalC.a;
             o.Alpha = finalC.a;
         }
         ENDCG
     }
 
     Fallback "Transparent/VertexLit"
 }
 
avatar image Razouille · Oct 22, 2014 at 03:40 PM 0
Share

Thanks a lot ! The only problem remaining is that in the motion, when I click on "Add Curve", I don't have the option to change the mask sprite, only "$$anonymous$$aask_ST" and "$$anonymous$$ask_Texel Size" :/

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

28 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

Related Questions

2D Animation does not start 1 Answer

Setting animator parameter on a single instance of a prefab sets the parameter for all instances 3 Answers

Avatar Mask for 2d sprites with skinning and rig 0 Answers

Object not moving after changing animator parameter 1 Answer

How to get all Sprites used in a 2D Animator? 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