Skip to content

Commit 9573c72

Browse files
committed
add comments to functions + clean up code
1 parent 45f8a14 commit 9573c72

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

Packages/com.unity.formats.fbx/Editor/Scripts/IExportData.cs

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,37 @@ IExportOptions exportOptions
101101
}
102102
}
103103

104-
public static GameObject GetGameObjectBoundToEditorClip(object editorClip)
104+
/// <summary>
105+
/// Get the property propertyName from object obj using reflection.
106+
/// </summary>
107+
/// <param name="obj"></param>
108+
/// <param name="propertyName"></param>
109+
/// <returns>property propertyName as an object</returns>
110+
private static object GetPropertyReflection(object obj, string propertyName)
111+
{
112+
return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
113+
}
114+
115+
/// <summary>
116+
/// Get the timeline clip from the given editor clip using reflection.
117+
/// </summary>
118+
/// <param name="editorClip"></param>
119+
/// <returns>the timeline clip or null if none</returns>
120+
private static TimelineClip GetTimelineClipFromEditorClip(object editorClip)
105121
{
106-
object clipItem = editorClip.GetType().GetProperty("item").GetValue(editorClip, null);
107-
object parentTrack = clipItem.GetType().GetProperty("parentTrack").GetValue(clipItem, null);
122+
object clip = GetPropertyReflection(editorClip, "clip");
123+
return clip as TimelineClip;
124+
}
125+
126+
/// <summary>
127+
/// Get the GameObject that the editor clip is bound to in the timeline using reflection.
128+
/// </summary>
129+
/// <param name="editorClip"></param>
130+
/// <returns>The GameObject bound to the editor clip or null if none.</returns>
131+
private static GameObject GetGameObjectBoundToEditorClip(object editorClip)
132+
{
133+
object clipItem = GetPropertyReflection(editorClip, "item");
134+
object parentTrack = GetPropertyReflection(clipItem, "parentTrack");
108135
AnimationTrack animTrack = parentTrack as AnimationTrack;
109136

110137
#if UNITY_2018_2_OR_NEWER
@@ -131,15 +158,19 @@ public static GameObject GetGameObjectBoundToEditorClip(object editorClip)
131158
return animationTrackGO;
132159
}
133160

134-
public static KeyValuePair<GameObject, AnimationClip> GetGameObjectAndAnimationClip(Object obj)
161+
/// <summary>
162+
/// Get the GameObject and it's corresponding animation clip from the given editor clip.
163+
/// </summary>
164+
/// <param name="editorClip"></param>
165+
/// <returns>KeyValuePair containing GameObject and corresponding AnimationClip</returns>
166+
public static KeyValuePair<GameObject, AnimationClip> GetGameObjectAndAnimationClip(Object editorClip)
135167
{
136-
if (!ModelExporter.IsEditorClip(obj))
168+
if (!ModelExporter.IsEditorClip(editorClip))
137169
return new KeyValuePair<GameObject, AnimationClip>();
170+
171+
TimelineClip timeLineClip = GetTimelineClipFromEditorClip(editorClip);
138172

139-
object clip = obj.GetType().GetProperty("clip").GetValue(obj, null);
140-
TimelineClip timeLineClip = clip as TimelineClip;
141-
142-
var animationTrackGO = GetGameObjectBoundToEditorClip(obj);
173+
var animationTrackGO = GetGameObjectBoundToEditorClip(editorClip);
143174
if (animationTrackGO == null)
144175
{
145176
return new KeyValuePair<GameObject, AnimationClip>();
@@ -148,31 +179,34 @@ public static KeyValuePair<GameObject, AnimationClip> GetGameObjectAndAnimationC
148179
return new KeyValuePair<GameObject, AnimationClip>(animationTrackGO, timeLineClip.animationClip);
149180
}
150181

182+
/// <summary>
183+
/// Get the filename of the format {model}@{anim}.fbx from the given object
184+
/// </summary>
185+
/// <param name="obj"></param>
186+
/// <returns>filename for use for exporting animation clip</returns>
151187
public static string GetFileName(Object obj)
152188
{
153-
if (ModelExporter.IsEditorClip(obj))
189+
if (!ModelExporter.IsEditorClip(obj))
154190
{
155-
object clip = obj.GetType().GetProperty("clip").GetValue(obj, null);
156-
TimelineClip timeLineClip = clip as TimelineClip;
191+
// not an editor clip so just return the name of the object
192+
return obj.name;
193+
}
157194

158-
if (timeLineClip.displayName.Contains("@"))
159-
{
160-
return timeLineClip.displayName;
161-
}
162-
else
163-
{
164-
var goBound = GetGameObjectBoundToEditorClip(obj);
165-
if (goBound == null)
166-
{
167-
return null;
168-
}
169-
return string.Format("{0}@{1}", goBound.name, timeLineClip.displayName);
170-
}
195+
TimelineClip timeLineClip = GetTimelineClipFromEditorClip(obj);
196+
197+
// if the timeline clip name already contains an @, then take this as the
198+
// filename to avoid duplicate @
199+
if (timeLineClip.displayName.Contains("@"))
200+
{
201+
return timeLineClip.displayName;
171202
}
172-
else
203+
204+
var goBound = GetGameObjectBoundToEditorClip(obj);
205+
if (goBound == null)
173206
{
174207
return obj.name;
175208
}
209+
return string.Format("{0}@{1}", goBound.name, timeLineClip.displayName);
176210
}
177211
}
178212
}

0 commit comments

Comments
 (0)