Skip to content

Commit 0eaa21d

Browse files
committed
Code to access Animation Clips and GameObject in Context Menu
1 parent 853eb64 commit 0eaa21d

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.IO;
22
using System.Collections.Generic;
33
using UnityEngine;
4-
using UnityEngine.SceneManagement;
54
using UnityEditor;
65
using Unity.FbxSdk;
76
using System.Linq;
@@ -58,6 +57,8 @@ public static void Dump (this AnimationCurve animCurve, string message, float[]
5857
namespace Editor
5958
{
6059
using CustomExtensions;
60+
using UnityEngine.Playables;
61+
using UnityEngine.Timeline;
6162

6263
public class ModelExporter : System.IDisposable
6364
{
@@ -79,6 +80,8 @@ public class ModelExporter : System.IDisposable
7980
// from being passed to command, thus resulting in OnContextItem()
8081
// being called only once regardless of what is selected.
8182
const string MenuItemName = "GameObject/Export Model...";
83+
84+
const string ClipMenuItemName = "GameObject/Export All Recorded Animation Clips...";
8285

8386
const string FileBaseName = "Untitled";
8487

@@ -2606,6 +2609,119 @@ private void ReplaceFile ()
26062609
}
26072610
}
26082611

2612+
/// <summary>
2613+
/// Add an option "Update from FBX" in the contextual GameObject menu.
2614+
/// </summary>
2615+
[MenuItem(ClipMenuItemName, false, 31)]
2616+
static void OnClipContextClick(MenuCommand command)
2617+
{
2618+
/*var obs = Selection.objects;
2619+
foreach (var obj in obs)
2620+
{
2621+
if (obj.GetType().Name.Contains("EditorClip"))
2622+
{
2623+
Debug.Log("clip selected");
2624+
var selClip = obj.GetType().GetProperty("clip").GetValue(obj, null);
2625+
var timeLineClip = selClip as UnityEngine.Timeline.TimelineClip;
2626+
Debug.Log("clip name: " + timeLineClip.displayName);
2627+
}
2628+
}*/
2629+
2630+
// Now that we know we have stuff to export, get the user-desired path.
2631+
string directory = string.IsNullOrEmpty(LastFilePath)
2632+
? Application.dataPath
2633+
: System.IO.Path.GetDirectoryName(LastFilePath);
2634+
2635+
2636+
string title = string.Format("Export Model FBX ({0})", FileBaseName);
2637+
2638+
string folderPath = EditorUtility.SaveFolderPanel(title, directory, "");
2639+
2640+
if (string.IsNullOrEmpty(folderPath))
2641+
{
2642+
return;
2643+
}
2644+
Debug.Log(folderPath);
2645+
2646+
Object[] selection = null;
2647+
2648+
if (command == null || command.context == null)
2649+
{
2650+
// We were actually invoked from the top GameObject menu, so use the selection.
2651+
selection = Selection.GetFiltered<Object>(SelectionMode.Editable | SelectionMode.TopLevel);
2652+
}
2653+
else
2654+
{
2655+
// We were invoked from the right-click menu, so use the context of the context menu.
2656+
var selected = command.context as GameObject;
2657+
if (selected)
2658+
{
2659+
selection = new GameObject[] { selected };
2660+
}
2661+
}
2662+
foreach (GameObject obj in selection)
2663+
{
2664+
Debug.Log(obj.GetType().BaseType.ToString() + ":" + obj.name);
2665+
2666+
PlayableDirector pd = obj.GetComponent<PlayableDirector>();
2667+
if (pd != null)
2668+
{
2669+
foreach (PlayableBinding output in pd.playableAsset.outputs)
2670+
{
2671+
AnimationTrack at = output.sourceObject as AnimationTrack;
2672+
2673+
GameObject atObject = pd.GetGenericBinding(output.sourceObject) as GameObject;
2674+
2675+
//Debug.Log(pd.GetGenericBinding(output.sourceObject).name);
2676+
2677+
// var at = pd.GetGenericBinding(output.sourceObject) as GameObject;
2678+
foreach (TimelineClip myclip in at.GetClips())
2679+
{
2680+
2681+
AnimationClip animationClip = myclip.animationClip;
2682+
2683+
Debug.Log("output.streamName: " + output.streamName + " /Animation Track name: " + at.name + " : " + animationClip.name + " /myclip.displayName: " + myclip.displayName + " atObject.name " + atObject.name);
2684+
2685+
Dictionary<GameObject, List<AnimationClip>> dict = new Dictionary<GameObject, List<AnimationClip>>();
2686+
// Detect if it's a recorded anim clip
2687+
//ExportAnimationClip(animationClip, obj, null);
2688+
2689+
2690+
// Export it to FBX in the path specified by the user.
2691+
}
2692+
}
2693+
}
2694+
}
2695+
2696+
}
2697+
2698+
2699+
/// <summary>
2700+
/// Validate the menu item defined by the function above.
2701+
/// </summary>
2702+
[MenuItem(ClipMenuItemName, true, 31)]
2703+
public static bool ValidateClipContextClick()
2704+
{
2705+
//return true;
2706+
2707+
Object[] selection = Selection.objects;
2708+
2709+
if (selection == null || selection.Length == 0)
2710+
{
2711+
return false;
2712+
}
2713+
2714+
foreach (GameObject obj in selection)
2715+
{
2716+
if (obj.GetComponent<PlayableDirector>() != null)
2717+
{
2718+
return true;
2719+
}
2720+
}
2721+
2722+
return false;
2723+
}
2724+
26092725
/// <summary>
26102726
/// Add a menu item to a GameObject's context menu.
26112727
/// </summary>
@@ -3198,4 +3314,4 @@ public static string ConvertToValidFilename(string filename)
31983314
}
31993315
}
32003316
}
3201-
}
3317+
}

Assets/FbxExporters/Editor/FbxPrefabAutoUpdater.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
using System.Collections;
21
using System.Collections.Generic;
3-
using System.Reflection;
42
using UnityEngine;
53
using UnityEditor;
64
using System.Linq;
75
using System;
8-
using FbxExporters.Editor;
9-
using UnityEngine.TestTools;
106

117
namespace FbxExporters
128
{

0 commit comments

Comments
 (0)