Skip to content

Instantly share code, notes, and snippets.

@Taremin
Last active May 6, 2020 05:11
Show Gist options
  • Save Taremin/1afe15d72513f3e6adddcabd4e9c7cd3 to your computer and use it in GitHub Desktop.
Save Taremin/1afe15d72513f3e6adddcabd4e9c7cd3 to your computer and use it in GitHub Desktop.
namespace ExportHumanoidRig
{
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
[System.Serializable]
public class ExportHumanoidRigWindow : EditorWindow
{
private GameObject obj = null;
[MenuItem("GameObject/ExportHumanoidRig", false, 20)]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(ExportHumanoidRigWindow));
}
private string ConvertAvatarToJSON()
{
var animator = obj.GetComponent<Animator>();
var dictionary = CreateEnumDictionary<HumanBodyBones>();
var output = "{";
foreach (var kvp in dictionary)
{
var bone = kvp.Value;
if (bone == HumanBodyBones.LastBone)
{
continue;
}
var transform = animator.GetBoneTransform(kvp.Value);
output += "\"" + kvp.Key + "\":" + (transform ? "\"" + transform.name + "\"" : "null") + ",";
}
output = output.TrimEnd(',');
output += "}";
return output;
}
static public Dictionary<string, EnumType> CreateEnumDictionary<EnumType>()
{
return System.Enum.GetValues(typeof(EnumType)).Cast<EnumType>().ToDictionary(t => t.ToString(), t => t);
}
private void OnGUI()
{
obj = (GameObject)EditorGUILayout.ObjectField("GameObject", obj, typeof(GameObject), true);
if (GUILayout.Button("Export"))
{
var json = ConvertAvatarToJSON();
var path = Application.dataPath + "/" + obj.name + ".json";
System.IO.File.WriteAllText(path, json);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment