Skip to content

Commit c800de0

Browse files
committed
Add blendShape recording function to UnityAnimationRecorder
1 parent 3f5f31f commit c800de0

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class UnityAnimationRecorderEditor : Editor {
1616
SerializedProperty showLogGUI;
1717
SerializedProperty recordLimitedFrames;
1818
SerializedProperty recordFrames;
19+
SerializedProperty recordBlendShape;
1920

2021
SerializedProperty changeTimeScale;
2122
SerializedProperty timeScaleOnStart;
@@ -33,6 +34,7 @@ void OnEnable () {
3334
showLogGUI = serializedObject.FindProperty ("showLogGUI");
3435
recordLimitedFrames = serializedObject.FindProperty ("recordLimitedFrames");
3536
recordFrames = serializedObject.FindProperty ("recordFrames");
37+
recordBlendShape = serializedObject.FindProperty ("recordBlendShape");
3638

3739
changeTimeScale = serializedObject.FindProperty ("changeTimeScale");
3840
timeScaleOnStart = serializedObject.FindProperty ("timeScaleOnStart");
@@ -70,6 +72,7 @@ public override void OnInspectorGUI () {
7072

7173
// Other Settings
7274
EditorGUILayout.LabelField( "== Other Settings ==" );
75+
recordBlendShape.boolValue = EditorGUILayout.Toggle ("Record BlendShapes", recordBlendShape.boolValue);
7376
bool timeScaleOption = EditorGUILayout.Toggle ( "Change Time Scale", changeTimeScale.boolValue);
7477
changeTimeScale.boolValue = timeScaleOption;
7578

Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using UnityEngine;
33
using UnityEditor;
44
using System.Collections;
5+
using System.Collections.Generic;
56

67
public class UnityAnimationRecorder : MonoBehaviour {
78

@@ -27,8 +28,13 @@ public class UnityAnimationRecorder : MonoBehaviour {
2728
public float timeScaleOnStart = 0.0f;
2829
public float timeScaleOnRecord = 1.0f;
2930

31+
public bool recordBlendShape = false;
32+
33+
3034
Transform[] recordObjs;
35+
SkinnedMeshRenderer[] blendShapeObjs;
3136
UnityObjectAnimation[] objRecorders;
37+
List<UnityBlendShapeAnimation> blendShapeRecorders;
3238

3339
bool isStart = false;
3440
float nowTime = 0.0f;
@@ -42,13 +48,26 @@ void Start () {
4248
void SetupRecorders () {
4349
recordObjs = gameObject.GetComponentsInChildren<Transform> ();
4450
objRecorders = new UnityObjectAnimation[recordObjs.Length];
51+
blendShapeRecorders = new List<UnityBlendShapeAnimation> ();
4552

4653
frameIndex = 0;
4754
nowTime = 0.0f;
4855

4956
for (int i = 0; i < recordObjs.Length; i++) {
5057
string path = AnimationRecorderHelper.GetTransformPathName (transform, recordObjs [i]);
5158
objRecorders [i] = new UnityObjectAnimation ( path, recordObjs [i]);
59+
60+
// check if theres blendShape
61+
if (recordBlendShape) {
62+
if (recordObjs [i].GetComponent<SkinnedMeshRenderer> ()) {
63+
SkinnedMeshRenderer tempSkinMeshRenderer = recordObjs [i].GetComponent<SkinnedMeshRenderer> ();
64+
65+
// there is blendShape exist
66+
if (tempSkinMeshRenderer.sharedMesh.blendShapeCount > 0) {
67+
blendShapeRecorders.Add (new UnityBlendShapeAnimation (path, tempSkinMeshRenderer));
68+
}
69+
}
70+
}
5271
}
5372

5473
if (changeTimeScale)
@@ -72,6 +91,12 @@ void Update () {
7291
for (int i = 0; i < objRecorders.Length; i++) {
7392
objRecorders [i].AddFrame (nowTime);
7493
}
94+
95+
if (recordBlendShape) {
96+
for (int i = 0; i < blendShapeRecorders.Count; i++) {
97+
blendShapeRecorders [i].AddFrame (nowTime);
98+
}
99+
}
75100
}
76101

77102
}
@@ -145,6 +170,18 @@ void ExportAnimationClip () {
145170
}
146171
}
147172

173+
if (recordBlendShape) {
174+
for (int i = 0; i < blendShapeRecorders.Count; i++) {
175+
176+
UnityCurveContainer[] curves = blendShapeRecorders [i].curves;
177+
178+
for (int x = 0; x < curves.Length; x++) {
179+
clip.SetCurve (blendShapeRecorders [i].pathName, typeof(SkinnedMeshRenderer), curves [x].propertyName, curves [x].animCurve);
180+
}
181+
182+
}
183+
}
184+
148185
clip.EnsureQuaternionContinuity ();
149186
AssetDatabase.CreateAsset ( clip, exportFilePath );
150187

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class UnityBlendShapeAnimation {
5+
6+
public UnityCurveContainer[] curves;
7+
public SkinnedMeshRenderer skinMeshObj;
8+
string[] blendShapeNames;
9+
int blendShapeCount = 0;
10+
public string pathName = "";
11+
12+
public UnityBlendShapeAnimation( string hierarchyPath, SkinnedMeshRenderer observeSkinnedMeshRenderer ) {
13+
pathName = hierarchyPath;
14+
skinMeshObj = observeSkinnedMeshRenderer;
15+
16+
//int blendShapeCount = skinMeshObj.getbl
17+
Mesh blendShapeMesh = skinMeshObj.sharedMesh;
18+
blendShapeCount = blendShapeMesh.blendShapeCount;
19+
20+
blendShapeNames = new string[blendShapeCount];
21+
curves = new UnityCurveContainer[blendShapeCount];
22+
23+
// create curve objs and add names
24+
for (int i = 0; i < blendShapeCount; i++) {
25+
blendShapeNames [i] = blendShapeMesh.GetBlendShapeName (i);
26+
curves [i] = new UnityCurveContainer ("blendShape." + blendShapeNames [i]);
27+
}
28+
}
29+
30+
public void AddFrame ( float time ) {
31+
32+
for (int i = 0; i < blendShapeCount; i++)
33+
curves [i].AddValue (time, skinMeshObj.GetBlendShapeWeight (i));
34+
35+
}
36+
}

Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityBlendShapeAnimation.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)