Last active
October 6, 2015 14:58
-
-
Save asus4/3011590 to your computer and use it in GitHub Desktop.
Split Model Animation with CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections.Generic; | |
/// <summary> | |
/// Model animation spliter. | |
/// by Koki Ibukuro @asus4 | |
/// </summary> | |
public class ModelAnimationSpliter : EditorWindow { | |
TextAsset csvAsset; | |
GameObject modelAsset; | |
void OnGUI () | |
{ | |
GUILayout.Label ("CSV to Split Animtion", EditorStyles.boldLabel); | |
GUILayout.Space (10f); | |
csvAsset = EditorGUILayout.ObjectField ("CSV Text", csvAsset, typeof(TextAsset), false) as TextAsset; | |
if (csvAsset == null) { | |
GUILayout.Label ("Set CSV Data. Extension need .txt"); | |
} | |
GUILayout.Space (10f); | |
modelAsset = EditorGUILayout.ObjectField ("3D Model", modelAsset, typeof(GameObject), false) as GameObject; | |
if (modelAsset == null) { | |
GUILayout.Label ("Set FBX or other 3D model object."); | |
} | |
GUILayout.Space (20f); | |
if (GUILayout.Button ("Split with CSV", GUILayout.Width (140f))) { | |
SplitWithCsv (); | |
} | |
} | |
void SplitWithCsv () | |
{ | |
if (Assert (csvAsset != null, "Need to set CSV Text") || Assert (modelAsset != null, "Need to set 3D Model")) { | |
return; | |
} | |
string csv = csvAsset.text; | |
ModelImporterClipAnimation[] clipAnimations = ParceCSV (csv); | |
if (Assert (clipAnimations != null, "can't parse CSV to Animation")) { | |
Debug.Log (csv); | |
return; | |
} | |
string path = AssetDatabase.GetAssetPath (modelAsset); | |
ModelImporter importer = AssetImporter.GetAtPath (path) as ModelImporter; | |
if (Assert (importer != null, "set 3D model, like FBX,etc..")) { | |
return; | |
} | |
//importer.splitAnimations = true; // obsolute in Unity4 | |
importer.clipAnimations = clipAnimations; | |
AssetDatabase.ImportAsset (path); | |
DisplayDialog ("Success", "finish split!"); | |
} | |
bool Assert (bool b, string error) | |
{ | |
if (!b) { | |
DisplayDialog ("Warning", error); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
void DisplayDialog (string title, string message) | |
{ | |
Debug.LogWarning (message); | |
EditorUtility.DisplayDialog (title, message, "OK"); | |
} | |
ModelImporterClipAnimation[] ParceCSV (string csv) | |
{ | |
// split to lines | |
string[] lines = csv.Split ('\n'); | |
char[] trims = {'\r', ' '}; | |
List<ModelImporterClipAnimation> clips = new List<ModelImporterClipAnimation> (); | |
for (int i=0; i<lines.Length; i++) { | |
string s = lines [i].Trim (trims); | |
ModelImporterClipAnimation clip = ParseClip (s); | |
if (clip != null) { | |
Debug.Log (clip); | |
clips.Add (clip); | |
} | |
} | |
if (clips.Count == 0) { | |
return null; | |
} | |
return clips.ToArray(); | |
} | |
ModelImporterClipAnimation ParseClip (string line) | |
{ | |
string[] arr = line.Split (','); | |
if (arr.Length < 4) { | |
return null; | |
} | |
ModelImporterClipAnimation clip = new ModelImporterClipAnimation (); | |
// name | |
string name = arr [0]; | |
if (!string.IsNullOrEmpty (name)) { | |
clip.name = name; | |
} else { | |
return null; | |
} | |
// first | |
try { | |
//clip.firstFrame = int.Parse (arr [1]); // change to float in unity4 | |
clip.firstFrame = float.Parse (arr [1]); | |
} catch { | |
return null; | |
} | |
// last | |
try { | |
//clip.lastFrame = int.Parse (arr [2]); // change to float in unity4 | |
clip.lastFrame = float.Parse (arr [2]); | |
} catch { | |
return null; | |
} | |
// wrapmode | |
try { | |
clip.wrapMode = (WrapMode) System.Enum.Parse(typeof(WrapMode), arr [3]); | |
} catch { | |
return null; | |
} | |
if (clip.wrapMode == WrapMode.Loop || clip.wrapMode == WrapMode.PingPong) { | |
clip.loop = true; | |
} | |
return clip; | |
} | |
#region Static | |
/// <summary> | |
/// Open the tool window | |
/// </summary> | |
[MenuItem("Tools/Model/Animation Spliter")] | |
static public void OpenWindow () | |
{ | |
EditorWindow.GetWindow<ModelAnimationSpliter> (true, "Model Animation Spliter", true); | |
} | |
#endregion | |
} |
- Open Window from [Menu/Tools/Model/Animation Spliter]
- Set CSV text and 3D Model
2013/01/07
fix for Unity4
fix for unity4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample csv
rename CSV file to *.txt
wait1,1,36,Loop
wait2,50,58,Loop
wait2loop,58,59,Loop
buttlePose1,150,190,Once
buttlePose2,200,215,PingPong
buttlePose3,215,250,ClampForever
talkStart,300,312,Default
hear,400,412,Default
thankyou,500,512,Default
bow,600,612,Default
swiinghand,700,712,Default
swiinghandLoop,712,726,Loop