uezo/ChatdollKit

Setting up and using ChatdollKit from code without the prefab

Opened this issue · 3 comments

Hi.

Thanks for this great framework. It's a lot of work. uezo

We have successfully tested ChatdollKit in the Unity Editor but we're having problems while integrating it to our existing Unity app. In our use case we want to spawn ChatdollKit at runtime when we need it and we want to set and change the models at runtime also. Is there any sample code for this use case? For setting up ChatdollKit via C# code and not with a prefab in the editor?

Thanks.

uezo commented

Hi @ahmetardal ,
Thank you for very good question✨ And, I'm sorry for our poor documentations🙏

You can spawn the character all in runtime by following steps:

  1. Make configured inactive ChatdollKit prefab. Copy from scene DemoChatGPT is the easiest way I think.

  2. Make inactive Character prefab.

  3. Place object and buttons that manages spawn/destroy.

  4. Make script like below and attach it to the object you made in step3, and set SpawnCharacter and DestroyCharacter to OnClick() of the buttons.

using UnityEngine;
using ChatdollKit.Model;

namespace yourapp
{
    public class CharacterManager : MonoBehaviour
    {
        [SerializeField]
        private GameObject chatdollKitPrefab;
        [SerializeField]
        private GameObject characterPrefab;
        [SerializeField]
        private RuntimeAnimatorController animatorController;

        // NOTE: Multiple instances are not considered in this example
        private GameObject characterObject;
        private GameObject cdkObject;

        public void SpawnCharacter()
        {
            // NOTE:
            // - characterPrefab and chatdollKitPrefab should be INACTIVE when instantiate
            // - Components in chatdollKitPrefab should be configured

            // Instantiate character and set animator
            characterObject = Instantiate(characterPrefab);
            var animator = characterObject.GetComponent<Animator>();
            animator.runtimeAnimatorController = animatorController;

            // Instantiate ChatdollKit
            cdkObject = Instantiate(chatdollKitPrefab);

            // Set character to ModelController
            var modelController = cdkObject.GetComponent<ModelController>();
            modelController.AvatarModel = characterObject;

            // Blink
            cdkObject.GetComponent<IBlink>().Setup(characterObject);

            // Face expression
            cdkObject.GetComponent<IFaceExpressionProxy>().Setup(characterObject);

            // LipSync
            cdkObject.GetComponent<ILipSyncHelper>().ConfigureViseme(characterObject);

            // Start ChatdollKit
            cdkObject.SetActive(true);

            // Wait for a brief moment after ChatdollKit activation to show the character after the idle animation has started
            Invoke(nameof(ShowCharacter), 0.1f);
        }

        private void ShowCharacter()
        {
            characterObject.SetActive(true);
        } 

        public void DestroyCharacter()
        {
            characterObject.SetActive(false);
            cdkObject.SetActive(false);

            Destroy(cdkObject);
            Destroy(characterObject);
        }
    }
}
  1. Run. Buttons to spawn and destroy👍

I'm happy if you give me the feedback to this🥰 I will add a demo to our project.

uezo commented
image

Hi @uezo

Thank you for the quick and detailed reply. I appreciate it. 🙏🏻

We'll try the example you've sent and get back to you as soon as possible.