Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting up and using ChatdollKit from code without the prefab #285

Open
ahmetardal opened this issue Feb 13, 2024 · 3 comments
Open

Setting up and using ChatdollKit from code without the prefab #285

ahmetardal opened this issue Feb 13, 2024 · 3 comments

Comments

@ahmetardal
Copy link

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
Copy link
Owner

uezo commented Feb 14, 2024

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
Copy link
Owner

uezo commented Feb 14, 2024

image

@ahmetardal
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants