如何在同一网格上设置多个纹理

时间:2018-09-17 07:12:51

标签: c# unity3d

我正在使用Unity 3D。我正在从一个网格中的资源实例化两个游戏对象。

这是我设置纹理的代码:

public static void EquipItem(Dictionary<string, object> permissions)
{
    if (permissions.Count == 0)
        return;

    ItemsTemplate iTemplate = ItemsHandler.itemsTemplate.Find(x => x.id == Convert.ToInt32(permissions["itemId"]));
    SlotType slotType = iTemplate.slot_type;
    CharacterData charData = Character.characterDetails[permissions["characterId"].ToString()];

    if(Convert.ToBoolean(permissions["allowedToEquip"]) == false)
    {
        if(Convert.ToInt32(permissions["characterId"]) == Character.characterId)
            Character.RecursiveFindChild(GameObject.Find("UI/Window (Character)/Content/Character Content/Equip Slots/").transform, slotType.ToString()).GetComponent<UIEquipSlot>().Unassign();
    }
    else
    {
        if(iTemplate.weapon_type == WeaponType.NotAWeapon){

        //Record added in clients ram about equiped items.
        Character.characterDetails[permissions["characterId"].ToString()].characterEquipedItems.Add(
        new CharacterEquipedItems(
            slotType,
            iTemplate
        ));

        //Applying visual appearence on the character
        DynamicCharacterAvatar charAv = GameObject.Find("CharactersOnline/"+ permissions["characterId"]).GetComponent<DynamicCharacterAvatar>();
        UMAWardrobeRecipe wardrobeRecipe = Resources.Load("Wardrobe/" + iTemplate.model) as UMAWardrobeRecipe;

        charAv.SetSlot(wardrobeRecipe);
        charAv.BuildCharacter();

        Dictionary<string, UMATextRecipe> allEquipedWardrobeRecipes = charAv.WardrobeRecipes;

        //To do - Add items stats to character stats.
        } else {
            GameObject CharacterGameObject = GameObject.Find("CharactersOnline/"+ permissions["characterId"]);
            //Loads the Weapon gameobject.
            GameObject WeaponGameObject = Instantiate(Resources.Load("Wardrobe/Weapons/" + iTemplate.model)) as GameObject;
            if (iTemplate.texture != null)
            {
                foreach(Material mat in WeaponGameObject.GetComponent<MeshRenderer>().sharedMaterials)
                {
                    mat.color = Color.white;
                    mat.SetTexture("_MainTex", Resources.Load("Wardrobe/Weapons/" + iTemplate.texture + "/" + iTemplate.model + "_" + mat.name + "_AlbedoTransparency") as Texture);
                    mat.SetTexture("_MetallicGlossMap", Resources.Load("Wardrobe/Weapons/" + iTemplate.texture + "/" + iTemplate.model + "_" + mat.name + "_MetallicSmoothness") as Texture);
                    mat.SetTexture("_BumpMap", Resources.Load("Wardrobe/Weapons/" + iTemplate.texture + "/" + iTemplate.model + "_" + mat.name + "_Normal") as Texture);
                    mat.SetTexture("_OcclusionMap", Resources.Load("Wardrobe/Weapons/" + iTemplate.texture + "/" + iTemplate.model + "_" + mat.name + "_AO") as Texture);
                }                   
            }

            //Search for the holders in the character
            GameObject MainHand = Character.RecursiveFindChild(CharacterGameObject.transform, "mixamorig:RightHandIndex2");
            GameObject OffHand = Character.RecursiveFindChild(CharacterGameObject.transform, "mixamorig:LeftHandIndex2");
            switch (iTemplate.slot_type)
            {
                case SlotType.MainHand:
                    GameObject MainHandWeapon = Instantiate(WeaponGameObject, MainHand.transform);
                    MainHandWeapon.transform.localPosition = new Vector3(0f,0.05f,0f);
                    MainHandWeapon.transform.localRotation = Quaternion.Euler(new Vector3(-45f, 0f, -70f));
                    MainHandWeapon.transform.localScale = Character.RaceEquipItemSizeModifier(charData.characterRace, charData.characterGender, MainHandWeapon.transform);
                    break;
                case SlotType.OffHand:
                    GameObject OffHandWeapon = Instantiate(WeaponGameObject, OffHand.transform);
                    OffHandWeapon.transform.localPosition = new Vector3(0f,0f,0.05f);
                    OffHandWeapon.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 70f));
                    OffHandWeapon.transform.localScale = Character.RaceEquipItemSizeModifier(charData.characterRace, charData.characterGender, OffHandWeapon.transform);
                    break;
                case SlotType.Shield:                        
                    GameObject Shield = Instantiate(WeaponGameObject, OffHand.transform);
                    //Shield.transform.localRotation = Quaternion.Euler(new Vector3(180f, -60f, 0f));
                    //Shield.transform.localScale += Character.RaceEquipItemSizeModifier(charData.characterRace, charData.characterGender, CharacterGameObject.transform);
                    break;
                default:
                    break;
            }           
        }

    }
}

但是,它设置了最后实例化的游戏对象的纹理。从同一个网格实例化的多个游戏对象上甚至可能具有不同的纹理,如果是这样,我的错误在哪里?我该怎么做?

这是WeaponGameObject拥有的材料数量:

enter image description here

2 个答案:

答案 0 :(得分:1)

我看到的唯一可能的问题是使用sharedMaterials属性:

foreach(Material mat in WeaponGameObject.GetComponent<MeshRenderer>().sharedMaterials)

sharedMaterials用于修改材料的其他实例。

请参见doc

  

修改sharedMaterials中的任何材料都会改变外观   使用该材质的所有对象,并更改材质设置   也存储在项目中。

您似乎应该使用materials属性,该属性只会修改该对象的外观。

foreach(Material mat in WeaponGameObject.GetComponent<MeshRenderer>().materials)

答案 1 :(得分:0)

如果正确实例化,

WeaponGameObject是唯一的对象。您必须使用Instantiate()创建新的引用才能应用其他纹理。

例如:

public void applyTexture(string texture, GameObject WeaponGameObject)
{
    foreach(Material mat in WeaponGameObject.GetComponent<MeshRenderer>().sharedMaterials)
    {
        mat.color = Color.white;
        mat.SetTexture("_MainTex", Resources.Load("Wardrobe/Weapons/" + texture + "_" + mat.name + "_AlbedoTransparency") as Texture);
        mat.SetTexture("_MetallicGlossMap", Resources.Load("Wardrobe/Weapons/" + texture + "_MetallicSmoothness") as Texture);
        mat.SetTexture("_BumpMap", Resources.Load("Wardrobe/Weapons/" + texture + "_" + mat.name + "_Normal") as Texture);
        mat.SetTexture("_OcclusionMap", Resources.Load("Wardrobe/Weapons/" + texture + "_" + mat.name + "_AO") as Texture);
    }
}

GameObject WeaponGameObject = Instantiate(Resources.Load("Wardrobe/Weapons/" + iTemplate.model)) as GameObject;
GameObject WeaponGameObject2 = Instantiate(Resources.Load("Wardrobe/Weapons/" + iTemplate.model)) as GameObject;
GameObject WeaponGameObject3 = Instantiate(Resources.Load("Wardrobe/Weapons/" + iTemplate.model)) as GameObject;

if (iTemplate.texture != null)
    applyTexture(iTemplate.texture + "/" + iTemplate.model, WeaponGameObject);
if (iTemplate2.texture != null)
    applyTexture(iTemplate2.texture + "/" + iTemplate2.model, WeaponGameObject2);
if (iTemplate3.texture != null)
    applyTexture(iTemplate3.texture + "/" + iTemplate3.model, WeaponGameObject3);