Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
  • Help Room /
avatar image
1
Question by Nicoolai · Jul 19, 2018 at 07:40 AM · animation2dspritespritesheet

Changing sprite and animation, through script

Hi,
I'm trying to build multiple skins, for the hero character, into my game. I can't really figure out exactly how to do it though.

Basically, the move animation is the same on all of them, so each sprite is a spritesheet with 3 pictures in it. Exactly the same setup for each.

So, can I create a generic animation, that will simply go for the next sprite in the sheet, and re-use that for each "skin"?

Also, how do I set these sprites from script? I've tried various methods but I end up with a blank character, each time.

I've tried various combinations of the following code:

         renderer = GetComponent<SpriteRenderer>();
         
         var spriteName = PlayerPrefs.GetString("Sprite", "flaming");
         var charSprite = Resources.Load("CharAssets/" + spriteName, typeof(Sprite)) as Sprite;
         renderer.sprite = charSprite;

But as mentioned, not much luck. My sprites for the hero character, is in a folder called CharAssets, in the Assets folder.

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

Answer by kalen_08 · Jul 19, 2018 at 03:05 PM

First of all don't be using Resources.Load and that kinda thing, the solution is simple yet hard... The thing for this is a AnimationControllerOverride,

  1. Create it in the same way as creating an Animator, you should see the option.

  2. Drag the Animator into the Override as the runtimeAnimatorController

  3. Create Default Animations (ie: IDLE, MOVE), they can just be empty animations

  4. Place these defaults into the Animator. (you should now see in the overrideController the names of the normal states.)

  5. Create a scriptableObject to store all of the different skins

script being able to switch skins:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [CreateAssetMenu (menuName = "Skin", fileName = "New Skin")]
 public class Skin : ScriptableObject
 {
     [SerializeField] AnimationClip idleAnimation = null;
     [SerializeField] AnimationClip movingAnimation = null;
 
     public AnimationClip GetIdleAnimation ()
     {
         return idleAnimation;
     }
 
     public AnimationClip GetMovingAnimation ()
     {
         return movingAnimation;
     }
 }

 [CreateAssetMenu (menuName = "Skin Atlas", fileName = "New Skin Atlas")]
 public class SkinsAtlas : ScriptableObject
 {
     [SerializeField] Skin defaultSkin = null;
     [SerializeField] List<Skin> Skins = new List<Skin> ();
 
     public Skin GetSkinAtIndex (int index)
     {
         if (index < Skins.Count)
         {
             return Skins[index];
         }
         else
         {
             Debug.LogWarning("index is not in range, add more skins or request a lower index");
             return defaultSkin;
         }
     }
 }

 public class Player : MonoBehaviour
 {
     [SerializeField] SkinsAtlas skinAtlas = null;
 
     Animator animator;
     AnimatorOverrideController overrideController;
 
     const string IDLE = "IDLE";
     const string MOVING = "MOVING";
 
     void SetSkinFromAtlas (int index)
     {
         var skin = skinAtlas.GetSkinAtIndex(index);
         SetSkinAnimations(skin);
     }
 
     void SetSkinAnimations (Skin skin)
     {
         animator = overrideController;
         overrideController.animationClips[IDLE] = skin.GetIdleAnimation();
         overrideController.animationClips[MOVING] = skin.GetMovingAnimation();
     }
 }
Comment
Add comment · Show 4 · 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 Nicoolai · Jul 19, 2018 at 05:51 PM 0
Share

Does this mean I would have to hook up each skin, with a Skin class, inside the editor? I kind of want to avoid having to do this manual work when I add new skins, and have a somewhat generic way of doing it. $$anonymous$$aybe just load up all the skins through code.

avatar image kalen_08 Nicoolai · Jul 19, 2018 at 06:00 PM 1
Share

Yes but this is the easiest way. Copy the three scripts in and create up a few Skin assets and a single SkinAtlas. Add the code from my player script to yours and then drag the Atlas to your player object. This is the most flexible way because you can create as many skins as you want. $$anonymous$$anually loading them will probably take time and also adding in all the different code will be a lot more work and WAY more messy way to do it. This is exactly how I do my weapons and there attacks.

avatar image kalen_08 Nicoolai · Jul 19, 2018 at 06:08 PM 1
Share

The skin and skin atlas are scriptable objects with [CreateAsset$$anonymous$$enu] so if you right click in the project window at the top should be options to create the two objects. :) Generally the logic is this: If something is a behaviour then you I herit from amonovegacuour If it is data (which this is) then you inherit from ScriptableObject And if its logic or calculations you inherit from nothing.

avatar image Nicoolai kalen_08 · Jul 19, 2018 at 06:15 PM 0
Share

Thanks, I'll give this a shot.

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

367 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Sprite sheet and animation slice doubt 0 Answers

Multiple Sprite Pivot Points 0 Answers

Same animation from multiple spritesheets 1 Answer

Should weapons be different sprites? 1 Answer

Sprite Animation via script C# 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