Hello Guest

Author Topic: Getting Texture2D instance from sprite collection  (Read 3853 times)

Pezzo

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Getting Texture2D instance from sprite collection
« on: March 16, 2014, 05:22:55 pm »
I'm new to tk2d and to unity in general, so bear with me...

I'm having a problem trying to get a Tex2D instance from a sprite collection

My code is
Code: [Select]
Collection.spriteCollection.textures[Collection.spriteCollection.GetSpriteIdByName(TextureName)] as Texture2D).GetPixels()and the error i get is :
"Texture 'atlas0' is not readable, the texture memory cannot be accessed from scripts. You can make the texture readable in the Texture Import Settings"

I found a checkbox in the Texture Import Settings (Read/Write Enabled) that i set to True, but the problems seems to persist : maybe that wasn't the right option to change?

If you're feeling generous, i have an additional question: i'm not sure that my approach to the problem is the best one, so maybe someone can put me in the right direction. What i'm trying to accomplish is how many pixels of 2 textures are 'overlapping': my attempt was to create the pixelmaps of the 2 textures, then check one of them : if the alpha channel was above a threshold i would then check if the corresponding pixel of the other pixelmap had the alpha channel above the threshold too. English is not my native language and i understand that this stuff i wrote may not be understandable, so i'll put a mock-up code here:
Code: [Select]
map1=CreatePixelMap1();
map2=CreatePixelMap2();

Foreach (color col in map1){
if (col.a >something){
                if(map2[Someindex].a>something) overlappingPixels++;
          }
}


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting Texture2D instance from sprite collection
« Reply #1 on: March 17, 2014, 11:57:52 am »
Its the read/write setting that needs to be set, but your code is incorrect, indexing by SpriteId will result in you picking textures outside the bounds. sprite Id defines the sprite definition.

Collection.spriteDefinitions[Collection.spriteCollection.GetSpriteIdByName(TextureName)].materialInst.mainTexture will give you the atlas texture.
The source textures do not exist in the sprite collection at runtime. you will need to set the read write enabled setting on the atlas textures.


What you're trying to do isn't that straightforward. The textures are atlased, so you'll need to find out where in the atlas your texture is in the atlas (http://2dtoolkit.com/forum/index.php/topic,3675.msg17653.html#msg17653) and then you can work out alpha and compare them. It is very slow, and making your texture read/write will make an additional copy of the texture in memory.