在Unity3d中切换渲染器的纹理

问题描述 投票:-1回答:1

我正在创建一个角色定制系统。我有2件具有自己2种纹理的衬衫。

当我换衬衫时,所有纹理都应用两个渲染器,而不是它们自己的两个

任何帮助将不胜感激。这是我的脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharCustomizationOne : MonoBehaviour
{
    public GameObject[] characters;

    public GameObject[] maleHairs;
    public GameObject[] maleShirts;
    public GameObject[] malePants;

    public GameObject[] femaleHairs;
    public GameObject[] femaleShirts;
    public GameObject[] femalePants;

    public Texture[] charTexturesM;
    public Texture[] charTexturesF;

    public Texture[] shirtTextureM;
    public Texture[] shirtTextureF;

    public Texture[] pantTextureM;
    public Texture[] pantTextureF;

    private int currentCharacters;

    private int currentMaleHair;
    private int currentMaleShirts;
    private int currentMalePants;

    private int currentFemaleHair;
    private int currentFemaleShirt;
    private int currentFemalePant;

    private int currentCharTextureM;
    private int currentCharTextureF;

    private int currentShirtTextureM;
    private int currentShirtTextureF;

    private int currentPantTextureM;
    private int currentPantTextureF;

    public Renderer rendererM;
    public Renderer rendererF;

    public Renderer[] rendererShirtM = new Renderer[2];
    public Renderer[] rendererShirtF = new Renderer[2];

    public Renderer[] rendererPantM = new Renderer[2];
    public Renderer[] rendererPantF = new Renderer[2];
    public void Update() {

        //Gender Loop
        for (int i = 0; i < characters.Length; i++)
        {
            if (i == currentCharacters)
            {
                characters[i].SetActive(true);
            }
            else
            {
                characters[i].SetActive(false);
            }

        }
        //Hair Loop
        if (currentCharacters == 0)
        {
            for (int i = 0; i < maleHairs.Length; i++)
            {
                if (i == currentMaleHair)
                {
                    femaleHairs[i].SetActive(false);
                    maleHairs[i].SetActive(true);
                }
                else
                {
                    femaleHairs[i].SetActive(false);
                    maleHairs[i].SetActive(false);
                }
            }
        }

        else
        {
            for (int i = 0; i < femaleHairs.Length; i++)
            {
                if (i == currentFemaleHair)
                {
                    maleHairs[i].SetActive(false);
                    femaleHairs[i].SetActive(true);
                }
                else
                {
                    maleHairs[i].SetActive(false);
                    femaleHairs[i].SetActive(false);
                }
            }
        }
        // Shirt Loop


        if (currentCharacters == 0)
        {
            for (int i = 0; i < maleShirts.Length; i++)
            {
                if (i == currentMaleShirts)
                {
                    femaleShirts[i].SetActive(false);
                    maleShirts[i].SetActive(true);
                }
                else
                {
                    femaleShirts[i].SetActive(false);
                    maleShirts[i].SetActive(false);
                }
            }
        }
        else
        {
            for (int i = 0; i < femaleShirts.Length; i++)
            {
                if (i == currentFemaleShirt)
                {
                    maleShirts[i].SetActive(false);
                    femaleShirts[i].SetActive(true);
                }
                else
                {
                    maleShirts[i].SetActive(false);
                    femaleShirts[i].SetActive(false);
                }
            }
        }



        // Pants Loop

        if (currentCharacters == 0)
        {
            for (int i = 0; i < malePants.Length; i++)
            {
                if (i == currentMalePants)
                {
                    femalePants[i].SetActive(false);
                    malePants[i].SetActive(true);
                }

                else
                {
                    femalePants[i].SetActive(false);
                    malePants[i].SetActive(false);
                }
            }
        }
        else
        {
            for (int i = 0; i < femalePants.Length; i++)
            {
                if (i == currentFemalePant)
                {
                    malePants[i].SetActive(false);
                    femalePants[i].SetActive(true);
                }
                else
                {
                    malePants[i].SetActive(false);
                    femalePants[i].SetActive(false);
                }
            }
        }

        //CharTexture Loop

        if (currentCharacters == 0)
        {
            for (int i = 0; i < charTexturesM.Length; i++)
            {
                if (i == currentCharTextureM)
                {
                    rendererM.sharedMaterial.mainTexture = charTexturesM[i];
                }
            }
        }
        else
        {
            for (int i = 0; i < charTexturesF.Length; i++)
            {
                if (i == currentCharTextureF)
                {
                    rendererF.sharedMaterial.mainTexture = charTexturesF[i];
                }
            }
        }

        //Shirt Texture Loop
        if (currentCharacters == 0)
        {
            for (int i = 0; i < maleShirts.Length; i++)
            {
                for (int j = 0; j < shirtTextureM.Length; j++)
                {
                    if (j == currentShirtTextureM)
                    {
                        rendererShirtM[i].sharedMaterial.mainTexture = shirtTextureM[j];
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i < femaleShirts.Length; i++)
            {
                for (int j = 0; j < shirtTextureF.Length; j++)
                {
                    if (j == currentShirtTextureF)
                    {
                        rendererShirtF[i].sharedMaterial.mainTexture = shirtTextureF[j];
                    }
                }
            }
        }

        //Pant Texture Loop

        if (currentCharacters == 0)
        {
            for (int i = 0; i < malePants.Length; i++)
            {
                for (int j = 0; j < pantTextureM.Length; j++)
                {
                    if (j == currentPantTextureM)
                    {
                        rendererPantM[i].sharedMaterial.mainTexture = pantTextureM[j];
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i < femalePants.Length; i++)
            {
                for (int j = 0; j < pantTextureF.Length; j++)
                {
                    if (j == currentPantTextureF)
                    {
                        rendererPantF[i].sharedMaterial.mainTexture = pantTextureF[j];
                    }
                }
            }
        }

    }     

    //Switching Characters
    public void SwitchCharacters() {
        Debug.Log("Method Called!");



        if (currentCharacters == characters.Length - 1)
        {
            currentCharacters = 0;
        }
        else
        {
            currentCharacters++;
        }

        }

    //Switching Hairs
    public void SwitchHairs() {
        Debug.Log("MaleHairsCalled");
        if (currentMaleHair == maleHairs.Length - 1)
        {
            currentMaleHair = 0;
        }
        else
        {
            currentMaleHair++;
        }



        if (currentFemaleHair == femaleHairs.Length - 1)
        {
            currentFemaleHair= 0;
        }
        else
        {
            currentFemaleHair++;
        }
    }

    //Switching Shirts
    public void SwitchShirts()
    {
        Debug.Log("MaleShirtCalled");
        if (currentMaleShirts == maleShirts.Length - 1)
        {
            currentMaleShirts = 0;
        }
        else
        {
            currentMaleShirts++;
        }

        if (currentFemaleShirt == femaleShirts.Length - 1)
        {
            currentFemaleShirt = 0;
        }
        else
        {
            currentFemaleShirt++;
        }
    }

    //Switching Pants
    public void SwitchPants() {
        Debug.Log("MalePantsCalled");
        if (currentMalePants == malePants.Length - 1)
        {
            currentMalePants = 0;
        }
        else
        {
            currentMalePants++;
        }

        if (currentFemalePant == femalePants.Length - 1)
        {
            currentFemalePant = 0;
        }
        else
        {
            currentFemalePant++;
        }
    }

    //Switching Character Textures
    public void SwitchCharTexture()
    {
        Debug.Log("Char Texture Method Was Called!");
        if (currentCharTextureM == charTexturesM.Length - 1)
        {
            currentCharTextureM = 0;
        }
        else
        {
            currentCharTextureM++;
        }

        if (currentCharTextureF == charTexturesF.Length - 1)
        {
            currentCharTextureF = 0;
        }
        else
        {
            currentCharTextureF++;
        }

    }

    public void SwitchShirtTexture()
    {
        Debug.Log("Shirt Texture Method Was Called!");
        if (currentShirtTextureM == shirtTextureM.Length - 1)
        {
            currentShirtTextureM = 0;
        }
        else
        {
            currentShirtTextureM++;
        }

        if (currentShirtTextureF == shirtTextureF.Length - 1)
        {
            currentShirtTextureF = 0;
        }
        else
        {
            currentShirtTextureF++;
        }
    }

    public void SwitchPantTexture()
    {
        Debug.Log("Pant Texture Method Was Called!");

        if (currentPantTextureM == pantTextureM.Length - 1)
        {
            currentPantTextureM = 0;
        }
        else
        {
            currentPantTextureM++;
        }

        if (currentPantTextureF == pantTextureF.Length - 1)
        {
            currentPantTextureF = 0;
        }
        else
        {
            currentPantTextureF++;
        }
    }
    //End of Class
}
unity3d character textures renderer
1个回答
0
投票

而不是Renderer.sharedMaterial

修改Renderer.sharedMaterial将使用此材质来更改所有对象的外观,并更改项目中也存储的材质设置

不建议修改sharedMaterial返回的资料。如果要修改渲染器的材质,请使用sharedMaterial

而是使用sharedMaterial

修改sharedMaterial仅会更改该对象的材质。

如果该材质被其他渲染器使用,则将克隆共享的材质并从现在开始使用。


还检查您的循环..有些事情看起来很奇怪,例如

material

似乎只是一种非常简单的书写方式

material

对于所有切换方法:请注意,可以简单地为数组中的索引环绕一个正计数器的最简单解决方案

Renderer.material
© www.soinside.com 2019 - 2024. All rights reserved.