c# - How to change texture of game object in Unity? -
i'm working on unity school project , using kinect recognize facial expressions. "default" expression shows neutral , changes change facial expression happy or suprised, etc.
i created game object called face set different textures (faces pc shows in response expression) , wanted him change when change facial expression. reason not working.
i using c# , first set this:
public static texture[] textures = new texture[7]; public texture neutral, smiling, happy, angry, sad, kissing, surprised; public gameobject face; public renderer rend;
in start have this:
face = gameobject.find ("face"); textures[0] = neutral; textures[1] = smiling; textures[2] = happy; textures[3] = angry; textures[4] = sad; textures[5] = sad; textures[6] = surprised;
by way, didn't find face put there inspector , did same textures/faces. in update put defined below:
classifyandapply(numbers); private void saveanimunits() { numbers[0] = _animunits.lipraiser; numbers[1] = _animunits.jawlowerer; numbers[2] = _animunits.lipstretcher; numbers[3] = _animunits.browlowerer; numbers[4] = _animunits.lipcornerdepressor; numbers[5] = _animunits.outerbrowraiser; } private void classifyandapply(float[] units){ // renderer rend = getcomponent<renderer>(); // face = gameobject.find ("face"); if (units[2] <= 0.264888){ if (units[3] <= 0.817408){ if (units[1] <= 0.181886){ if (units[0] <= -0.216908){ if (units[4] <= 0.395523){ if (units[1] <= 0.104226){ face. getcomponent<renderer>().material.maintexture = textures[3]; } else{ face. getcomponent<renderer>().material.maintexture = textures[0]; }
this tree continues think problem not sure.
face. getcomponent<renderer>().material.maintexture = textures[0];
try executing face.getcomponent<renderer>().material.maintexture = textures[0];
without huge if
structure, 100% sure not working. sure have textures drag-n-dropped in inspector ?
if case try creating new material (right-mouse-in-project-tab -> create -> material) , give standard
shader. give material face
object.
setting maintexture
works if object using material shader has parameter named "maintexture" (some simple color or exotic fx shaders might not have use texture , and have no such parameter)
Comments
Post a Comment