Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by Ruprecht1010 · Apr 14, 2015 at 04:59 PM · animationspritesspritesheet

Switching sprite sheets at runtime using Unityscript

Near the start of my game the user selects one of two playable characters.

For either character, all the animations are the same. It's just the look of the character that's different.

I am trying to figure out a unityscript function that will load the appropriate sprite sheet and 're-skin' the character.

In searches, I found another thread that points to a helpful YouTube tutorial (starts at 20:00) 2D issues and how to solve them with Veli Pekka Kokkonen;

However, it's in C# and I'm not very good at converting some of the more complex C# script.

I need to convert it because the other option is to try to pass a variable from javascript to C# and I'm having difficulty doing that as well.

Everything I've figured out so far in javascript functions as it should, but I've hit a wall trying to work with the loop and array.

Can someone help me convert the rest of the C# code?

The variable spriteSheetName is either "Girl1" or "Girl2" and refer to the actual sprite sheet files which reside in 'Assets/Resources/kid/...'

The code is attached to the animated sprite.

Here's the C# script:

 using UnityEngine;
 using System;
 
 public class ReSkinGemma : MonoBehaviour {
 
     public string spriteSheetName = "Girl1";
 
     void LateUpdate () 
     {
         var subSprites = Resources.LoadAll<Sprite>("kid/" + spriteSheetName);
         foreach (var renderer in GetComponentsInChildren<SpriteRenderer>())
         {
             Debug.Log("renderer= " + renderer + "\n");
 
             string spriteName = renderer.sprite.name;
             var newSprite = Array.Find(subSprites, item => item.name == spriteName);        
             if (newSprite)
                 renderer.sprite = newSprite;            
         }
     }
 }

And, here's as far as I've gotten so far trying to convert it to unityscript:

 #pragma strict
 public    var spriteSheetName: String = "Girl1";
 public    var subSprites: Object[];
 
 function LateUpdate () 
 {
     subSprites = Resources.LoadAll("kid/Girl1", typeof(Sprite));  
     var renderer = GetComponentInChildren(SpriteRenderer);

     Debug.Log("renderer= " + renderer + "\n");

 //    for (???)
 //    {
         var spriteName : String = renderer.sprite.name;
     //    var newSprite = Array.Find(subSprites, item => item.name == spriteName);        
     //    if (newSprite)
     //        renderer.sprite = newSprite;    
 //    }

 }


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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

how should I make sprites 0 Answers

How can I change sprites in animation clip? 0 Answers

Is it possible to animate a sprite on a 3d mesh? 0 Answers

how to display a single frame of a sprite animation 1 Answer

add animation frames to existing spritesheet with animations? 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