2D Toolkit Forum

2D Toolkit => Support => Topic started by: oug on July 21, 2015, 11:38:09 am

Title: [Solved] Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: oug on July 21, 2015, 11:38:09 am
Hi!

I've managed to create a spriteCollection that supports tk2dSystem.Currentplatform 1x, 2x and 4x. This all works great and very intuitive (thank you for that!). However I've found a small issue with my current setup:

Since I've added ALL my PNG's to the spriteCollection (including multiple background images (of 2560x1600 in the 4x category)) my spriteSize is significant (the 4x spriteSize is 4096x8192), which causes an all black game on my iPad Retina (the game runs perfectly on my iPad2 though cause the 2x spriteSize is smaller, oh the irony).

I've removed the background images from the spriteCollection and now everything runs perfectly (aside from the game not having backgrounds of course).

My question is: how can I use these backgrounds (with the 1x 2x and 4x platform-support in mind) without them adding up to the spriteCollection. Can I use SpriteFromTexture for this? And if so: how? (I've already tested this using my current setup, but it just takes the 1x version).

Keep up the good work and thank you in advance!
Title: Re: Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: unikronsoftware on July 21, 2015, 12:35:09 pm
You will just need to load the correct texture and use SpriteFromTexture to draw the background - you can scale it appropriately for the 2x and 4x versions. Try it and it should be obvious what you'll need to do tghere.
Title: Re: Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: oug on July 21, 2015, 12:48:41 pm
So I manually (based on the tk2dSystem.CurrentPlatform value) add the correct PNG to the Sprite from Texture script, or am I reading this wrong?
Title: Re: Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: unikronsoftware on July 21, 2015, 12:56:33 pm
Thats right. You'll also need to scale them so they are the same physical size but you can do this by scaling the pixelsPerMeter / orthoSize values when you create the tk2dSpriteCollectionSize object.
Title: Re: Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: oug on July 21, 2015, 01:04:13 pm
Allright thanks! I'll try this.

You think this is the best way to tackle this problem? Would a second SpriteCollection be better (draw-call wise)?
Title: Re: Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: oug on July 21, 2015, 01:45:53 pm
Okay so this works!

I've  added a script on the tk2dSpriteFromTexture with the following code:

Code: [Select]
using UnityEngine;
using System.Collections;

public class SpriteFromTexturePlatformScript : MonoBehaviour {

    public Texture[] test;
    private Texture xSprite;
    private int size = 0;

    void Awake(){

        if(tk2dSystem.CurrentPlatform == "4x"){
            xSprite = test[2];
            size = 4;
        } else if(tk2dSystem.CurrentPlatform == "2x"){
            xSprite = test[1];
            size = 2;
        } else {
            xSprite = test[0];
            size = 1;
        }
        GetComponent<tk2dSpriteFromTexture>().texture = xSprite;
       
        GetComponent<tk2dSpriteFromTexture>().spriteCollectionSize.type = tk2dSpriteCollectionSize.Type.PixelsPerMeter;
        GetComponent<tk2dSpriteFromTexture>().spriteCollectionSize.pixelsPerMeter = size;
        GetComponent<tk2dSpriteFromTexture>().ForceBuild();
    }
}

This adds the proper sprite (i.e. 1x, 2x or 4x) to the tk2dSpriteFromTexture and scales the pixelsPerMeter to either 1, 2 or 4.

In terms of loading and memory this is probably not the best way (adding all three sprites (1x, 2x and 4x) to the public Array. Would Resources.Load() be better in this situation?

Anyway, thank you for your quick responses once again!
Title: Re: [Solved] Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: unikronsoftware on July 22, 2015, 04:15:45 pm
Hi, Resources.Load would be way better, otherwise you'd be using up a lot of memory.
Title: Re: [Solved] Sprite from texture and tk2dSystem.Currentplatform (1x 2x 4x)
Post by: oug on August 07, 2015, 10:05:47 am
Hi, Resources.Load would be way better, otherwise you'd be using up a lot of memory.
Awesome! Thanks!