c# - Change Material Image/Texture from Sphere with Script -


i'm trying change image/texture sphere material code.

try in new script , add object component:

c#:

class materialsetter : monobehaviour{     public void setmaterial(material newmaterial)     {         renderer renderer = getcomponent<renderer>();         renderer.material = newmaterial;     } } 

you can call setmaterial other script, provided have reference target object. could, example, set material @ beginning of game adding start() function materialsetter class:

c#:

class materialsetter : monobehaviour{     public material startmaterial;      // ...     void start()     {         setmaterial(startmaterial);     } } 

now if want set material name, material needs in folder assets/resources. via resources.load, must located in folder. code this:

c#:

class materialsetter : monobehaviour{     public void setmaterial(string materialname)     {         material mat = resources.load(materialname, typeof(material)) material;         renderer renderer = getcomponent<renderer>();         renderer.material = newmaterial;     } } 

if want change texture used in material (assuming use unity standard shader) you'll have texture reference or resources, if want name:

c#:

public void settexture(string texturename) {     texture tex = resources.load(texturename, typeof(texture)) texture;     renderer renderer = getcomponent<renderer>();     renderer.material.settexture("_maintex", tex); } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -