I have a small side scrolling shooter with a camera holding two backgrounds for parallax effect.
Each level is just the same except for these two backgrounds.
I have duplicated the level for the five different sets of background but I was wondering if it was possible and more efficient with space and resources to just load different materials into the backgrounds at the end of each level.
Any advice and scripting tips would be really appreciated.
It would be better for maintenance of your project if you load the textures from Resources (Resources.Load), and then apply them to your renderers (renderer.material.SetTexture). This way you avoid duplicating the scene, and also avoid using memory for textures you wont be needing on the level.
Yeh that’s right, the folder should be called exactly Resources and be under the Assets folder somewhere (can be within subfolders of that, as long as it’s under assets)
I would personally use renderer.sharedMaterial.SetTexture to avoid a new material instance being created (it creates a new one with every change to .material on the renderer, using up more memory than you think). So you’d end up with something like so when loading your game:
m_LevelOneTex = Resources.Load("LevelOne") as Texture2D;
m_LevelTwoTex = Resources.Load("LevelTwo") as Texture2D;
m_LevelThreeTex = Resources.Load("LevelTwo") as Texture2D;
With the variables being texture 2d cached in a class or maybe even statically (be careful with that), then on changing the level
Hey, i would make it with “Random.Range” & “Switch Statement”
Example:
[HideInInspector]
public Texture2D backgroundTexture = null;
int textureNr;
//Attack this Script to a SceneManagerGameObject etc.
//Invoke that function only when the game Starts
void Start ()
{
textureNr = Random.Range (0, (int)maxTextures);
switch (textureNr)
{
case 0 :
backgroundTexture = Resources.Load("LevelOne") as Texture2D;
break;
case 1 :
backgroundTexture = Resources.Load("LevelTwo") as Texture2D;
break;
default :
break;
}
}