- Home /
Check Available Variables of Mecanim Animator
Is there a way to check which variables I can set on a Mecanim animator?
I have lots of NPCs with sets of animation clips that I want to select from at random. For example, here's a set of Idle animations:
So I simply added a Variable called Random100
to all my animators that is, once per second, assigned a random number between 0 and 99, allowing my Mecanim state machine to transition (eg. with 30% likelihood it could transition to StandAround
or LookBored
, with 10% likelihood it could transition to Stretch
, Yawn
or ScratchHead
.
It would be nice it I could make this Random100
a built-in feature of my controllers that, when present, gets fed a random number regularly. However, I couldn't find a convenient HasInteger()
or similar method to detect whether there is a variable called Random100
in my animator.
Is there a way to check for this?
The layout of your animation controller is unsettling...
The problem is your pentagram is missing candles and an incantation.
Oh wait, $$anonymous$$ecanim.
Answer by CapeGuyBen · Mar 05, 2015 at 04:40 PM
I know this is quite an old thread but it hasn't really been answered so thought I'd share my solution.
I wrote the following extension method for Animator:
public static partial class AnimatorExtensions {
public static bool HasParameterOfType (this Animator self, string name, AnimatorControllerParameterType type) {
var parameters = self.parameters;
foreach (var currParam in parameters) {
if (currParam.type == type && currParam.name == name) {
return true;
}
}
return false;
}
}
You can now test if a given parameter exists on an animator instance using:
Animator myAnimator = GetComponent<Animator> ();
bool hasMyParam = myAnimator.HasParameterOfType ("MyParam", AnimatorControllerParameterType.Int);
it's also easy to write a version which compares 'nameHash' instead of 'name' if that's more useful to you.
thanks for the extension mate, I rewrote the code to correct the error at the top, (static partial), not (partial static) i also wrote it to accommodate the situation of a null animator using UnityEngine;
public static partial class AnimatorExtension {
public static bool HasParameterOfType (this Animator self, string name, AnimatorControllerParameterType type) {
if(null == self){
return false;
}
var parameters = self.parameters;
foreach (var currParam in parameters) {
if (currParam.type == type && currParam.name == name) {
return true;
}
}
return false;
}
}
Thanks for the feedback. I fixed the static partial typo in the original post.
Not sure I agree with testing for null self though. It's not usual to handle null 'this' dereferences gracefully. I would expect an exception (specifically a System.NullReferenceException) to be thrown - as the original implementation will do.
Whatever works for you though :-)
Answer by Xtro · Aug 20, 2013 at 02:11 PM
When you try to read from a variable, you get an error if it doesn't exist. You can catch that error to see if it exists or not.
This is a very basic programming concept. Not related with Unity at all.
A very basic program$$anonymous$$g concept is also to not rely on exceptions for normal code flow control. Especially if said exceptions, even if caught, produce a warning in the output window.
This question is related to Unity in that I am asking for a way to check the existence of a variable via the $$anonymous$$ecanim API, which I couldn't find in the docs.
Finally, no offense intended, but I would recommend some humble sauce, especially when one's own recommendation betrays a dangerous half-knowledge of program$$anonymous$$g concepts.
Relying on exceptions is inevitable when the existence check is not present. If you can tell me a better idea, I'll be happy to hear.
Errors in output window can be prevented if you consume the exception. I'm not sure about warnings.
I didn't get what you mean by "half-knowledge". Did you think I have half-knowledge because I recommend the use of exceptions when the existence check is not present ??
That's why I asked this question, I was hoping someone might have figured out a better way to check for this.
The warning is sadly generated before the exception is thrown, so even if I set some flag like variableNotPresent
the first time I catch the exception, one warning will always trigger. I've now avoided it by adding a public bool ProvideRandomNumbers
so I can turn it on and off on a per-case basis in the inspector window.
I just tried to mirror the attitude I perceived from you. Your answer sounded like it would be the right and proper way to rely on exceptions and that I'm wasting people's time even asking for a method to check a variable's existence.
$$anonymous$$y attitude was quite and nice since I was trying to help you for free spending my office time. If you don't like the answer or you are already aware of the answer, please just ignore it quietly.
Btw, I really hate the idea of avoiding exception use. They are there for a reason. Just use it people! (When there is no alternative checking method)
Answer by TonyLi · Aug 22, 2013 at 03:50 PM
I don't think this functionality has been exposed yet, not even in Unity 4.2. At least I don't see it in any of the release notes or documentation.
I know it's far from ideal, but what about adding a set of Boolean parameters guaranteed to exist on all of your animator controllers that indicate what the controller supports -- for example, a hasRandom100 Boolean that you can check.
He already thought of that idea.
C'mon guys. This is uglier than the exception testing.
Answer by meat5000 · Mar 06, 2015 at 01:18 AM
You mean parameters?
Use this recent addition to the API (I think)
http://docs.unity3d.com/ScriptReference/Animator-parameters.html
Strange implementation... Trying it right now. But with this you need to check if a concrete parametrer exists in the array. Won't be better to use a lyst?
Your answer
Follow this Question
Related Questions
Checking Transitions in Animator 0 Answers
How to setup this melee animation system in Mecanim? 1 Answer
Is it possible to play animator transition in script? 1 Answer
Transition to multiple states from Entry Node in Unity 5 1 Answer
Can't add more than one transtition to animator controller. 0 Answers