示例#1
0
    public static void CreateCharacter(CharacterAppearanceObject appearance, CharacterBehaviorObject behaviour, ItemObject leftHandItem, ItemObject rightHandItem, AccessoryObject[] accessories, Vector3 offset, bool keepCharacterPrefabConnection = false, bool keepItemPrefabConnection = true)
    {
        if (EditorApplication.isPlaying)
        {
            if (keepCharacterPrefabConnection)
            {
                keepCharacterPrefabConnection = false;
                Debug.LogError("Character Maker: keepCharacterPrefabConnection is not supported in playmode");
            }

            if (keepItemPrefabConnection)
            {
                keepItemPrefabConnection = false;
                Debug.LogError("Character Maker: keepItemPrefabConnection is not supported in playmode");
            }
        }

        GameObject character = Instantiate(appearance.Model, offset, Quaternion.identity, keepCharacterPrefabConnection);

        CapsuleCollider collider = character.AddComponent <CapsuleCollider>();

        collider.radius    = 0.1f;
        collider.direction = 1;
        collider.center    = new Vector3(0.0f, 0.5f, 0.0f);

        Rigidbody rigidbody = character.AddComponent <Rigidbody>();

        rigidbody.angularDrag = 5.0f;
        rigidbody.constraints = RigidbodyConstraints.FreezeRotation;

        if (behaviour != null)
        {
            behaviour.InitializeBehaviour(character);
        }

        ItemHoldLogic      itemHoldLogic      = character.GetComponent <ItemHoldLogic>();
        AccessoryWearLogic accessoryWearLogic = character.GetComponent <AccessoryWearLogic>();
        bool hasItems = leftHandItem || rightHandItem;

        if ((itemHoldLogic == null) && (leftHandItem || rightHandItem))
        {
            itemHoldLogic = character.AddComponent <ItemHoldLogic>();
            hasItems      = true;
        }

        if (itemHoldLogic != null)
        {
            itemHoldLogic.Initialize(character);
        }

        if (accessoryWearLogic == null)
        {
            if ((leftHandItem != null && leftHandItem.Item != null && leftHandItem.Item.AccessoryLogic != null) ||
                (rightHandItem != null && rightHandItem.Item != null && rightHandItem.Item.AccessoryLogic != null) ||
                (accessories != null && accessories.Length > 0))
            {
                accessoryWearLogic = character.AddComponent <AccessoryWearLogic>();
            }
        }

        // Attach items
        if (hasItems)
        {
            CharacterItemAnimator itemAnimator;
            itemAnimator          = itemHoldLogic.gameObject.AddComponent <CharacterItemAnimator>();
            itemAnimator.ThisHand = CharacterItemAnimator.Hand.Left;

            itemAnimator          = itemHoldLogic.gameObject.AddComponent <CharacterItemAnimator>();
            itemAnimator.ThisHand = CharacterItemAnimator.Hand.Right;
        }

        AttachItem(character, accessoryWearLogic, leftHandItem, itemHoldLogic, CharacterItemAnimator.Hand.Left, keepItemPrefabConnection);
        AttachItem(character, accessoryWearLogic, rightHandItem, itemHoldLogic, CharacterItemAnimator.Hand.Right, keepItemPrefabConnection);

        if (accessories != null)
        {
            // Attach accessories
            foreach (AccessoryObject a in accessories)
            {
                if (a == null)
                {
                    continue;
                }
                AccessoryLogic accessory = Instantiate(a.Accessory, keepItemPrefabConnection);
                accessory.transform.parent = character.transform;
                accessoryWearLogic.Attach(accessory);
            }
        }
    }