AnimatorController









How do I use Animator Controller
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PhotonAnimatorViewEditor.cs Copy
54     private void OnEnable()
55     {
56         this.m_Target = (PhotonAnimatorView) target;
57         this.m_Animator = this.m_Target.GetComponent();
58
59#if UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6
60         this.m_Controller = AnimatorController.GetEffectiveAnimatorController(this.m_Animator);
61#endif
62
63         CheckIfStoredParametersExist();
64     }
File name: PhotonAnimatorViewEditor.cs Copy
179     private void DrawParameterInspector()
180     {
181         SerializedProperty foldoutProperty = serializedObject.FindProperty("ShowParameterInspector");
182         foldoutProperty.boolValue = PhotonGUI.ContainerHeaderFoldout("Synchronize Parameters", foldoutProperty.boolValue);
183
184         if (foldoutProperty.boolValue == false)
185         {
186             return;
187         }
188
189         float lineHeight = 20;
190         Rect containerRect = PhotonGUI.ContainerBody(GetParameterCount()*lineHeight);
191
192         for (int i = 0; i < GetParameterCount(); i++)
193         {
194             AnimatorControllerParameter parameter = null;
195
196#if UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6
197             parameter = this.m_Controller.GetParameter(i);
198#else
199             parameter = m_Animator.parameters[ i ];
200#endif
201
202             string defaultValue = "";
203
204             if (parameter.type == AnimatorControllerParameterType.Bool)
205             {
206                 defaultValue += parameter.defaultBool.ToString();
207             }
208             else if (parameter.type == AnimatorControllerParameterType.Float)
209             {
210                 defaultValue += parameter.defaultFloat.ToString();
211             }
212             else if (parameter.type == AnimatorControllerParameterType.Int)
213             {
214                 defaultValue += parameter.defaultInt.ToString();
215             }
216
217             if (this.m_Target.DoesParameterSynchronizeTypeExist(parameter.name) == false)
218             {
219                 this.m_Target.SetParameterSynchronized(parameter.name, (PhotonAnimatorView.ParameterType) parameter.type, PhotonAnimatorView.SynchronizeType.Disabled);
220                 EditorUtility.SetDirty(this.m_Target);
221             }
222
223             PhotonAnimatorView.SynchronizeType value = this.m_Target.GetParameterSynchronizeType(parameter.name);
224
225             Rect elementRect = new Rect(containerRect.xMin, containerRect.yMin + i*lineHeight, containerRect.width, lineHeight);
226
227             Rect labelRect = new Rect(elementRect.xMin + 5, elementRect.yMin + 2, EditorGUIUtility.labelWidth - 5, elementRect.height);
228             GUI.Label(labelRect, parameter.name + " (" + defaultValue + ")");
229
230             Rect popupRect = new Rect(elementRect.xMin + EditorGUIUtility.labelWidth, elementRect.yMin + 2, elementRect.width - EditorGUIUtility.labelWidth - 5, EditorGUIUtility.singleLineHeight);
231             value = (PhotonAnimatorView.SynchronizeType) EditorGUI.EnumPopup(popupRect, value);
232
233             if (i < GetParameterCount() - 1)
234             {
235                 Rect splitterRect = new Rect(elementRect.xMin + 2, elementRect.yMax, elementRect.width - 4, 1);
236                 PhotonGUI.DrawSplitter(splitterRect);
237             }
238
239             if (value != this.m_Target.GetParameterSynchronizeType(parameter.name))
240             {
241                 Undo.RecordObject(target, "Modify Synchronize Parameter " + parameter.name);
242                 this.m_Target.SetParameterSynchronized(parameter.name, (PhotonAnimatorView.ParameterType) parameter.type, value);
243
244                 EditorUtility.SetDirty(this.m_Target);
245             }
246         }
247     }
File name: ScmlPostProcessor.cs Copy
66         static void ImportScml(string assetPath)
67         {
68             string folderPath = Path.GetDirectoryName(assetPath);
69
70             //Load the SCML as XML
71             var doc = new XmlDocument();
72             doc.Load(assetPath);
73
74             //Parse the SCML file
75             var scml = new Spriter.ScmlObject(doc);
76
77             //TODO: Verify that all files/folders exist
78             var pb = new PrefabBuilder();
79             foreach (var entity in scml.Entities)
80             {
81                 //TODO: Settings file to customize prefab location
82                 var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab");
83
84                 //Change to forward slash for asset database friendliness
85                 prefabPath = prefabPath.Replace('\\', '/');
86
87                 //Either instantiate the existing prefab or create a new one
88                 GameObject go;
89                 var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
90                 if (prefabGo == null)
91                 {
92                     go = new GameObject();
93                     prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab);
94                 }
95                 else
96                 {
97                     go = GameObject.Instantiate(prefabGo) as GameObject;
98
99                     var oldAnimator = go.GetComponent();
100                     if (oldAnimator) GameObject.DestroyImmediate(oldAnimator);
101                 }
102
103                 //Build the prefab based on the supplied entity
104                 pb.MakePrefab(entity, go, folderPath);
105
106                 var animator = go.AddComponent();
107
108
109
110                 //Add animations to prefab object
111                 var anim = new AnimationBuilder();
112                 var allAnimClips = anim.BuildAnimationClips(go, entity, prefabPath);
113                 AssetDatabase.SaveAssets();
114
115                 var animatorControllerPath = Path.ChangeExtension(prefabPath, "controller");
116                 UnityEditor.Animations.AnimatorController oldController = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(animatorControllerPath, typeof (UnityEditor.Animations.AnimatorController));
117                 UnityEditor.Animations.AnimatorController controller = oldController;
118
119                 if (!oldController)
120                 {
121                     controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(animatorControllerPath);
122                     foreach (var animationClip in allAnimClips)
123                     {
124                         if (animationClip)
125                         {
126                             //UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, animationClip);
127                         }
128                     }
129                 }
130                 UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controller);
131                 go.SetActive(true);
132                 //Update the prefab
133                 PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab);
134
135                 //Add a generic avatar - because why not?
136                 //TODO: May need to eventually break this into a separate class
137                 // ie: if we want to look for a root motion node by naming convention
138                 //var avatar = AvatarBuilder.BuildGenericAvatar(go, "");
139                 //avatar.name = go.name;
140                 //AssetDatabase.AddObjectToAsset(avatar, prefabPath);
141
142                 GameObject.DestroyImmediate(go);
143
144                 AssetDatabase.SaveAssets();
145             }
146         }

AnimatorController 108 lượt xem

Gõ tìm kiếm nhanh...