Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to create an Avatar Mask for Custom GameObject hierarchy from scene ?

Discussion in 'Animation' started by JustAnotherDude, Oct 26, 2018.

  1. JustAnotherDude

    JustAnotherDude

    Joined:
    Oct 28, 2013
    Posts:
    279
    I want to create Avatar Masks for UI elements to later use Unity's Animation Controller to control the various animations on screen.

    Let's say I have 3 images on screen, a red, a blue and a green one, and I want them to slide in/out from the screen based on some state.

    I added an Animator to the root Canvas and created 3 animation clips for each of the images, all is fine.

    On the Animator Controller I added 3 Layers, one for each image, and each layer plays the correct animation for each Image.

    However there are some issues, I can see that whatever image is below the first layer doesn't play the animation properly, and I think that's because I don't have masks for these layers.

    So I wanted to created masks for each image so that I can play all animations properly.

    The problem is that searching for a solution brought me to this page :
    https://docs.unity3d.com/ScriptReference/AvatarBuilder.BuildGenericAvatar.html

    I used that code to create an Avatar :


    Code (CSharp):
    1. GameObject activeGameObject = Selection.activeGameObject;
    2.  
    3.         if (activeGameObject != null &&
    4.             activeGameObject.GetComponent<Animator>() != null)
    5.         {
    6.             Avatar avatar = AvatarBuilder.BuildGenericAvatar(activeGameObject, "test");
    7.  
    8.             avatar.name = "InsertYourName";
    9.  
    10.             Debug.Log(avatar.isValid ? "is valid" : "error");
    11.             Debug.Log(avatar.isHuman ? "is human" : "is generic");
    12.  
    13.             AssetDatabase.CreateAsset(avatar, "Assets/avatarcanvas.mask");
    14.         }
    Then I created an mask in the assets (right clicking and creating one), however when I try to assign that avatar to the mask I created, it doesn't work, nothing happens, and when I click outside the mask then select it again, the avatar I put on it was gone...
     
  2. austintaylorx

    austintaylorx

    Joined:
    Aug 13, 2018
    Posts:
    8
    If you still need it, this code makes it so I can generate an avatar and avatar mask for just about anything.
    Let me know if it works.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. namespace Infrastructure.Editor
    5. {
    6.     public class AvatarMaker
    7.     {
    8.         [MenuItem("CustomTools/MakeAvatarMask")]
    9.         private static void MakeAvatarMask()
    10.         {
    11.             GameObject activeGameObject = Selection.activeGameObject;
    12.  
    13.             if (activeGameObject != null)
    14.             {
    15.                 AvatarMask avatarMask = new AvatarMask();
    16.  
    17.                 avatarMask.AddTransformPath(activeGameObject.transform);
    18.  
    19.                 var path = string.Format("Assets/{0}.mask", activeGameObject.name.Replace(':', '_'));
    20.                 AssetDatabase.CreateAsset(avatarMask, path);
    21.             }
    22.         }
    23.  
    24.         [MenuItem("CustomTools/MakeAvatar")]
    25.         private static void MakeAvatar()
    26.         {
    27.             GameObject activeGameObject = Selection.activeGameObject;
    28.  
    29.             if (activeGameObject != null)
    30.             {
    31.                 Avatar avatar = AvatarBuilder.BuildGenericAvatar(activeGameObject, "");
    32.                 avatar.name = activeGameObject.name;
    33.                 Debug.Log(avatar.isHuman ? "is human" : "is generic");
    34.  
    35.                 var path = string.Format("Assets/{0}.ht", avatar.name.Replace(':', '_'));
    36.                 AssetDatabase.CreateAsset(avatar, path);
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    42.  
     
    tantx, nukadelic, mzlafty and 4 others like this.
  3. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,973
    Just came across a situation where I needed a special avatar mask. Your script worked perfectly. Thank you! :)
     
  4. diegodelarocha

    diegodelarocha

    Joined:
    Nov 15, 2013
    Posts:
    1
    Been looking and breaking my head all day for a way to make this work! Thank you so much! <3
     
  5. mzlafty

    mzlafty

    Joined:
    Jan 20, 2020
    Posts:
    3
    This is perfect for ik bones from the animation rigging package. Thank you.