//** RemoveProp ** //Remove a prop from actor public void RemoveProp(int idProp) { int propSlot = tableProps.GetRecord(idProp).IDSlot; //Get the name of prop prefab string modelName = tableProps.GetModelObjectName(idProp, actorBody.BodySubtype); //Get the name of slot (bone) where the prop is attached BodyStructureTable.BodyPropsSlots recTemp = tableBodyStructure.GetPropSlotRecord(propSlot, actorBody.BodyStructure); string slotName = ""; if (recTemp != null) { slotName = recTemp.ObjectName; } Transform destSlot = null; //Look for the slot (bone) in the skeleton structure Transform[] bones = actorBody.BonesArmature.GetComponentsInChildren <Transform> (); for (int i = 0; i < bones.Length; i++) { if (bones [i].name == slotName) { destSlot = bones [i]; i = bones.Length + 1; } } if (destSlot == null) { Debug.LogError("Prop slot bone cannot be found."); } //Remove prop in the slot if (idProp > 0) { Transform[] toRemove = actorBody.BonesArmature.GetComponentsInChildren <Transform> (); for (int i = 0; i < toRemove.Length; i++) { if (toRemove [i].name == modelName) { Destroy(toRemove [i].gameObject); i = toRemove.Length + 1; } } } //Remove prop from the props list propsList.RemoveAll(x => x.PropSlot == propSlot); }
//** AddProp ** //Append a prop on its designed bone //The method of adding prop is the same for both methods //because a prop is added to a bone public void AddProp(int idProp) { //Create new record to store the information about the prop ActorProp toAdd = new ActorProp(); toAdd.IDProp = idProp; //ID of the prop toAdd.PropSlot = tableProps.GetRecord(idProp).IDSlot; //Id of slot //GetType the name of prop prefab string modelName = tableProps.GetModelObjectName(idProp, actorBody.BodySubtype); //Get the name of slot (bone) where the prop need to be attached BodyStructureTable.BodyPropsSlots recTemp = tableBodyStructure.GetPropSlotRecord(toAdd.PropSlot, actorBody.BodyStructure); string slotName = ""; if (recTemp != null) { slotName = recTemp.ObjectName; } Transform destSlot = null; //Look for the slot (bone) in the skeleton structure Transform[] bones = actorBody.BonesArmature.GetComponentsInChildren <Transform> (); for (int i = 0; i < bones.Length; i++) { if (bones [i].name == slotName) { destSlot = bones [i]; i = bones.Length + 1; } } if (destSlot == null) { Debug.LogError("Prop slot bone cannot be found."); } //Get the name of prop that is using the slot int prevPropID = 0; ActorProp tRec = propsList.Find(x => x.PropSlot == toAdd.PropSlot); if (tRec != null) { prevPropID = tRec.IDProp; } //Remove previous prop in the slot. if it exists if (prevPropID > 0) { string prevPropName = tableProps.GetModelObjectName(prevPropID, actorBody.BodySubtype); Transform[] toRemove = actorBody.BonesArmature.GetComponentsInChildren <Transform> (); for (int i = 0; i < toRemove.Length; i++) { if (toRemove [i].name == prevPropName) { Destroy(toRemove [i].gameObject); i = toRemove.Length + 1; } } } //Remove the prop from the props list propsList.RemoveAll(x => x.PropSlot == toAdd.PropSlot); //Add the prop to props list propsList.Add(toAdd); //Assembly the full path of the prefab: folder and name string modelObjectName = _config.GetPathName(CSConfig.PathTypeEnum.PropPath) + "/" + modelName; //Load the prefab from the Resources folder GameObject tempObject = (GameObject)Resources.Load(modelObjectName); if (tempObject == null) { //Error loading the object Debug.LogError("Prop object cannot be found."); } //Create a new instance of the prefab GameObject propObject = (GameObject)Instantiate(tempObject); //Change the name of the new instantiated object, to remove the "(Clone)" //from the name, and make sure the name is the same as in the cloth database propObject.name = modelName; //Add the object to actor propObject.transform.SetParent(destSlot, false); }