themes - Unity3d Sprite change with prefabs -
ive question how change spirte images during runtime bunch of objects.
so made tiny racer 2d game, , therefore can choose differend themes. have option in integraded menu (not seperate scene).
my question: can switch sprites easy during runtime? ive made prefabs each track element - , changed sprites of prefabs, change gets visible, after scene reloaded. need avoid this.
has solution or hint how that?
thanks in advance!
code:
public class background_controller : monobehaviour { public camera maincamera; public color colornormal; public gameobject[] prefabs; public sprite[] normalsprites; public sprite[] tronsprites; // use initialization void awake () { switchbackgroundfunction(); } public void switchbackground(string theme) { switch(theme) { case "greenhell": playerprefs.setstring("theme", "normal"); break; case "neoncity": playerprefs.setstring("theme", "tron"); break; } switchbackgroundfunction(); } private void switchbackgroundfunction() { int prefabcount = prefabs.length; if (playerprefs.getstring("theme") == "normal") { maincamera.backgroundcolor = colornormal; (int = 0; <= prefabcount - 1; i++) { prefabs[i].getcomponent<spriterenderer>().sprite = normalsprites[i]; } } if (playerprefs.getstring("theme") == "tron") { maincamera.backgroundcolor = color.black; (int = 0; <= prefabcount - 1; i++) { prefabs[i].getcomponent<spriterenderer>().sprite = tronsprites[i]; } } } // update called once per frame void update () { }
}
you can along following lines swap in sprite within resources folder during runtime.
sprite spr; spr = resources.load<sprite>("mysprite"); //insert name , file path sprite within resources folder getcomponent<spriterenderer>().sprite = spr;
wiki
Comments
Post a Comment