possible renderer array?

Hi all,

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.

1 Like

I don’t normally use resources - do i just create resources folder from assets and then drop the materials in there?

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

cameraBGOne.renderer.sharedMaterial.SetTexture(m_LevelOne);

Something like that anyway, not tried in unity so syntax might be off a bit.

there are more optimisations to be had there, but that’s the basic idea of what I think you want to do and what Andres said

1 Like

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;
   }
}
1 Like

Thanks for the helpful advice guys, I will code it in and get back to you on how it effects the game.

cheers.:slight_smile: