-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnimationImporter.cs
106 lines (84 loc) · 3.87 KB
/
AnimationImporter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
public class AnimationImporter : MonoBehaviour {
public DataLoader dataLoader;
private void Start() {
Debug.Log("Importing Animations ⊂( ・∀・) 彡 =͟͟͞͞ ⌧");
EditorCurveBinding spriteBinding = new EditorCurveBinding {
type = typeof(SpriteRenderer),
path = "",
propertyName = "m_Sprite"
};
DataItem[] jsonItems = dataLoader.dataClass.items;
if (!AssetDatabase.IsValidFolder("Assets/Resources/Animation Controllers")) {
AssetDatabase.CreateFolder("Assets/Resources", "Animation Controllers");
}
foreach (DataItem item in jsonItems) {
if (!AssetDatabase.IsValidFolder("Assets/Resources/Animation Controllers/" + item.name)) {
AssetDatabase.CreateFolder("Assets/Resources/Animation Controllers", item.name);
}
AnimatorController controller = AnimatorController.CreateAnimatorControllerAtPath("Assets/Resources/Animation Controllers/" + item.name + "/" + item.name + ".controller");
AnimatorStateMachine root = controller.layers[0].stateMachine;
TransitionClass[] transitions = item.animData.transitions;
AnimClass[] animations = item.animData.animations;
TriggerClass[] triggers = item.animData.triggers;
foreach (AnimClass anim in animations) {
Sprite[] sprites = Resources.LoadAll<Sprite>("Animation Assets/" + item.name + "/" + anim.name);
ObjectReferenceKeyframe[] spriteKeyFrames = sprites.Select( (sprite,index) => (
new ObjectReferenceKeyframe {
time = index / anim.fps,
value = sprite
}
)).ToArray();
AnimationClip clip = new AnimationClip() {
name = anim.name
};
if (anim.isLoop) {
AnimationClip tempClip = new AnimationClip();
AnimationClipSettings newSettings = AnimationUtility.GetAnimationClipSettings(tempClip);
newSettings.loopTime = true;
AnimationUtility.SetAnimationClipSettings(clip, newSettings);
}
AnimationUtility.SetObjectReferenceCurve(clip, spriteBinding, spriteKeyFrames);
if (anim.children!=null) {
AnimationEvent[] events = anim.children.Select( (e,index) => (
new AnimationEvent() {
stringParameter = e.name,
time = e.time,
functionName = "spawn"
}
)).ToArray();
AnimationUtility.SetAnimationEvents(clip, events);
}
//allows animations to build properly
AssetDatabase.CreateAsset(clip, "Assets/Resources/Animation Controllers/" + item.name + "/" + clip.name + ".anim");
controller.AddMotion(clip);
}
foreach (TriggerClass trig in triggers) {
controller.AddParameter(trig.name,AnimatorControllerParameterType.Trigger);
}
foreach (TransitionClass t in transitions) {
AnimatorCondition[] conditionsArray = t.conditions.Select( (con,index) => (
new AnimatorCondition() {
mode=AnimatorConditionMode.If,
parameter = con,
threshold = 0
}
)).ToArray();
AnimatorState origin = System.Array.Find(root.states, element => t.source.Equals(element.state.name)).state;
AnimatorState target = System.Array.Find(root.states, element => t.target.Equals(element.state.name)).state;
AnimatorStateTransition transition = new AnimatorStateTransition() {
hasExitTime = t.hasExitTime,
destinationState = target,
conditions = conditionsArray
};
origin.AddTransition(transition);
}
}
Debug.Log("Animations Imported! ♪┏(・o・)┛♪┗ ( ・o・) ┓");
}
}