I want to split all the animations inside a FBX by using a .txt file which contains start times and names for the animations in this FBX.
I found a script that does exactely that here.
However, this script uses the PostProcessor, and this means that I have little possiblity over when it's called.
Instead, I would like this to happen when I click a certain menu item. To do that, I have added a new menu item to the Unity toolbar like this:
public class NewBehaviourScript : MonoBehaviour
{
[MenuItem("Tools/FBX/Split")]
private static void SplitFBX()
{
However, as soon as I add the line
ModelImporter modelImporter = assetImporter as ModelImporter;
from the other script, the Editor says that
"The name assetImporter isn't available in the current context".
Does anybody see why this compiler error occurs?
I have also tried
"public class NewBehaviourScript : AssetPostprocessor"
but it gave me the error "An object reference is required for the non-static field, method or property "AssetPostprocessor.assetImporter".
This is the entire script:
using UnityEngine;
using UnityEditor;
using UnityEditor.VersionControl;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System;
public class NewBehaviourScript : MonoBehaviour
{
[MenuItem("Tools/FBX/Split")]
private static void SplitFBX()
{
var selected = Selection.activeObject;
string assetPath = AssetDatabase.GetAssetPath(selected);
EditorUtility.DisplayDialog("", assetPath, "Ok");
try
{
// Remove 6 chars because dataPath and assetPath both contain "assets" directory
string fileAnim = Application.dataPath + Path.ChangeExtension(assetPath, ".txt").Substring(6);
if (!File.Exists(fileAnim))
{
return;
}
StreamReader file = new StreamReader(fileAnim);
string sAnimList = file.ReadToEnd();
file.Close();
if (EditorUtility.DisplayDialog("FBX Animation Import from file", fileAnim, "Import", "Cancel"))
{
System.Collections.ArrayList List = new ArrayList();
pParseAnimFile(sAnimList, ref List);
ModelImporter modelImporter = assetImporter as ModelImporter;
modelImporter.clipAnimations = (ModelImporterClipAnimation[])List.ToArray(typeof(ModelImporterClipAnimation));
EditorUtility.DisplayDialog("Imported animations", "Number of imported clips: " + modelImporter.clipAnimations.GetLength(0).ToString(), "OK");
}
}
catch (Exception e)
{
// EditorUtility.DisplayDialog("Imported animations error: ", e.Message, "OK");
}
}
void pParseAnimFile(string sAnimList, ref System.Collections.ArrayList List)
{
Regex regexString = new Regex(" *(?<firstFrame>[0-9]+) *- *(?<lastFrame>[0-9]+) *(?<loop>(loop|noloop| )) *(?<name>[^\r^\n]*[^\r^\n^ ])",
RegexOptions.Compiled | RegexOptions.ExplicitCapture);
Match match = regexString.Match(sAnimList, 0);
while (match.Success)
{
ModelImporterClipAnimation clip = new ModelImporterClipAnimation();
if (match.Groups["firstFrame"].Success)
{
clip.firstFrame = System.Convert.ToInt32(match.Groups["firstFrame"].Value, 10);
}
if (match.Groups["lastFrame"].Success)
{
clip.lastFrame = System.Convert.ToInt32(match.Groups["lastFrame"].Value, 10);
}
if (match.Groups["loop"].Success)
{
clip.loop = match.Groups["loop"].Value == "loop";
}
if (match.Groups["name"].Success)
{
clip.name = match.Groups["name"].Value;
}
List.Add(clip);
match = regexString.Match(sAnimList, match.Index + match.Length);
}
}
}
assetImporter
is a member variable ofAssetPostprocessor
. By making your script aMonoBehaviour
, it is no longer anAssetPostprocessor
, so it has noassetImporter
member. Instead of asking "why is my compiler telling me this obviously illegal thing is illegal?" you probably want to ask "how can I make a menu item that splits animations based on a text file?" \$\endgroup\$assetImporter
is a non-static member. You need an instance of the class to be able to access its non-static members — you can't do it from a static member. Again, this is C# 101 material. If you're finding it surprising, you might want to work your way through some more beginner C# tutorials to make sure you understand the language you're using. \$\endgroup\$