예제 #1
0
        public Item LoadItem(int itemId, ItemType itemType)
        {
            try {
                if (itemId == 0)
                {
                    return(null);
                }
                GameObject loadedItem   = null;
                object     loadedObject = Resources.Load("CustomizableCharacters/Race " + race + "/" + itemType.ToString() + "/" + itemId, typeof(GameObject));
                if (loadedObject == null)
                {
                    Debug.LogError(gameObject.name + " Failed to load " + itemType.ToString() + " item " + itemId + ". Make sure you are using the correct item ID!");
                    return(null);
                }
                loadedItem = GameObject.Instantiate((GameObject)loadedObject) as GameObject;

                Item          item       = loadedItem.GetComponent <Item>();
                EquipmentSlot slot       = GetEquipmentSlot(item.equipmentSlot);
                GameObject    itemObject = null;
                item.male.SetActive(false);
                item.female.SetActive(false);

                if (gender == 0)
                {
                    itemObject = item.male;
                    GameObject.Destroy(item.female); // not needed -> destroy
                }
                else
                {
                    itemObject = item.female;
                    GameObject.Destroy(item.male); // not needed -> destroy
                }
                itemObject.SetActive(true);
                loadedItem.name =
                    (gender == 0 ? "M" : "F") + " " +
                    (item.equipmentSlot == Item.EquipmentSlots.none ? "BodyPart" : item.equipmentSlot.ToString()) + " "
                    + itemId; // give the gameobject a more descriptive name

                if (item.skinned)
                {
                    SkinnedMeshRenderer skinnedRenderer = itemObject.GetComponentInChildren <SkinnedMeshRenderer>();
                    skinnedRenderer.bones = GetReferenceMesh().bones;
                    skinnedRenderer.updateWhenOffscreen = true;
                    if (itemType == ItemType.Equipment)
                    {
                        item.transform.SetParent(equippedItemsParent);
                    }
                    else
                    {
                        item.transform.SetParent(bodyPartsParent);
                    }


                    foreach (Transform t in skinnedRenderer.GetComponentsInParent <Transform>()[1].GetComponentInChildren <Transform>()) // destroy unnecessary gameobjects
                    {
                        if (t.GetComponent <SkinnedMeshRenderer>() == null)
                        {
                            GameObject.Destroy(t.gameObject);
                        }
                    }
                }
                else
                {
                    item.transform.SetParent(slot.container);
                }


                loadedItem.transform.localPosition = Vector3.zero;
                loadedItem.transform.localScale    = new Vector3(1, 1, 1);
                loadedItem.transform.localRotation = Quaternion.identity;



                if (slot != null)
                {
                    if (slot.inUse)   // replace current item, if you need to remove stats of the previous item, do it here (e.g. character health, damage)
                    {
                        if (loadAllItemsMode == false)
                        {
                            GameObject.Destroy(slot.instancedObject);
                        }
                    }
                    slot.item            = item;
                    slot.itemId          = itemId;
                    slot.inUse           = true;
                    slot.instancedObject = loadedItem;
                    slot.activeObject    = itemObject.gameObject;
                }

                if (itemType == ItemType.BodyParts)
                {
                    bodyParts.Add(loadedItem);
                }
                GetReferenceMesh().enabled = false;
                if (loadAllItemsMode == false)
                {
                    SetItemColor(itemObject.gameObject);
                }
                return(item);
            } catch (Exception e) {
                Debug.LogError(gameObject.name + " Failed to load " + itemType.ToString() + " item " + itemId + " (" + e.ToString() + ")");
                return(null);
            }
        }