Hello,
I have been looking through numerous solutions to this problem and im not sure they meet my situation. I have an FBX blender model that has read only animations. I need to add keyframes/events to this animation to be able to call a function. My animations are contained within my model though, so if I try to duplicate the animation like other posts have said, it is not possible. I’ve heard that if you put the model in the scene, then make a prefab of that model, then it would work, but I already have a prefab based off this model… Does anyone have any experience with this situation? I have looked at this post already:
if someone could offer a more detailed solution they used that would be awesome. Thanks,
When Unity imports the FBX, the animation clip by default (at least on my Unity) is given a name like ‘Anim|Cube’. You have to rename this in the import settings for the object, removing the | characters. Then after clicking apply, you can click on your clip in the project browser, press Ctrl-D and it should give you a copy outside the object.
I’m also using read-only animations. And I’ve also tried duplicating my animations and edit them manually. But it’s troublesome. So i add my events to my animations at runtime once in Start(). For instance this is an event which calls a function with a float parameter:
AnimationEvent animationEvent = new AnimationEvent();
animationEvent.functionName = "your method name to be called"
animationEvent.floatParameter = "single type parameter you want to pass"
animationEvent.time = "time when to trigger event"
Animation["state you wish to add event"].clip.AddEvent(animationEvent);
Then define your method in your script and it will be fired off as you described.
Good solution... but "Animation["state you wish to add event"].clip.AddEvent(animationEvent);" not work for me. I modify your code: void AddEvent(int MyClip, float time, string functionName, float floatParameter) { anim = GetComponent(); AnimationEvent animationEvent = new AnimationEvent(); animationEvent.functionName = functionName; animationEvent.floatParameter = floatParameter; animationEvent.time = time; AnimationClip clip = anim.runtimeAnimatorController.animationClips[MyClip]; clip.AddEvent(animationEvent); }
But beware, in the presence of multiple objects with the same animation, the event is added several times because the animation is always one for all the objects.
You need to add a static variable that specifies that the event should only be added once.
Omg thanks work very well! Thank you!
– DiegoMontania