如何在Unity中更改游戏对象的纹理? [英] How to change texture of game object in Unity?
问题描述
我正在为学校项目开发Unity,并且正在使用Kinect识别面部表情.它显示的默认"表情为中性,并且随着我将表情更改为开心"或惊讶"等而改变.
I'm working on Unity for a School Project and I am using Kinect to recognize facial expressions. The "default" expression it shows is Neutral and it changes as I change facial expression to Happy or Suprised, etc.
我创建了一个名为Face的游戏对象,在其中设置了不同的纹理(PC根据我的表情显示的面孔),并且我希望他在更改面部表情时进行更改.但是由于某种原因,它无法正常工作.
I created a game object called Face where I set the different textures (faces that the PC shows in response to my expression) and I wanted him to change when I change my facial expression. But for some reason it is not working.
我正在使用C#,首先我要设置它:
I am using C# and first I set this:
public static Texture[] textures = new Texture[7];
public Texture neutral, smiling, happy, angry, sad, kissing, surprised;
public GameObject Face;
public Renderer rend;
在开始时我有这个:
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;
顺便说一句,它没有找到Face,所以我由检查员将其放置在这里,并且对纹理/面部进行了相同的处理. 然后在更新中,我将其定义如下:
By the way, it didn't find Face so I put it there by the inspector and I did the same with the textures/faces. Then in the Update I put this which is 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 but I think my problem is this but I am not sure.
Face. GetComponent<Renderer>().material.mainTexture = textures[0];
推荐答案
尝试在没有庞大的if
结构的情况下执行Face.GetComponent<Renderer>().material.mainTexture = textures[0];
,以确保100%无法正常运行.确定要在检查器中拖放所有纹理吗?
Try executing Face.GetComponent<Renderer>().material.mainTexture = textures[0];
without the huge if
structure, to be 100% sure it is not working. Are you sure you have all the textures drag-n-dropped in the inspector ?
如果是这种情况,请尝试创建新材质(右键单击项目中的鼠标"选项卡->创建"->材质"),并为其指定Standard
着色器.然后将材质提供给您的Face
对象.
If that is the case then try creating new material (right-mouse-in-project-tab -> Create -> Material) and give it the Standard
shader. Then give the material to your Face
object.
设置mainTexture
仅在您的对象使用带有名为"MainTexture"的参数的着色器的材质时有效(某些简单的颜色或奇异的fx着色器可能不用于纹理,并且没有这样的参数)
Setting mainTexture
only works if your object is using material with shader that has a parameter named "MainTexture" (some simple color or exotic fx shaders might not have use for texture and and have no such parameter)
这篇关于如何在Unity中更改游戏对象的纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!