Hello Guest

Author Topic: Lifetime of self created sprite?  (Read 3706 times)

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Lifetime of self created sprite?
« on: November 30, 2013, 03:49:56 pm »
Hi,

I am just in the process of creating my own scene transition manager. I know there are a lot around using GUI textures, but I wanted to try my own using only toolkit 2D features, with a 1x1 black px scaled sprite that overlays the screen and will be tweened over the alpha. This all works great for the fade-out, the new scene is loaded, but then the GO-child of the TransitionManager singleton holding the black sprite no longer has the generated sprite on board. The GO itself is not destroyed on load, and I have no clue why the sprite isn't surviving the loading. This is how I set up the TransitionManager singleton:

Code: [Select]
         // create a black overlay for the whole screen
Texture2D fadeTexture = new Texture2D(1,1);
fadeTexture.SetPixel(0, 0, Color.black);
fadeTexture.Apply();

_fadeGO = tk2dSprite.CreateFromTexture(fadeTexture, tk2dSpriteCollectionSize.ForTk2dCamera(tk2dCamera.Instance),
new Rect(0,0,1,1), new Vector2(0.5f, 0.5f) );
_fadeGO.name = "Fader";
_fadeGO.transform.parent = transform;
_fadeGO.renderer.sortingLayerName = "Effects";
_fadeSprite = _fadeGO.GetComponent<tk2dSprite>();
_fadeSprite.scale = new Vector2(tk2dCamera.Instance.ScreenExtents.width/_fadeSprite.CurrentSprite.GetUntrimmedBounds().size.x,
                                tk2dCamera.Instance.ScreenExtents.height/_fadeSprite.CurrentSprite.GetUntrimmedBounds().size.y);
_fadeGO.SetActive(false);

DontDestroyOnLoad(this);

Is the tk2dUIManager somehow cleaning up sprites?

This is how it looks before the screen transition:


And this is the state after the new scene has been loaded:


Anything else that I need to do to save my little sprite over to the other side?

Thanks, habitoti
Checkout our homepage or visit the Rombos blog.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Lifetime of self created sprite?
« Reply #1 on: November 30, 2013, 06:15:10 pm »
When you .CreateFromTexture, it creates a spriteCollectionData object in the scene. This is getting destroyed when you switch scenes. You will need to set that to DontDestroyOnLoad, or alternatively, make it a child of the sprite.

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Re: Lifetime of self created sprite?
« Reply #2 on: December 01, 2013, 08:32:07 am »
A always, you've hit the bull's eye right away...works like a charme now with the collection being a child of the sprite GO...
Thanks, habitoti
Checkout our homepage or visit the Rombos blog.