Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dhaggerfin · Aug 11, 2017 at 07:32 AM · spriteresizesprite renderer

Resize a sprite on a sprite renderer

I've got a Sprite inside a Sprite Renderer and it looks great. However, when I click a button I want that sprite to change from one Sprite to another. However, my second sprite is much larger than the first. As a result, it's overflowing my Sprite Renderer.

Here is the code attached to my button:

 public Sprite[] myCardSprites;

 public void changeCardArtwork ()
     {
         GameObject g = GameObject.FindGameObjectWithTag ("CardArt");
         SpriteRenderer s = g.GetComponent<SpriteRenderer> ();
 
         foreach (Sprite currentSprite in myCardSprites)
         {
             //Set the image to the Rhino
             if (currentSprite.name == "Rhino")
             {
                 s.sprite = currentSprite;
             }
         }
     }

I've tried a couple of differnt things like changing the transform.localscale on the SpriteRender and tried Sprite.texture.Resize. I also tried to add a Rect Transform onto my SpriteRender to sort of set bounds. Nothing has worked so far.

What I'd love to happen is for the sprite to just take on the size of the sprite renderer.

Thanks in advance.

Comment
Add comment · Show 1
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 dhaggerfin · Jun 18, 2016 at 05:55 PM 0
Share

For a workaround, I've resized all of my sprites to be the same size. The two answers below sort of have me thinking it's based on the size of the screen, and that I'd have to do some calculations to fit a randomly sized image into my sprite renderer.

http://answers.unity3d.com/questions/620699/scaling-my-background-sprite-to-fill-screen-2d-1.html

http://answers.unity3d.com/questions/1042119/getting-a-sprites-size-in-pixels.html

Something like

  1. Figure out the size in PX of the sprite

  2. Figure out the size of the screen

  3. Figure out the size of the sprite renderer

  4. Scale the sprite renderer by some factor X to match the amount of space I want it to take up on the screen.

In the end it was less brain work to just resize each sprite. I'll leave the question here unanswered unless someone wants me to close it, in case someone wants to comment for a future person to use.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LZakhar · Aug 14, 2017 at 08:53 PM

@dhaggerfin if this would help you I`ve used next script to scale sprites to one size. Note that they must be read\write enabled.

     private Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
     {
         Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, true);
         Color[] rpixels = result.GetPixels(0);
         float incX = (1.0f / (float)targetWidth);
         float incY = (1.0f / (float)targetHeight);
         for (int px = 0; px < rpixels.Length; px++)
         {
             rpixels[px] = source.GetPixelBilinear(incX * ((float)px % targetWidth), incY * ((float)Mathf.Floor(px / targetWidth)));
         }
         result.SetPixels(rpixels, 0);
         result.Apply();
         return result;
 
     }
Comment
Add comment · Show 1 · 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 amorales · Apr 24, 2020 at 06:09 PM 0
Share

@LZakhar , thanks for share this.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I change a game object's sprite in C# 1 Answer

How to optimise 5000 sprites? 1 Answer

Sorting layers without far objects overlapping closer ones? 0 Answers

some sprites sliced in Game view, How can I fix this? 0 Answers

Sprite didn't change more than once 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