Hello Guest

Author Topic: [Solved] tk2dSprite.CreateFromTexture w/generated texture help.  (Read 6210 times)

KronusGT

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
[Solved] tk2dSprite.CreateFromTexture w/generated texture help.
« on: September 27, 2013, 11:36:59 pm »
I'm having trouble getting this to work properly. I'm new to Unity/2D Toolkit and moving my project over from FlatRedBall, where I did everything in code. In that, I had created a method which outputted a solid Texture2D to be used for a sprite. I created this so I could easily throw together overlays and fades of any color. Here is the method:

Code: [Select]
public Texture2D GetSolidTexture(Color C)
{
Texture2D _SolidTexture = new Texture2D(64, 64, TextureFormat.RGBA32, false, false);

Color[] _C = new Color[_SolidTexture.width * _SolidTexture.height];

for (int i = 0; i < _C.Length; i++)
{ _C[i] = C; }

_SolidTexture.SetPixels(_C);

return _SolidTexture;
}

I had it at 2x2 so it would take up as little space as possible, but I changed it to 64x64 so it would be visible for testing.

Here is me trying to create the sprite, a black overlay that I'll be fading out:

Code: [Select]
GameObject _BlackOverlay = tk2dSprite.CreateFromTexture(GetSolidTexture(new Color(0f, 0f, 0f, 1f)), tk2dSpriteCollectionSize.Explicit(576f, 64f), new Rect(0f, 0f, 64f, 64f), new Vector2(0f, 0f));
tk2dSprite _BlackOverlaySprite = _BlackOverlay.GetComponent<tk2dSprite>();
_BlackOverlaySprite.SortingOrder = 5;
_BlackOverlaySprite.scale.Set(32f, 18f, 1f);
_BlackOverlaySprite.transform.Translate(-1024f, 576f, 0f);

I'm not using tk2dCamera. The base size seems to be fine (still getting used to this ortho+height thing...), but scale.Set fails to change it. The texture color is also incorrect, instead of solid black it is transparent white. Can anyone figure out what I'm doing wrong?
« Last Edit: September 28, 2013, 12:34:22 am by KronusGT »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dSprite.CreateFromTexture w/generated texture help.
« Reply #1 on: September 27, 2013, 11:41:09 pm »
sprite.scale is a property.
Changing the return value won't actually change the value do this instead:

Code: [Select]
Vector3 s = sprite.scale;
s.Set( x, y, z );
sprite.scale = s;

KronusGT

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: tk2dSprite.CreateFromTexture w/generated texture help.
« Reply #2 on: September 28, 2013, 12:07:50 am »
Ok, got that fixed then. Thanks. I'm used to being able to change scale, position, and color directly.

Any idea on why the color ends up transparent white instead of solid black?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dSprite.CreateFromTexture w/generated texture help.
« Reply #3 on: September 28, 2013, 12:25:23 am »
Missed that - you need to call texture.Apply() before using it.

KronusGT

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: tk2dSprite.CreateFromTexture w/generated texture help.
« Reply #4 on: September 28, 2013, 12:34:06 am »
Thanks, that was driving me nuts.