Mecanim get current Layer

Hi,
maybe i just have the wrong approach, but i’m looking for a way to get
the current layer in Mecanim.
I dont want to check on all clips contained in the layer, as in
AnimatorStateInfo.nameHash //“LayerName.ClipName”
(Animator.stringToHash(“OnlyLayerName”) doesnt trigger)

I have all characters emotes in a layer (set to override) and triggered by an int.
Now i want to check if it’s currently inside the emote layer to reset the emote-int to 0
(and i do not want to check if each of the emotes is currently playing, as this seems very inefficient (around 15 emotes) )

In a general sense, there’s no such thing as a “current layer.” Every layer is active with a weight that determines its influence on the final animation. Of course, override also influences the final animation. As an optimization, Mecanim doesn’t evaluate layers with a weight of 0.

If you only increase your emote layer above 0 when you’re playing an emote, then you can call GetLayerWeight().

If your emote layer’s weight is always 1, then you probably have a null state in the layer. Just check what state you’re in, using GetCurrentAnimatorStateInfo(). This is basically what the example scripts do in Update().

You could also use a coroutine that sets emote-int back to 0 after, say, 0.1 seconds. Start the coroutine in the same code block where you set the emote-int.

I created this extension to get the dominant layer.

using UnityEngine;

public static class Extensions {

	public static int GetDominantLayer(this Animator Animator) {
		int dominant_index = 0;
		float dominant_weight = 0f;
		float weight = 0f;
		for (int index = 0; index < Animator.layerCount; index++) {
			weight = Animator.GetLayerWeight(index);
			if (weight > dominant_weight) {
				dominant_weight = weight;
				dominant_index = index;
			}
		}
		return dominant_index;
	}

}

Basically it iterates through all layers in the animator and searches for the one with the highest weight.