Question by
hakansahin · Sep 28, 2016 at 09:52 PM ·
gameobjectiosmaterialsruntimebitmap
Change Material of an GameObject at Runtime using a bitmap dynamically created
I would like to change material of a gameobject with a bitmap dynamically created at runtime or a bitmap read from a database. How can I do this? Thanks.
Comment
Best Answer
Answer by TBruce · Sep 28, 2016 at 11:00 PM
Lets say you have two variables (I will make them public so they can be set through the inspector) like this
public Sprite sprite;
public Material material;
You can then set the main texture of the material using the following code
if ((sprite != null) && (material != null))
{
material.SetTexture("_MainTex", sprite);
}
material.SetTexture("_$$anonymous$$ainTex", sprite); I received following error: Error CS1503 Argument 2: cannot convert from 'UnityEngine.Sprite' to 'UnityEngine.Texture' $$anonymous$$ahveTest1.CSharp
I am sorry, that should have been
material.SetTexture("_$$anonymous$$ainTex", sprite.texture);
I used this script, there's no error but nothing happen at runtime what's wrong here?
public GameObject[] brixk;
public $$anonymous$$aterial mat;
public Texture text$$anonymous$$ap;
void start()
{
foreach(GameObject brick in brixk)
{
mat.SetTexture("_$$anonymous$$ainTex", text$$anonymous$$ap);
}
}