Reputation: 3306
I am using Unity 3D. I am instantiating two gameobjects from resources from one single mesh.
Here is my code for setting the textures:
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;
}
}
}
}
However it sets the textures of the last instantiated gameobject. Is it even possible to have different textures on multiple gameobjects instantiated from same mesh, and if so where is my mistake? How can i make it ?
This is the amount of materials WeaponGameObject
have:
Upvotes: 0
Views: 2503
Reputation: 125455
The only possible issue I see is the use of the sharedMaterials
property:
foreach(Material mat in WeaponGameObject.GetComponent<MeshRenderer>().sharedMaterials)
The sharedMaterials
is used to modify other instances of the material.
See doc:
Modifying any material in sharedMaterials will change the appearance of all objects using this material, and change material settings that are stored in the project too.
It looks like you should use the materials
property which will only modify the appearance of only that object.
foreach(Material mat in WeaponGameObject.GetComponent<MeshRenderer>().materials)
Upvotes: 1
Reputation: 15257
WeaponGameObject
is an unique object if instantiated properly. You have to use Instantiate()
to create new references to apply other textures.
In example :
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);
Upvotes: 0