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
1
Question by AmazingMrBrock · Mar 04, 2014 at 05:24 AM · c#spritespriterendererspritesheet

(C#) Help with Spriterenderer.sprite

I'm trying to figure out how to use this and I havent been able to search up too much information on it, the unity docs page is fairly useless.

What I want to do with it is have the spriterenderer load a various sprites when various variables are met, for example when my character jumps I want to change the idle sprite to a jump sprite. All my sprites are contained in a sprite sheet. Here is the script I've been working on for this, I pulled out empty methods and cleaned it up slightly.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     public Sprite stickSprite;
 
     GameObject pc;
     bool wallContact = false;
     bool facingRight = true;
     SpriteRenderer pcSprite;
 
     void Start (){
         pc = GameObject.Find("Player");
         pcSprite = pc.GetComponent<SpriteRenderer>();
         stickSprite = Resources.LoadAll<Sprite>("Sprites/StickManSheet");
         pcSprite.sprite = stickSprites["pcIdle"];
     }
 }


So please if someone would explain the SpriteRenderer.sprite workings to me I would be super appreciative.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Loius · Mar 04, 2014 at 07:41 AM

You can't (or couldn't when I tried) programmatically get complete listings of sprites outside of the Editor.

I have an Asset Store asset that creates SpriteSheets in the editor so you can do:

sprenderer.sprite = sheet.sprites[number]

As far as I know the only way to collect all sprites out of a "sprite" texture is this (you'll need "using System.Linq;"):

                     Object[] objects = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(sourceTexture));
                     Sprite[] sprites = objects.Select(x=>(x as Sprite)).Where(x=>x.name.Contains(sourceTexture.name+"_")).ToArray();


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 AmazingMrBrock · Mar 04, 2014 at 08:05 AM 0
Share

What about just pulling a single sprite out of the sprite sheet. I've already cut it up, and if I manually assign the sprite to a sprite variable through the editor it works fine. Is there a way I can programmatically point towards the sprites in the sprite sheet?

avatar image Loius · Mar 04, 2014 at 05:55 PM 0
Share
           Object[] objects = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(sourceTexture));
           Sprite[] sprites = objects.Select(x=>(x as Sprite)).Where(x=>x.name.Contains(sourceTexture.name+"_")).ToArray();

You just need to say

mySpriteRenderer.sprite = sprites[someIndex];

avatar image
1

Answer by Dblfstr · Mar 04, 2014 at 07:09 PM

You should really use mecanim for sprite animations (that is what this is). You have an idle animation (can be just one sprite) and a jump animation (also does not have to be multiple sprites). You create a parameter in the animator (created when you make an animation for a sprite) call it hasJumped or isJumping (set to false by default), whatever, make it a trigger or a bool. In your script that controls the jump set the trigger to true to tell the animator to switch to that animation. animator.setTrigger(hasJumped, true) something like that.

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

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

Prefabs and multiple tilesets? 0 Answers

Problem with a color randomization 2 Answers

How to truncate sprite without changing the scale of the sprite? 1 Answer

Override sprite geometry of sprite generated at runtime 1 Answer

How to Detect Which Sprite a Sprite Renderer Is Using? 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