Swapping materials efficiently at runtime

Hey guys,

I’m trying to allow two options for a ‘skin’ on an object, and I want to swap them during runtime.

I’m troubled by this…

The ‘object’ I’m skinning is built of a few parts, and if I set this per piece, this could explode ram consumption.

So, is there a way I can swap the material without blowing ram away, or without expensive cloned objects, etc / so forth?

Oh, and a slight delay in loading/dumping of assets is acceptable as this all occurs in a pop-up panel.

Look for sharedMaterial property, it will change the shared material itself.

  1. how many of these objects are there? more than one instance of that object or not?

  2. are you talking about switching the material or switching the texture used in the material? the latter will cause the material to be cloned (a new material to be created - using more ram) if I understand correctly.

therefore, am I right in thinking switching the material, i.e. assigning a different material rather than changing the texture used in the material would be a better option?

what would be the best way of doing that? I don’t see a setMaterial command…

maybe loading the material from the resources folder and using "renderer.material = " ?

Yes would be nice, but…how !?
I couldn’t find documentation on how to do that.

I can create my brand new material by scripting,
but I don’t know how to load an existing one from file !

Please help!

sharedMaterial works great

all my effects share one texture atlas, and one material, I ping pong the alpha of this shared material as simulating glow. this makes all effects on screen to glow in one swoop

Material yourMaterial = (Material)Resources.Load("MaterialName", typeof(Material));

yourObject.renderer.sharedMaterial = yourMaterial;

I am newbie to unity please help me out form this, i am trying to make a unlimited 2d runner game and i have 5 environments to swap as per the user selection, on the play button event i am calling the “SetTheBackground()” method out of 9 bgs only one got effected. (may be the problem is in single frame it has been called). if i kept the folowwing code in update method it’s working but fps issue.

we should not call in every frame. please help me out.

using UnityEngine;
using System.Collections;

public class BackgroundManager : MonoBehaviour {
public static BackgroundManager instance;

GameObject mgameObject;
public Material[ ] bgmaterial;

void Awake()
{
mgameObject = this.gameObject;
instance = this;
}

public void SetTheBackground()
{
if (GameController.instance != null) {
switch (GameController.instance._ThemeType) {
case eTheme.Theme1:
mgameObject.renderer.material= bgmaterial [0];
break;
case eTheme.Theme2:
mgameObject.renderer.material = bgmaterial [1];
break;
case eTheme.Theme3:
mgameObject.renderer.material = bgmaterial [2];
break;
case eTheme.Theme4:
mgameObject.renderer.material = bgmaterial [3];
break;
case eTheme.Theme5:
mgameObject.renderer.material = bgmaterial [4];
break;
default:
break;

}

}
else {

}

}
Thanks in advance!