Exemple #1
0
    public void Attach(AccessoryLogic accessory)
    {
        if (!m_initialized)
        {
            Initialize(gameObject);
            if (!m_initialized)
            {
                Debug.LogWarning("AccessoryWearLogic not initialized correctly, can't attach accessories.");
                return;
            }
        }
        else if (accessory == null)
        {
            Debug.LogWarning("Trying to attach null accessory.");
            return;
        }
        else if (accessory.Renderer == null)
        {
            Debug.LogWarning("Trying to attach accessory with missing renderer.");
            return;
        }
        else if (accessory.Renderer.rootBone == null)
        {
            Debug.LogWarning("Trying to attach accessory with missing root bone.");
            return;
        }

        Transform[] newBones = GetBones(accessory.Renderer.bones, m_characterBones);
        if (newBones == null)
        {
            Debug.LogWarning("Trying to attach accessory with incompatible rig.");
            return;
        }

        accessory.Renderer.bones    = newBones;
        accessory.Renderer.rootBone = m_characterRenderer.rootBone;
    }
    private void OnWizardCreate()
    {
        GameObject character = Instantiate(m_appearanceObject.Model);

        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;

        m_behaviorObject.InitializeBehaviour(character);

        character.transform.position = Vector3.zero;

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

        if ((itemHoldLogic == null) &&
            (m_itemLeftHand || m_itemRightHand))
        {
            itemHoldLogic = character.AddComponent <ItemHoldLogic>();
            hasItems      = true;
        }
        if (accessoryWearLogic == null)
        {
            if (m_itemLeftHand != null && m_itemLeftHand.Item.AccessoryLogic != null)
            {
                accessoryWearLogic = character.AddComponent <AccessoryWearLogic>();
            }
            else if (m_itemRightHand != null && m_itemRightHand.Item.AccessoryLogic != null)
            {
                accessoryWearLogic = character.AddComponent <AccessoryWearLogic>();
            }
            else if (m_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;
        }

        if (m_itemLeftHand)
        {
            ItemLogic item = Instantiate(m_itemLeftHand.Item);
            itemHoldLogic.m_itemInHandL = item;

            if (m_itemLeftHand.Item.AccessoryLogic)
            {
                item.transform.parent = character.transform;
                accessoryWearLogic.Attach(item.AccessoryLogic);
            }
            else
            {
                item.transform.parent        = itemHoldLogic.HandBoneL;
                item.transform.localPosition = Vector3.zero;
                item.transform.localRotation = Quaternion.identity;
            }
        }

        if (m_itemRightHand)
        {
            ItemLogic item = Instantiate(m_itemRightHand.Item);
            itemHoldLogic.m_itemInHandR = item;

            if (m_itemRightHand.Item.AccessoryLogic)
            {
                item.transform.parent = character.transform;
                accessoryWearLogic.Attach(item.AccessoryLogic);
            }
            else
            {
                item.transform.parent        = itemHoldLogic.HandBoneR;
                item.transform.localPosition = Vector3.zero;
                item.transform.localRotation = Quaternion.identity;
            }
        }

        // Attach accessories
        foreach (AccessoryObject a in m_accessories)
        {
            AccessoryLogic accessory = Instantiate(a.Accessory);
            accessory.transform.parent = character.transform;
            accessoryWearLogic.Attach(accessory);
        }
    }
    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);
            }
        }
    }