123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.Timeline;
- using System.Collections.Generic;
- namespace UnityEditor.Formats.Fbx.Exporter
- {
-
-
-
- internal interface IExportData
- {
- HashSet<GameObject> Objects { get; }
- }
-
-
-
-
- internal class AnimationOnlyExportData : IExportData
- {
-
-
- public Dictionary<AnimationClip, GameObject> animationClips;
-
- public HashSet<GameObject> goExportSet;
- public HashSet<GameObject> Objects { get { return goExportSet; } }
-
- public Dictionary<GameObject, System.Type> exportComponent;
-
- public AnimationClip defaultClip;
- public AnimationOnlyExportData(
- Dictionary<AnimationClip, GameObject> animClips,
- HashSet<GameObject> exportSet,
- Dictionary<GameObject, System.Type> exportComponent
- )
- {
- this.animationClips = animClips;
- this.goExportSet = exportSet;
- this.exportComponent = exportComponent;
- this.defaultClip = null;
- }
- public AnimationOnlyExportData()
- {
- this.animationClips = new Dictionary<AnimationClip, GameObject>();
- this.goExportSet = new HashSet<GameObject>();
- this.exportComponent = new Dictionary<GameObject, System.Type>();
- this.defaultClip = null;
- }
-
-
-
- public void CollectDependencies(
- AnimationClip animClip,
- GameObject rootObject,
- IExportOptions exportOptions
- )
- {
- Debug.Assert(rootObject != null);
- Debug.Assert(exportOptions != null);
- if (this.animationClips.ContainsKey(animClip))
- {
-
- return;
- }
-
-
- this.animationClips.Add(animClip, rootObject);
- foreach (EditorCurveBinding uniCurveBinding in AnimationUtility.GetCurveBindings(animClip))
- {
- Object uniObj = AnimationUtility.GetAnimatedObject(rootObject, uniCurveBinding);
- if (!uniObj)
- {
- continue;
- }
- GameObject unityGo = ModelExporter.GetGameObject(uniObj);
- if (!unityGo)
- {
- continue;
- }
- if (!exportOptions.AnimateSkinnedMesh && unityGo.GetComponent<SkinnedMeshRenderer>())
- {
- continue;
- }
-
-
- if (unityGo.GetComponent<Light>())
- this.exportComponent[unityGo] = typeof(Light);
- else if (unityGo.GetComponent<Camera>())
- this.exportComponent[unityGo] = typeof(Camera);
- this.goExportSet.Add(unityGo);
- }
- }
-
-
-
- public void CollectDependencies(
- AnimationClip[] animClips,
- GameObject rootObject,
- IExportOptions exportOptions
- )
- {
- Debug.Assert(rootObject != null);
- Debug.Assert(exportOptions != null);
-
- foreach (var animClip in animClips)
- {
- CollectDependencies(animClip, rootObject, exportOptions);
- }
- }
-
-
-
-
-
-
- private static object GetPropertyReflection(object obj, string propertyName)
- {
- return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
- }
-
-
-
-
-
- private static TimelineClip GetTimelineClipFromEditorClip(object editorClip)
- {
- object clip = GetPropertyReflection(editorClip, "clip");
- return clip as TimelineClip;
- }
-
-
-
-
-
- private static GameObject GetGameObjectBoundToEditorClip(object editorClip)
- {
- var timelineClip = GetTimelineClipFromEditorClip(editorClip);
- object parentTrack = timelineClip.parentTrack;
- AnimationTrack animTrack = parentTrack as AnimationTrack;
-
- Object animationTrackObject = UnityEditor.Timeline.TimelineEditor.inspectedDirector.GetGenericBinding(animTrack);
- GameObject animationTrackGO = null;
- if (animationTrackObject is GameObject)
- {
- animationTrackGO = animationTrackObject as GameObject;
- }
- else if (animationTrackObject is Animator)
- {
- animationTrackGO = (animationTrackObject as Animator).gameObject;
- }
- if (animationTrackGO == null)
- {
- Debug.LogErrorFormat("Could not export animation track object of type {0}", animationTrackObject.GetType().Name);
- return null;
- }
- return animationTrackGO;
- }
-
-
-
-
-
- public static KeyValuePair<GameObject, AnimationClip> GetGameObjectAndAnimationClip(Object editorClip)
- {
- if (!ModelExporter.IsEditorClip(editorClip))
- return new KeyValuePair<GameObject, AnimationClip>();
-
- TimelineClip timeLineClip = GetTimelineClipFromEditorClip(editorClip);
- var animationTrackGO = GetGameObjectBoundToEditorClip(editorClip);
- if (animationTrackGO == null)
- {
- return new KeyValuePair<GameObject, AnimationClip>();
- }
- return new KeyValuePair<GameObject, AnimationClip>(animationTrackGO, timeLineClip.animationClip);
- }
-
-
-
-
-
- public static string GetFileName(Object obj)
- {
- if (!ModelExporter.IsEditorClip(obj))
- {
-
- return obj.name;
- }
- TimelineClip timeLineClip = GetTimelineClipFromEditorClip(obj);
-
-
- if (timeLineClip.displayName.Contains("@"))
- {
- return timeLineClip.displayName;
- }
- var goBound = GetGameObjectBoundToEditorClip(obj);
- if (goBound == null)
- {
- return obj.name;
- }
- return string.Format("{0}@{1}", goBound.name, timeLineClip.displayName);
- }
- }
- }
|