Small skill in unity when the Uisprite component switches pictures

Source: Internet
Author: User
Tags switches

Ngui should be considered the most common component in unity. Uisprite is a good comrade, hard to help the game engine to improve rendering efficiency. But I do not understand that the localsize of the transform component for Mao Uisprite is to be set to the original size of the picture in order to display it properly, why not let localsize = new Vector3 (1,1,1) just fine?

This also brings a problem, the game in a gameobject with a sprite when switching to other sprites, but also keep the original Sprite transform properties, so that some pictures may be in the process of switching to become disproportionate. For example, look at the following two pictures, the protagonist of our game after a change of action, because the size of the picture is inconsistent and has been stretched.



The localsize of the sprite is then reset every time you switch. Where can you know the size of the sprite, in fact, there is no relevant property in the Sprite component, this property is hidden in the sprite corresponding to the Altas. So, I wrote the following code. Specifically for resizing when switching sprites. The code is quite simple and should be easy to understand.

Using Unityengine;
Using System.Collections;

public class Spritehelper:monobehaviour
{

    //adjust uisprite public
    static void Adjustscale (Uisprite uisprite)
    {
        //based on Spritename find Sprite
        uiatlas.sprite sprite = UiSprite.atlas.GetSprite in Uiatlas ( Uisprite.spritename);
        UiSprite.transform.localScale = new Vector3 (sprite.outer.width, sprite.outer.height, 0);
    }
   Adjust uitexture public
    static void Adjustscale (Uitexture uitexture)
    {
        Texture Texture = Uitexture.maintexture;
        UiTexture.transform.localScale = new Vector3 (texture.width, texture.height, 0);
    }
}

Okay, now it's a perfect solution.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.