简体   繁体   English

如何在Unity中更改游戏对象的纹理?

[英]How to change texture of game object in Unity?

I'm working on Unity for a School Project and I am using Kinect to recognize facial expressions. 我正在为Unity学校项目工作,我正在使用Kinect来识别面部表情。 The "default" expression it shows is Neutral and it changes as I change facial expression to Happy or Suprised, etc. 它显示的“默认”表达是中性的,它会随着我将面部表情改为Happy或Suprised等而改变。

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. 我创建了一个名为Face的游戏对象,我设置了不同的纹理(PC显示的面部以响应我的表情),我希望他在我改变面部表情时改变。 But for some reason it is not working. 但由于某种原因它不起作用。

I am using C# and first I set this: 我正在使用C#,首先我设置了这个:

public static Texture[] textures = new Texture[7];
public Texture neutral, smiling, happy, angry, sad, kissing, surprised;
public GameObject Face;
public Renderer rend;

In the Start I 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 the way, it didn't find Face so I put it there by the inspector and I did the same with the textures/faces. 顺便说一句,它没有找到Face所以我把它放在检查员那里,我对纹理/面孔做了同样的事情。 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];

Try executing Face.GetComponent<Renderer>().material.mainTexture = textures[0]; 尝试执行Face.GetComponent<Renderer>().material.mainTexture = textures[0]; without the huge if structure, to be 100% sure it is not working. 没有巨大的if结构,100%确定它不起作用。 Are you sure you have all the textures drag-n-dropped in the inspector ? 您确定在检查器中拖放了所有纹理吗?

If that is the case then try creating new material (right-mouse-in-project-tab -> Create -> Material) and give it the Standard shader. 如果是这种情况,那么尝试创建新材料(右键在项目中选项卡 - >创建 - >材质)并为其指定Standard着色器。 Then give the material to your Face object. 然后将材质提供给Face对象。

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) 设置mainTexture仅在您的对象使用具有名为“MainTexture”参数的着色器的材质时才有效(某些简单的颜色或奇异的fx着色器可能没有用于纹理并且没有这样的参数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM