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
1
Question by tanoshimi · Oct 07, 2016 at 09:48 PM · animation2dspritesnormal map

Is it possible to change the texture property of a material via Animation clip?

I have a 2d character that has separate sprite sheets for each of their animations - idle, walk, run cycles etc. Those sprite sheets each have associated normal maps as well, so in the animation clip that defines the individual sprites to animate, I'd also like to specify the corresponding texture to load into the normal map slot of my custom material.

The problem is that, while every other property of my material appears to be exposed in the animation window, the normal map texture simply is not, and I can't work out whether there's a reason for this limitation or (even better) a workaround? ![alt text][1]

My current fix is (as can be seen in the screenshot) to create entirely separate materials for each cycle with the correct normal map statically assigned to each, and change the material via the Sprite Renderer.Material Ref property. However, this seems very unwieldy to create separate materials for each animation, and I'd much rather simply animate the texture property of a single material. Has anyone else experienced this issue or got any advice? Thanks. [1]: /storage/temp/79719-animate-normals.jpg

animate-normals.jpg (121.2 kB)
Comment
Add comment · Show 6
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 RomanLed · Oct 07, 2016 at 10:13 PM 1
Share

could you put your idle, walk, run cycles all on one texture and just start the animation at the correct frame depending on what animation is supposed to play? then you wont have to change the texture and save memory.

avatar image tanoshimi RomanLed · Oct 07, 2016 at 10:17 PM 0
Share

Thanks for the reply, but we have at least 12 different character animation cycles, some containing 60+ frames, so we can't pack them all into one sheet.

avatar image RomanLed · Oct 07, 2016 at 10:29 PM 0
Share

you try material.setTexture? https://docs.unity3d.com/ScriptReference/$$anonymous$$aterial.SetTexture.html

avatar image tanoshimi RomanLed · Oct 08, 2016 at 06:44 AM 0
Share

Yes, I'm aware I can use SetTexture to do it in code. And I could also create an animation event in the clip that triggers that code. But for the purposes of maintainability, I'd like to keep all the animated properties relating to the animation directly in the clip itself. For some reason, all the other properties of the material are exposed, but not any texture slots.

avatar image RomanLed tanoshimi · Oct 08, 2016 at 07:44 AM 1
Share

to my knowledge it is not possible to unload one texture and load another texture from within the animation window without using a script, especially without creating an instance of the material. it is only possible to alter its properties such as vertex coords, uvs, scale, tiling etc.

However maybe someone will correct me if I'm wrong because my experience is with meshRenderers rather than spriteRenderers. $$anonymous$$eshRenders always create instances and i work with $$anonymous$$eshFilters uv coordinates ins$$anonymous$$d to animate textures because they dont create instances of the material. Sorry i dont have a solution to your question

Show more comments

2 Replies

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

Answer by tanoshimi · Oct 08, 2016 at 08:26 AM

Based on @RomanLed's comments above and my own experience, it seems it's not possible to animate textures of a material directly via an animation clip. The solution I've ended up going for is to create a new StateMachineBehaviour which overrides OnStateEnter attached to each state in my Animator, which assigns the correct _NormalMap texture corresponding to the atlas of sprites used in that state.

 public class SetNormalMap : StateMachineBehaviour
 {
     public Material material;
     public Texture2D normalMap;
 
     // This will be called when the animator first transitions to this state.
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         material.SetTexture("_NormalMap", normalMap);
     }
 }

alt text

In hindsight, this might actually be a more elegant solution than my original plan anyway, since it only assigns the texture once when each state is first entered, rather than reassigning the same texture each time an animation clip looped which would be the case either with an animation event or a animated material property.


statemachinebehaviour.jpg (29.9 kB)
Comment
Add comment · Show 2 · 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 Chipjolo · Jan 16, 2019 at 05:16 PM 0
Share

EDIT to the Script: the _Normal$$anonymous$$ap doesn't work ; _Bump$$anonymous$$ap does

avatar image DanielAtLevelEx · Oct 10, 2019 at 03:52 PM 0
Share

Wanted to say thank you so much for this script as I didn't know you could run scripts like this in the animator states until now and it really has unlocked many possibilities

avatar image
0

Answer by talonairk · Mar 28 at 03:28 AM

Thanks for the solution! It wasn't working for me as-is. So I modified it for anyone else that's having trouble: public class SetNormalMap : StateMachineBehaviour { private Renderer _renderer; public Material material; public Texture2D normalMap;

     // This will be called when the animator first transitions to this state.
     private void Awake()
     {
         _renderer = FindObjectOfType<PlayerController>().GetComponent<Renderer>();
         _renderer.material.EnableKeyword ("_NormalMap");
     }
 
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         _renderer.material.SetTexture("_NormalMap", normalMap);
     }
 }

If you get any glitches with the normal maps at the start/end of animations, check the blending on your animation states.

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

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

I have an animaiton problem about sprite loading. 0 Answers

Animating 2D Character made up of 2 or more swapable sprites 1 Answer

Is Unity capable of implementing Complex 2D Characters/Animation/Sprites 1 Answer

Alpha masking multiple 2D Sprite GameObjects with 1 Sprite GameObject 0 Answers

Fast Sprites Extraction from Sprite Sheet 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