Переключение текстур рендеров в Unity3d - PullRequest
0 голосов
/ 17 октября 2019

Я создаю систему настройки персонажа. У меня есть 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
}

1 Ответ

0 голосов
/ 17 октября 2019

вместо Renderer.sharedMaterial

Изменение sharedMaterial изменит внешний вид всех объектов с использованием этого материала, и измените настройки материала , которые также хранятся в проекте.

Не рекомендуется изменять материалы, возвращаемые sharedMaterial. Если вы хотите изменить материал рендерера, используйте material.

вместо этого используйте Renderer.material

Изменение material изменит материал только для этого объекта.

Если материал используется любыми другими средствами визуализации, он клонирует общий материал и начинает использовать его с этого момента.


Также проверьте свои циклы .. некоторые вещи там кажутся странными, например,

for (int j = 0; j < someTextureArray.Length; j++)
{
    if (j == currentTextureIndex)
    {
        someRenderer[i].material.mainTexture = someTextureArray[j];
    }
}

кажется просто очень сложным способом простого написания

someRenderer[i].material.mainTexture = someTextureArray[currentTextureIndex];

Для всех ваших методов переключения: обратите внимание, что самое простое решение для обхода положительного счетчика для индексов в массиве, которое вы можете просто сделать

currentIndex = (currentIndex + 1) % accordingArray.Length;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...