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:
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.:
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
)