UISprite.MakePixelPerfect C# (CSharp) Method

MakePixelPerfect() public method

Adjust the scale of the widget to make it pixel-perfect.
public MakePixelPerfect ( ) : void
return void
    public override void MakePixelPerfect()
    {
        if (!isValid) return;

        UpdateUVs(false);

        UISprite.Type t = type;

        if (t == Type.Sliced)
        {
            // Sliced sprite should have dimensions divisible by 2 for best results
            Vector3 pos = cachedTransform.localPosition;
            pos.x = Mathf.RoundToInt(pos.x);
            pos.y = Mathf.RoundToInt(pos.y);
            pos.z = Mathf.RoundToInt(pos.z);
            cachedTransform.localPosition = pos;

            Vector3 scale = cachedTransform.localScale;
            scale.x = Mathf.RoundToInt(scale.x * 0.5f) << 1;
            scale.y = Mathf.RoundToInt(scale.y * 0.5f) << 1;
            scale.z = 1f;
            cachedTransform.localScale = scale;
        }
        else if (t == Type.Tiled)
        {
            // Tiled sprite just needs whole integers
            Vector3 pos = cachedTransform.localPosition;
            pos.x = Mathf.RoundToInt(pos.x);
            pos.y = Mathf.RoundToInt(pos.y);
            pos.z = Mathf.RoundToInt(pos.z);
            cachedTransform.localPosition = pos;

            Vector3 scale = cachedTransform.localScale;
            scale.x = Mathf.RoundToInt(scale.x);
            scale.y = Mathf.RoundToInt(scale.y);
            scale.z = 1f;
            cachedTransform.localScale = scale;
        }
        else
        {
            // Other sprites should assume the original dimensions of the sprite
            Texture tex = mainTexture;
            Vector3 scale = cachedTransform.localScale;

            if (tex != null)
            {
                Rect rect = NGUIMath.ConvertToPixels(outerUV, tex.width, tex.height, true);
                float pixelSize = atlas.pixelSize;
                scale.x = Mathf.RoundToInt(rect.width * pixelSize) * Mathf.Sign(scale.x);
                scale.y = Mathf.RoundToInt(rect.height * pixelSize) * Mathf.Sign(scale.y);
                scale.z = 1f;
                cachedTransform.localScale = scale;
            }

            int width = Mathf.RoundToInt(Mathf.Abs(scale.x) * (1f + mSprite.paddingLeft + mSprite.paddingRight));
            int height = Mathf.RoundToInt(Mathf.Abs(scale.y) * (1f + mSprite.paddingTop + mSprite.paddingBottom));

            Vector3 pos = cachedTransform.localPosition;
            pos.x = (Mathf.CeilToInt(pos.x * 4f) >> 2);
            pos.y = (Mathf.CeilToInt(pos.y * 4f) >> 2);
            pos.z = Mathf.RoundToInt(pos.z);

            if (width % 2 == 1 && (pivot == Pivot.Top || pivot == Pivot.Center || pivot == Pivot.Bottom))
                pos.x += 0.5f;

            if (height % 2 == 1 && (pivot == Pivot.Left || pivot == Pivot.Center || pivot == Pivot.Right))
                pos.y += 0.5f;

            cachedTransform.localPosition = pos;
        }
    }

Usage Example

Exemplo n.º 1
0
	private void SetStarImage(StarType starType, UISprite spStar)
	{
		string starSpriteName = "fb_xing_yidengdao";
		switch(starType)
		{
            case StarType.StarType1:
		    {
                starSpriteName = "zs";
			
			    spStar.atlas = MogoUIManager.Instance.GetAtlasByIconName(starSpriteName);
			    spStar.spriteName = starSpriteName;
                spStar.MakePixelPerfect();
		    }break;

            case StarType.StarType2:
		    {
                starSpriteName = "hg_h";
			
			    spStar.atlas = MogoUIManager.Instance.GetAtlasByIconName(starSpriteName);
			    spStar.spriteName = starSpriteName;	
			    spStar.MakePixelPerfect();
		    }break;

            case StarType.StarType3:
		    {
			    starSpriteName = "hg";
			
			    spStar.atlas = MogoUIManager.Instance.GetAtlasByIconName(starSpriteName);
			    spStar.spriteName = starSpriteName;	
			    spStar.MakePixelPerfect();
		    }break;
		}		
	}
All Usage Examples Of UISprite::MakePixelPerfect