Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

possible renderer array?

Discussion in 'Scripting' started by AndyNeoman, Jan 19, 2015.

  1. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    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.
     
  2. AndresBarrera

    AndresBarrera

    Joined:
    Jan 8, 2015
    Posts:
    5
    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.
     
    AndyNeoman likes this.
  3. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    I don't normally use resources - do i just create resources folder from assets and then drop the materials in there?
     
  4. draxov

    draxov

    Joined:
    Jun 2, 2012
    Posts:
    26
    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:

    Code (CSharp):
    1. m_LevelOneTex = Resources.Load("LevelOne") as Texture2D;
    2. m_LevelTwoTex = Resources.Load("LevelTwo") as Texture2D;
    3. 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

    Code (CSharp):
    1. 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
     
    AndyNeoman likes this.
  5. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Hey, i would make it with "Random.Range" & "Switch Statement"

    Example:
    Code (CSharp):
    1. [HideInInspector]
    2. public Texture2D backgroundTexture = null;
    3.  
    4. int textureNr;
    5.  
    6. //Attack this Script to a SceneManagerGameObject etc.
    7. //Invoke that function only when the game Starts
    8. void Start ()
    9. {
    10.    textureNr = Random.Range (0, (int)maxTextures);
    11.  
    12.    switch (textureNr)
    13.    {
    14.    case 0 :
    15.       backgroundTexture = Resources.Load("LevelOne") as Texture2D;
    16.    break;
    17.  
    18.    case 1 :
    19.       backgroundTexture = Resources.Load("LevelTwo") as Texture2D;
    20.    break;
    21.  
    22.    default :
    23.    break;
    24.    }
    25. }
     
    AndyNeoman likes this.
  6. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Thanks for the helpful advice guys, I will code it in and get back to you on how it effects the game.

    cheers.:)