Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dnkira · Nov 25, 2014 at 06:44 AM · fbxeventsystemeventsfbximporter

FBX animation properties inspector, events with code

need to edit fbx animations with code: http://screencast.com/t/eoSdcJzQ this is the inspector part where it can be edited without changing exported animation (animation tab of fbx properties inspector)

P.S. there is a way to do it for separate animations, but in this case I would like to change import settings, not the animation itself.

 AnimationUtility.SetAnimationEvents(c, new AnimationEvent[] { 
                         new AnimationEvent() { functionName = "onEvent", floatParameter = c.length, intParameter = 0 ,time = 0}
                     });
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SaraCecilia · Nov 25, 2014 at 01:50 PM 0
Share

Would FbxAnimListPostprocessor perhaps help?

http://wiki.unity3d.com/index.php/FbxAnimListPostprocessor

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dnkira · Dec 23, 2014 at 12:12 PM

Made a little Editor Script: - Setting Animation Events in export settings - Exporting editable clones - Setting Clones for Animation component

 using System.Collections.Generic;
 using System.Linq;
 using UnityEditor;
 using UnityEngine;
 
 public class AnimationProperties
 {
     public readonly string Name;
     public readonly float Length;
 
     public float procTime = 0;
     public AnimationProperties(string Name, float Length)
     {
         // TODO: Complete member initialization
         this.Name = Name;
         this.Length = Length;
     }
 
 }
 public class FBXEventEditor : EditorWindow
 {
     private const string ANIMATION_FOLDER = "Animation";
 
     [MenuItem("Animation/EBX animation events")]
     public static void ShowWindow()
     {
         EditorWindow.GetWindow(typeof(FBXEventEditor));
     }
     Object _targetObject;
     Object obj;
     List<AnimationProperties> animationProperties = new List<AnimationProperties>();
 
 
     void OnGUI()
     {
         obj = EditorGUILayout.ObjectField("FBX Asset", obj, typeof(GameObject), false);
 
 
         if (GUILayout.Button("ReRead"))
         {
             _targetObject = null;
         }
 
         if (obj != _targetObject)
         {
             _targetObject = obj;
             UpdateAnimationList();
         }
 
         EditorGUILayout.BeginHorizontal();
         GUILayout.Label("Name", GUILayout.Width(100));
         GUILayout.Label("Length", GUILayout.Width(43));
         GUILayout.Label("Proc", GUILayout.Width(43));
         EditorGUILayout.EndHorizontal();
         for (int i = 0; i < animationProperties.Count; i++)
         {
             AnimationProperties ap = animationProperties[i];
             EditorGUILayout.BeginHorizontal();
             GUILayout.Label(ap.Name, GUILayout.Width(100));
             GUILayout.Label(ap.Length.ToString(), GUILayout.Width(43));
             ap.procTime = EditorGUILayout.Slider(ap.procTime, 0, 1);
             EditorGUILayout.EndHorizontal();
         }
 
 
         if (GUILayout.Button("Set import events"))
         {
             SaveExportSettings();
         }
         GUILayout.Space(20);
         if (GUILayout.Button("Export Editable"))
         {
             ExportEditable();
         }
 
         if (GUILayout.Button("Set exported 2 selected"))
         {
             SetAnimationClips();
         }
     }
 
 
 
     private void ExportEditable()
     {
 
         AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
 
         string path = AssetDatabase.GetAssetPath(obj);
 
         Object[] assets = AssetDatabase.LoadAllAssetsAtPath(path);
         List<AnimationClip> clips = assets.ToList().Where(x => x is AnimationClip && !x.name.Contains("__preview__")).Cast<AnimationClip>().ToList();
 
         string clipFolder = System.IO.Path.Combine(System.IO.Directory.GetParent(path).ToString(), ANIMATION_FOLDER);
         if (!AssetExtentions.DirectoryExists(clipFolder))
         {
             Debug.Log("Create folder");
             AssetDatabase.CreateFolder(System.IO.Directory.GetParent(path).ToString(), ANIMATION_FOLDER);
         }
 
         foreach (AnimationClip sourceClip in clips)
         {
             AnimationProperties ap = animationProperties.Where(x => x.Name == sourceClip.name).FirstOrDefault();
             if (ap != null)
             {
                 string clipPath = clipFolder + "/" + sourceClip.name + ".anim";
                 Debug.Log(string.Format("Saving:    {0} at {1}", sourceClip.name, clipPath));
                 AnimationClip destClip = AssetDatabase.LoadAssetAtPath(clipPath, typeof(AnimationClip)) as AnimationClip;
                 if (destClip == null)
                 {
                     Debug.Log("new clip");
                     destClip = new AnimationClip();
                     AssetDatabase.CreateAsset(destClip, clipPath);
                 }
                 EditorUtility.CopySerialized(sourceClip, destClip);
                 List<AnimationEvent> events = new List<AnimationEvent>();
                 events.Add(new AnimationEvent() { time = 0, functionName = "onAnimationEvent", intParameter = (int)AnimationEventType.Start, stringParameter = destClip.name });
                 events.Add(new AnimationEvent() { time = destClip.length, functionName = "onAnimationEvent", intParameter = (int)AnimationEventType.End, stringParameter = destClip.name });
                 if (ap.procTime != 0)
                 {
                     events.Add(new AnimationEvent() { time = destClip.length * ap.procTime, functionName = "onAnimationEvent", intParameter = (int)AnimationEventType.Proc, stringParameter = destClip.name });
                 }
 
                 AnimationUtility.SetAnimationEvents(destClip, events.ToArray());
             }
             else
             {
                 Debug.LogWarning("not found settings WTF?!:" + sourceClip.name);
             }
 
         }
 
         Debug.Log(string.Join(",", clips.Select(x => x.name).ToArray()));
 
 
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
         Debug.Log("Export Done");
         //AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(_targetObject));
     }
 
     private void SaveExportSettings()
     {
         ModelImporter modelImporter = (ModelImporter)AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(obj));
 
         SerializedObject so = new SerializedObject(modelImporter);
 
         SerializedProperty clips = so.FindProperty("m_ClipAnimations");
         List<AnimationEvent[]> animationEvents = new List<AnimationEvent[]>(modelImporter.clipAnimations.Length);
 
         for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
         {
             modelImporter.clipAnimations[i].wrapMode = WrapMode.ClampForever;
             List<AnimationEvent> events = new List<AnimationEvent>();
             //events.AddRange(GetEvents(clips.GetArrayElementAtIndex(i)));//if we need 2 use old events
             events.Add(new AnimationEvent() { stringParameter = animationProperties[i].Name, functionName = "onAnimationEvent", time = 0, intParameter = 0 });
             if (animationProperties[i].procTime != 0)
                 events.Add(new AnimationEvent() { stringParameter = animationProperties[i].Name, functionName = "onAnimationEvent", time = animationProperties[i].procTime, intParameter = 1 });
 
             events.Add(new AnimationEvent() { stringParameter = animationProperties[i].Name, functionName = "onAnimationEvent", time = 1, intParameter = 2 });
             animationEvents.Add(events.ToArray());
         }
 
         // Make your changes and write them by setting clipAnimations, destroying the events.
         modelImporter.clipAnimations = modelImporter.clipAnimations;
 
         for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
         {
 
             SetEvents(clips.GetArrayElementAtIndex(i), animationEvents[i]);
         }
         so.SetIsDifferentCacheDirty();
         so.ApplyModifiedProperties();
         AssetDatabase.SaveAssets();
         AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(_targetObject));
     }
 
     private void UpdateAnimationList()
     {
         animationProperties.Clear();
         ModelImporter modelImporter = (ModelImporter)AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(_targetObject));
         SerializedObject so = new SerializedObject(modelImporter);
         SerializedProperty clips = so.FindProperty("m_ClipAnimations");
         for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
         {
             ModelImporterClipAnimation iClipAnim = modelImporter.clipAnimations[i];
             animationProperties.Add(new AnimationProperties(iClipAnim.name, modelImporter.clipAnimations[i].lastFrame - modelImporter.clipAnimations[i].firstFrame));
         }
 
     }
 
     private void SetAnimationClips()
     {
         Animation targetAnimationComponent = Selection.activeGameObject.animation;
 
         if (targetAnimationComponent == null)
         {
             ShowNotification(new GUIContent("No animation selected!", "Select a gameobject with animation component."));
         }
         else
         {
             WrapMode savedWrapMode = targetAnimationComponent.wrapMode;
             bool savedPlayAutomaticallyState = targetAnimationComponent.playAutomatically;
             bool savedAnimatePhysicsState = targetAnimationComponent.animatePhysics;
 
             DestroyImmediate(targetAnimationComponent);
 
             targetAnimationComponent = Selection.activeGameObject.AddComponent<Animation>();
             targetAnimationComponent.wrapMode = savedWrapMode;
             targetAnimationComponent.playAutomatically = savedPlayAutomaticallyState;
             targetAnimationComponent.animatePhysics = savedAnimatePhysicsState;
 
 
 
 
             string path = AssetDatabase.GetAssetPath(obj);
             Object[] assets = AssetDatabase.LoadAllAssetsAtPath(path);
             List<AnimationClip> clips = assets.ToList().Where(x => x is AnimationClip && !x.name.Contains("__preview__")).Cast<AnimationClip>().ToList();
 
             string clipFolder = System.IO.Path.Combine(System.IO.Directory.GetParent(path).ToString(), ANIMATION_FOLDER);
 
 
             foreach (AnimationClip sourceClip in clips)
             {
                 string clipPath = clipFolder + "/" + sourceClip.name + ".anim";
                 AnimationClip destClip = AssetDatabase.LoadAssetAtPath(clipPath, typeof(AnimationClip)) as AnimationClip;
                 targetAnimationComponent.AddClip(destClip, destClip.name);
 
                 if (targetAnimationComponent.clip == null)
                 {
                     targetAnimationComponent.clip = destClip;
                 }
             }
         }
     }
 
 
     public AnimationEvent[] GetEvents(SerializedProperty sp)
     {
         SerializedProperty serializedProperty = sp.FindPropertyRelative("events");
         AnimationEvent[] array = null;
 
         if (serializedProperty != null && serializedProperty.isArray)
         {
             int count = serializedProperty.arraySize;
             array = new AnimationEvent[count];
 
             for (int i = 0; i < count; i++)
             {
                 AnimationEvent animationEvent = new AnimationEvent();
 
                 SerializedProperty eventProperty = serializedProperty.GetArrayElementAtIndex(i);
                 animationEvent.floatParameter = eventProperty.FindPropertyRelative("floatParameter").floatValue;
                 animationEvent.functionName = eventProperty.FindPropertyRelative("functionName").stringValue;
                 animationEvent.intParameter = eventProperty.FindPropertyRelative("intParameter").intValue;
                 animationEvent.objectReferenceParameter = eventProperty.FindPropertyRelative("objectReferenceParameter").objectReferenceValue;
                 animationEvent.stringParameter = eventProperty.FindPropertyRelative("data").stringValue;
                 animationEvent.time = eventProperty.FindPropertyRelative("time").floatValue;
                 array[i] = animationEvent;
             }
         }
         return array;
     }
 
     public void SetEvents(SerializedProperty sp, AnimationEvent[] newEvents)
     {
         SerializedProperty serializedProperty = sp.FindPropertyRelative("events");
         if (serializedProperty != null && serializedProperty.isArray && newEvents != null && newEvents.Length > 0)
         {
             serializedProperty.ClearArray();
             for (int i = 0; i < newEvents.Length; i++)
             {
                 AnimationEvent animationEvent = newEvents[i];
                 serializedProperty.InsertArrayElementAtIndex(serializedProperty.arraySize);
 
                 SerializedProperty eventProperty = serializedProperty.GetArrayElementAtIndex(i);
                 eventProperty.FindPropertyRelative("floatParameter").floatValue = animationEvent.floatParameter;
                 eventProperty.FindPropertyRelative("functionName").stringValue = animationEvent.functionName;
                 eventProperty.FindPropertyRelative("intParameter").intValue = animationEvent.intParameter;
                 eventProperty.FindPropertyRelative("objectReferenceParameter").objectReferenceValue = animationEvent.objectReferenceParameter;
                 eventProperty.FindPropertyRelative("data").stringValue = animationEvent.stringParameter;
                 eventProperty.FindPropertyRelative("time").floatValue = animationEvent.time;
             }
         }
     }
 }
 

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SethMeshko · May 01, 2019 at 01:08 PM 0
Share

@dnkira , This script is outdated. Any chance you've updated it? Thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

3ds Max importer is broken 3 Answers

Button doesn't work when returning to scene (android) 1 Answer

Should I initialize my Unity events? 1 Answer

3d max importing problems 0 Answers

Is it possible to "trickle" down events or listen to events on parent gameobjects using UnityEnging.EventSystem? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges