umasteeringgroup / UMA

Unity Multipurpose Avatar
MIT License
726 stars 167 forks source link

Create and modify Avatar in play mode of Unity Editor #352

Closed QUAN-Zhao closed 3 years ago

QUAN-Zhao commented 3 years ago

I want to generate dataset of human faces to train Netural Networks. So I wrote scripts to capture snapshot of camera while enter play mode by using "EditorApplication.isPlaying = true;", now the DynamicCharacterAvatar doesn't render the character so I capture nothing... How should I dynamically create and modify Avatar of UMA in play mode of Unity Editor?

Now I use following codes to create GameObject and DynamicCharacterAvatar:

        // callback from EditorApplication.playModeStateChanged when enter play mode
        GameObject avatarObject = GameObject.Find("tmp_avatar");
        if (avatarObject == null)
            avatarObject = new GameObject("tmp_avatar");
        DynamicCharacterAvatar avatar = avatarObject.GetComponent<DynamicCharacterAvatar>();
        if (avatar == null)
            avatar = avatarObject.AddComponent<DynamicCharacterAvatar>();
        avatar.CreateEditorContext();
        avatar.ChangeRace("HumanMaleDCS");
        RuntimeAnimatorController controller = (RuntimeAnimatorController)UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/UMA/Examples/DynamicCharacterSystem Examples/Assets/DynamicDNA/Animation/UMA_TPose_Controller.controller");
        avatar.KeepAnimatorController = true;
        avatar.animationController = controller ;
        avatar.BuildCharacter();

But the character doesn't show in play mode of Unity Editor.