Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Preview Thumbnails of Animation in Editor

Discussion in 'Animation' started by BlankMauser, Nov 6, 2021.

  1. BlankMauser

    BlankMauser

    Joined:
    Dec 10, 2013
    Posts:
    138
    Trying to get animation thumbnail previews working in my editor window. I feel very close to getting it working. I am using the PreviewRenderUtility class, and its clearly updating my gameobject's rotations, but each thumbnail is not correctly sampling my animation using the new Playable system:



    Here is my code:


    Code (CSharp):
    1.  void CaptureThumbnail() {
    2.         if (chardata.Prefab.DefaultPrefab != null) {
    3.  
    4.             if (PreviewObject == null) {
    5.                 PreviewObject = (GameObject)Instantiate(chardata.Prefab.DefaultPrefab, Vector3.zero, Quaternion.identity);
    6.                 }
    7.  
    8.             KFightAnimator anim;
    9.                
    10.             if (!PreviewObject.TryGetComponent<KFightAnimator>(out anim)) {
    11.                 Debug.Log("No Animator Found");
    12.                 return;
    13.                 }
    14.  
    15.             int index = 0;
    16.  
    17.             anim.Init();
    18.  
    19.             foreach (BasicState s in chardata.Movesets.DefaultMoveset.BasicStates) {
    20.                 PreviewObject.transform.Rotate(new Vector3(0f, 90f, 0f));
    21.                 int i = anim.AddClip(s.Animation);
    22.  
    23.                 Animator ObjAnim = PreviewObject.GetComponent<Animator>();
    24.                 anim.ResetWeights();
    25.                 anim.SetTime(index, 5f);
    26.                 anim.SetWeight(index, 1f);
    27.                 Debug.Log(anim.GetAnimName(index));
    28.                 anim._graph.Play();
    29.                 anim._graph.Evaluate(0f);
    30.                 ObjAnim.Update(0);
    31.  
    32.  
    33.                 if (CachedThumbnails[index] == null) {
    34.                     CachePreview(PreviewObject, ref CachedThumbnails[index], index);
    35.                     } else {
    36.                     Debug.Log("taken");
    37.                     }
    38.                 GUI.DrawTexture(new Rect(250 * index, 10, 162, 288), CachedThumbnails[index], ScaleMode.StretchToFill, false);
    39.                 index++;
    40.                 Debug.Log(PreviewObject.transform.rotation);
    41.  
    42.  
    43.  
    44.                 }
    45.  
    46. public void CachePreview(GameObject go, ref Texture render, int index) {
    47.         m_PreviewUtility.BeginPreview(new Rect(250*index, 10, 162, 288), new GUIStyle(GUI.skin.box));
    48.         if (!PreviewSpawned) {
    49.             m_PreviewUtility.AddSingleGO(go);
    50.             }
    51.         Bounds bounds = new Bounds(go.transform.position, Vector3.zero);
    52.         GetRenderableBoundsRecurse(ref bounds, go);
    53.  
    54.         float halfSize = Mathf.Max(bounds.extents.magnitude, 0.0001f);
    55.         float distance = halfSize * 2.5f;
    56.  
    57.         Quaternion rot = Quaternion.Euler(0, 0, 0);
    58.         Vector3 pos = bounds.center - rot * (Vector3.forward * distance);
    59.  
    60.         m_PreviewUtility.camera.transform.position = pos;
    61.         m_PreviewUtility.camera.transform.rotation = rot;
    62.         m_PreviewUtility.camera.nearClipPlane = distance - halfSize * 1.1f;
    63.         m_PreviewUtility.camera.farClipPlane = distance + halfSize * 1.1f;
    64.  
    65.         m_PreviewUtility.lights[0].intensity = 1.24f;
    66.         m_PreviewUtility.lights[0].transform.rotation = rot * Quaternion.Euler(40f, 40f, 0);
    67.         m_PreviewUtility.lights[1].intensity = 1.24f;
    68.         m_PreviewUtility.lights[1].transform.rotation = rot * Quaternion.Euler(340, 218, 177);
    69.  
    70.         m_PreviewUtility.camera.Render();
    71.  
    72.         Texture2D image = new Texture2D(m_PreviewUtility.camera.targetTexture.width, m_PreviewUtility.camera.targetTexture.height);
    73.         image.ReadPixels(new Rect(0, 0, m_PreviewUtility.camera.targetTexture.width, m_PreviewUtility.camera.targetTexture.height), 0, 0);
    74.         image.Apply();
    75.  
    76.         render = image;
    77.         m_PreviewUtility.EndPreview();
    78.  
    79.         }
    And this is my Animator Class:

    Code (CSharp):
    1. public void Init() {
    2.         _graph = PlayableGraph.Create();
    3.         _playableOutput = AnimationPlayableOutput.Create(_graph, "ANIMATION", GetComponent<Animator>());
    4.  
    5.         _mixer = AnimationMixerPlayable.Create(_graph);
    6.  
    7.         _playableOutput.SetSourcePlayable(_mixer);
    8.         _clipPlayables = new AnimationClipPlayable[20];
    9.         }
    10.  
    11.     public void ResizeArray(int size) {
    12.         Array.Resize<AnimationClipPlayable>(ref _clipPlayables, size);
    13.         }
    14.  
    15.     public int AddClip(AnimationClip clip) {
    16.         _clipPlayables[GraphIndex] = AnimationClipPlayable.Create(_graph, clip);
    17.         int i = PlayableExtensions.AddInput(_mixer, _clipPlayables[GraphIndex], 0, 1f);
    18.         /*_graph.Connect(_clipPlayables[GraphIndex], 0, _mixer, GraphIndex);*/
    19.         GraphIndex++;
    20.         ResizeArray(GraphIndex+1);
    21.         return i;
    22.         }
    23.  
    24.     public void SetTime(int index, float time) {
    25.         _clipPlayables[index].SetTime(time);
    26.         }
    27.  
    28.     public void SetWeight(int index, float weight) {
    29.         _mixer.SetInputWeight(index, weight);
    30.         }
    31.  
    32.     public void ResetWeights() {
    33.         for (int i = 0; i < _mixer.GetInputCount(); i++) {
    34.  
    35.             _mixer.SetInputWeight(i, 0f);
    36.             }
    37.  
    38.         }
    Does anyone know what the issue could be? The animation just doesn't seem to be getting sampled.
     
    Mistermind likes this.