【问题标题】:How to change texture of game object in Unity?如何在 Unity 中更改游戏对象的纹理?
【发布时间】:2015-07-13 21:25:19
【问题描述】:

我正在为一个学校项目开发 Unity,我正在使用 Kinect 来识别面部表情。它显示的“默认”表情是中性的,当我将面部表情更改为快乐或惊讶等时,它会发生变化。

我创建了一个名为 Face 的游戏对象,我在其中设置了不同的纹理(PC 根据我的表情显示的面孔),并且我希望当我改变我的面部表情时他也会改变。但由于某种原因,它不起作用。

我正在使用 C#,首先我设置了这个:

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 所以我把它放在检查员那里,我对纹理/面做了同样的事情。 然后在更新中我把它定义如下:

    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];
                            }

这棵树还在继续,但我认为我的问题是这样,但我不确定。

Face. GetComponent<Renderer>().material.mainTexture = textures[0];

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    尝试在没有巨大的if 结构的情况下执行Face.GetComponent&lt;Renderer&gt;().material.mainTexture = textures[0];,以100% 确定它不起作用。你确定你已经在检查器中拖放了所有纹理吗?

    如果是这种情况,请尝试创建新材质(右键鼠标在项目选项卡中 -> 创建 -> 材质)并为其指定 Standard 着色器。然后将材料交给您的Face 对象。

    设置 mainTexture 仅在您的对象使用具有名为“MainTexture”参数的着色器的材质时才有效(一些简单的颜色或奇异的 fx 着色器可能不适用于纹理并且没有此类参数)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多