Hello Guest

Author Topic: tk2dSprites render pink when changing scene / using LoadScene  (Read 11952 times)

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
tk2dSprites render pink when changing scene / using LoadScene
« on: February 04, 2016, 06:21:11 pm »
Okay so here's a problem that I currently have with _all_ my projects (I'm guessing since the 5.3 update): every time I change a scene (using SceneManager.LoadScene("sceneName"); ) al lot of my sprites render pink (in fact: only the sprites where I access the material property). This is due to a missing Material in the Mesh Renderer (somehow the Mesh Renderer looses it's material). The strange thing is that when I start the loaded scene on it's own, everything works perfect. Let me explain step by step:

- If I run my "Game" scene, everything is peachy
In my Scene-view everything renders a-ok (see screenshot: GAMESceneViewNotRunning)
In my Game-view everything renders a-ok, after starting the "Game"-scene (see screenshot: GAMESceneRunning)

Now I'm currently using an "Init" scene which addresses all the properties of the camera. I'm using this scene to decide what ratio, resolution and view (landscape / portrait) I am going to use. This "Init" scene is basically an empty scene with a single tk2dCamera, and the following script:

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

public class InitializeStageScript : MonoBehaviour
{
void Start ()
{
// Set targetframe
Application.targetFrameRate = 60;

// set tk2d platform based on width or height (what's biggest)
float sWidth = Screen.width;
float sHeight = Screen.height;
float highest = 0;
bool isPortrait = false;

if (sWidth > sHeight) {
highest = sWidth;
} else {
isPortrait = true;
highest = sHeight;
}
if (highest > 1136) {
tk2dSystem.CurrentPlatform = "4x";
} else if (highest > 480) {
tk2dSystem.CurrentPlatform = "2x";
} else {
tk2dSystem.CurrentPlatform = "1x";
}

// optimize tk2d platform for platform based on maxSpriteSize
// TODO ALWAYS: change for max texture size in game (see 4x atlas in collectiondata)
// if atlas0.png in 4x folder is 2048x2048 then maxTextureSize < 2048
if (tk2dSystem.CurrentPlatform == "4x" && SystemInfo.maxTextureSize < 2048) {
tk2dSystem.CurrentPlatform = "2x";
} else if (tk2dSystem.CurrentPlatform == "2x" && SystemInfo.maxTextureSize < 1024) {
tk2dSystem.CurrentPlatform = "1x";
}

// setup level based on ratio
string prefix;
string ratio;
if (isPortrait) {
prefix = "Running in PORTRAIT mode with an aspect of: ";
if (Camera.main.aspect >= 1f) {
ratio = "1:1";
} else if (Camera.main.aspect >= 0.795f) {
ratio = "4:5";
} else if (Camera.main.aspect >= 0.745f) {
ratio = "3:4";
} else if (Camera.main.aspect >= 0.655f) {
ratio = "2:3";
} else if (Camera.main.aspect >= 0.615f) {
ratio = "10:16";
} else if (Camera.main.aspect >= 0.595f) {
ratio = "3:5";
} else if (Camera.main.aspect >= 0.575f) {
ratio = "10:17";
} else if (Camera.main.aspect >= 0.555f) {
ratio = "9:16";
} else if (Camera.main.aspect >= 0.415f) {
ratio = "9:21";
} else {
ratio = "NOT FOUND SET TO 9:21";
}
} else {
prefix = " and running in LANDSCAPE mode with an aspect of: ";
if (Camera.main.aspect >= 2.325f) {
ratio = "21:9";
} else if (Camera.main.aspect >= 1.765f) {
ratio = "16:9";
} else if (Camera.main.aspect >= 1.695f) {
ratio = "17:10";
} else if (Camera.main.aspect >= 1.655f) {
ratio = "5:3";
} else if (Camera.main.aspect >= 1.595f) {
ratio = "16:10";
} else if (Camera.main.aspect >= 1.495f) {
ratio = "3:2";
} else if (Camera.main.aspect >= 1.325f) {
ratio = "4:3";
} else if (Camera.main.aspect >= 1.245f) {
ratio = "5:4";
} else if (Camera.main.aspect >= 1f) {
ratio = "1:1";
} else {
ratio = "NOT FOUND SET TO 1:1";
}
}
Debug.Log ("Game Initialized at: " + tk2dSystem.CurrentPlatform + "  " + prefix + ratio);
SceneManager.LoadScene ("Game");
}
}

So basically the script runs on Start and decides what to do with the tk2dSystem.CurrentPlatform (and other aspecs such as ratio). In this project I only use the tk2dSystem.CurrentPlatform setup, in others I also use the ratio (to setup my scene differently for different ratios). When it's finished, it runs the next scene (which is the "Game" scene mentioned earlier).

Anyway. When running the "Init" scene, everything turns into a hot pink mess:
- The "Init" scene runs, and sets everything up. This can be seen by the Log-statement in the console (see screenshot: INITSceneRunningStep1)
- The "InitializeStageScript" runs and loads the "Game" scene, but now everything is Pinky-pink. Also the material of the sprite is missing (see screenshot: INITSceneRunningStep2)

As said, the missing material (pink sprite) only happens to the sprite that I access through the material properties (i.e.:

Code: [Select]
Material[] m = GetComponent<Renderer>().materials;
I don't get any errors, or other strange behaviour. I found this thread: http://2dtoolkit.com/forum/index.php/topic,5247.msg24038.html#msg24038 but I'm not quite sure if it applies here (I also have no idea how to fix my problem based on that thread).

Can you help? This really sucks (as it applies to all my projects :( )
« Last Edit: February 05, 2016, 06:13:33 pm by oug »

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #1 on: February 05, 2016, 05:03:08 pm »
I've created a clean test-environment (one sprite, two scenes, two scripts, just the bare basics) to rule out any other scripts or frameworks (pooling, tweening, etc), and it is still happening. I have the ZIP folder with the env, if you would like to have a look. Let me know where to send this (it's 2.1 mb).

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #2 on: February 05, 2016, 05:54:18 pm »
Holy sh*t, I found it (actually a colleague pointed me in the right direction)! The problem lies within Unity 5.3. Apparently, every call to the material properties of a tk2dSprite within the first frame of the Start() breaks about everything. If I skip the first frame in the Start(), everything work perfectly!

So if I do this hack, it works!
Code: [Select]
// this works
private IEnumerator Start(){
     yield return new WaitForSeconds(0);
     Material m = settingsBackgroundButton.GetComponent<Renderer> ().materials [0];
     Debug.Log(m[0].name);
}

// this generates pink sprites
private void Start(){
     Material m = settingsBackgroundButton.GetComponent<Renderer> ().materials [0];
     Debug.Log(m[0].name);
}

A second thing to keep track of is that these issues DON'T affect any -builds- that were made with 5.3. So the sprites in my build for iOS are all there (and not pink), even though the same build turns everything pink in the editor / playmode.

I did some other tests on the default Unity prefabs (sprites, cubes, etc), but it doesn't seem to affect those. I guess that this bug has something to do with Unity 5.3 in combination with tk2d? Something has changed in 5.3 that affects the allocation of the materials within the tk2d framework (in 5.2.4 everything works without hassle).

So yeah, I'm rambling. If anyone needs more info, let me know. For now I revert back to 5.2.4 (I don't feel like changing all my Start voids into IEnumerators). Hopefully unikron can come up with a fix, that would be awesome (or we can wait for the next update of Unity 5.3 and see what happens then).
« Last Edit: February 05, 2016, 06:13:04 pm by oug »

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #3 on: February 09, 2016, 04:35:17 pm »
Okay I did some more research regarding this issue. If I use LoadSceneMode.Additive when loading the "Game" scene (this way the "Init" scene doesn't get destroyed), everything works peachy ( SceneManager.LoadScene("Game", LoadSceneMode.Additive) )

However as soon as I Unload the "Init" scene in the "Game" scene (  SceneManager.UnloadScene("Init") ), the renderer looses it's material and the texture turns pink again.

So yeah, still not sure where exactly the problem lies. I'll keep on it, I guess.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #4 on: February 23, 2016, 09:36:00 pm »
Sorry for the late reply - why are you using GetComponent<Renderer>().materials instead of sharedMaterials?
ps. happy to look at the repro if you send it to support at unikronsoftware dot com.

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #5 on: February 24, 2016, 08:44:12 am »
Sorry for the late reply - why are you using GetComponent<Renderer>().materials instead of sharedMaterials?
ps. happy to look at the repro if you send it to support at unikronsoftware dot com.
No worries, we are all busy men :). Thank you for your reply.

Well, I'm actually not REALLY using GetComponent<Renderer>().materials. I'm using an addition to LeanTween that allows me to alpha-tween tk2dSprites really easy (and this breaks the material as well, comparable to the GetComponent<Renderer>().materials).

But I'll send you a clean repo via mail. Hopefully you can shed some light on this.

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #6 on: March 09, 2016, 11:13:39 am »
Not to be pushy, but were you able to have a look at the stuff I sent? Keep up the good work!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #7 on: March 14, 2016, 11:39:40 pm »
Yes! Replied now :) Sorry for the delay

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #8 on: March 15, 2016, 09:28:24 am »
Thanks! I've replied again with some more questions. Awesome to get a bit more insight in the inner workings of tk2d and Unity. :) Let's hope we can get to the bottom of this as well.

madfatcat

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #9 on: September 29, 2016, 10:52:39 am »
Hey,

Did you end up with a working solution on this? I faced the same problem in my game in Unity 5.3.4. I don't refer to object's material in any way, but sometimes after reloading a scene I got a pink rectangle instead of my character's sprite. This happens in 1 of 50 times or so.

Doghelmer

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #10 on: January 16, 2017, 07:18:58 pm »
I'm getting the same issue -- VERY occasional instances where objects are surrounded by pink squares -- and am curious if this was ever resolved.  I was able to alleviate the issue by setting the SharedMaterial in a script every time I change the sprite, but it still appears to be happening sometimes.

oug

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: tk2dSprites render pink when changing scene / using LoadScene
« Reply #11 on: January 17, 2017, 08:18:56 am »
Hi all,

Sorry for the late reply, I just received a notification on a new post and I didn't see the post of madfatcat, sorry. Anyway, I've been in contact with unikron and we've come up with the following solution:

Quote
Got to the bottom of it. Basically tk2d has a watcher to help unload unused textures when a scene has changed (Unity didn't do a great job with that and basically ended up running out of memory...) when platform collections are used.

So, whats happening here is -
1. Sprite starts up
2. You grab material that gets spawned
3. Listener destroys material
4. Turns pink

If you don't have any issues with memory you could try turning it off tk2dEditorSpriteDataUnloader, comment out the first line. If you do there might be other not so pleasant ways around this.

You can try this, it seemed to work for me. For those concerned about memory load unikron mentioned:

Quote
Disabling the tk2dEditorSpriteDataUnloader shouldn't cause any major issues in 64 bit versions of Unity (i.e. 5.x), it is there to reduce memory consumption in 32 bit versions.

It looks like the when SpriteDataUnloader runs has changed in 5.3. Nothing to worry about - I'd just leave it off until (if ever) it becomes an issue. Basically its supposed to unload all materials when a scene is unloaded, but Unity 5.3 introduced multiple scene loading and this is now moot because of that.

Hope to have helped some! Good luck and happy devving!