Last active
March 16, 2017 21:29
-
-
Save rakkarage/167d64e7dc8ebdcb6857b40c969edbf3 to your computer and use it in GitHub Desktop.
Add or extract subobjects in unity3d like fonts or animations
This file contains 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 System.IO; | |
using UnityEditor; | |
using UnityEditor.Animations; | |
using UnityEngine; | |
public class AdderEditor : EditorWindow | |
{ | |
public Font Font; | |
public Texture2D Texture; | |
public Material Material; | |
public AnimatorController Parent; | |
public AnimationClip Child; | |
[MenuItem("Deko/Adder")] | |
static public void Init() | |
{ | |
EditorWindow.GetWindow(typeof(AdderEditor)).Show(); | |
} | |
void OnGUI() | |
{ | |
Font = EditorGUILayout.ObjectField(Font, typeof(Font), false) as Font; | |
Texture = EditorGUILayout.ObjectField(Texture, typeof(Texture2D), false) as Texture2D; | |
Material = EditorGUILayout.ObjectField(Material, typeof(Material), false) as Material; | |
if (GUILayout.Button("Add Font")) | |
{ | |
AddFont(); | |
} | |
if (GUILayout.Button("Extract Font Texture")) | |
{ | |
ExtractFontTexture(); | |
} | |
Parent = EditorGUILayout.ObjectField(Parent, typeof(AnimatorController), false) as AnimatorController; | |
Child = EditorGUILayout.ObjectField(Child, typeof(AnimationClip), false) as AnimationClip; | |
if (GUILayout.Button("Add Animation")) | |
{ | |
AddAnimations(); | |
} | |
if (GUILayout.Button("Add Selected Animations")) | |
{ | |
AddSelectedAnimations(); | |
} | |
} | |
void AddFont() | |
{ | |
var name = Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(Font)); | |
var newTexture = Object.Instantiate(Texture) as Texture2D; | |
newTexture.name = name; | |
var newMaterial = Object.Instantiate(Material) as Material; | |
newMaterial.name = name; | |
newMaterial.mainTexture = newTexture; | |
Font.material = newMaterial; | |
AssetDatabase.AddObjectToAsset(newTexture, Font); | |
AssetDatabase.AddObjectToAsset(newMaterial, Font); | |
AssetDatabase.SaveAssets(); | |
} | |
void ExtractFontTexture() | |
{ | |
var bytes = Texture.EncodeToPNG(); | |
File.WriteAllBytes(EditorUtility.SaveFilePanel("Save PNG", Application.dataPath + "/../", "Font", "png"), bytes); | |
} | |
void AddAnimations() | |
{ | |
var newAnimation = Object.Instantiate(Child) as AnimationClip; | |
newAnimation.name = Child.name; | |
AssetDatabase.AddObjectToAsset(newAnimation, Parent); | |
AssetDatabase.SaveAssets(); | |
} | |
void AddSelectedAnimations() | |
{ | |
var clips = Selection.GetFiltered(typeof(AnimationClip), SelectionMode.Unfiltered); | |
if (clips != null && clips.Length > 0) | |
{ | |
foreach (var clip in clips) | |
{ | |
var newAnimation = Object.Instantiate(clip) as AnimationClip; | |
newAnimation.name = clip.name; | |
AssetDatabase.AddObjectToAsset(newAnimation, Parent); | |
} | |
AssetDatabase.SaveAssets(); | |
Selection.objects = new Object[0]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment