/// <summary>
    /// Assigns the animation to the character.
    /// </summary>
    static void AssignAnimationToCharacter(AnimationClip clip, GameObject character)
    {
        //create a new controller
        UnityEditorInternal.AnimatorController my_controller = new UnityEditorInternal.AnimatorController();
        my_controller.name = "generic_controller";

        //check if the animator component is already attached to the character
        if (character.GetComponent<Animator>() == null)
            character.AddComponent<Animator>();

        //create the state machine with the animation clip
        StateMachine sm = new StateMachine();
        sm.AddState("default_state");
        sm.GetState(0).SetMotion(0, clip);

        //check if the controller already has a based layer
        if (my_controller.GetLayerCount() == 0)
            my_controller.AddLayer("Base Layer");
        //set the state machine
        my_controller.SetLayerStateMachine(0, sm);

        //assign the controller
        Animator animator = (Animator)character.GetComponent<Animator>();
        UnityEditorInternal.AnimatorController.SetAnimatorController(animator,my_controller);
    }
Ejemplo n.º 2
0
    private static string[] GetTransitionNames(Animator animator, int layer)
    {
        List <string> transitionNames = new List <string>();

        UnityEditorInternal.AnimatorController ac           = animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
        UnityEditorInternal.StateMachine       stateMachine = ac.GetLayer(layer).stateMachine;

        FromStateMachineToTransitionName(stateMachine, transitionNames);

        return(transitionNames.ToArray());
    }
Ejemplo n.º 3
0
    private static int[] GetStateKeys(Animator animator, int layer)
    {
        List <int> stateKeys = new List <int>();

        UnityEditorInternal.AnimatorController ac           = animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
        UnityEditorInternal.StateMachine       stateMachine = ac.GetLayer(layer).stateMachine;

        FromStateMachineToStateKey(stateMachine, stateKeys);

        return(stateKeys.ToArray());
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Gets the count of transitions in a layer.
    /// </summary>
    /// <returns>
    /// The transition count.
    /// </returns>
    /// <param name='animator'>
    /// Animator.
    /// </param>
    /// <param name='layer'>
    /// Layer.
    /// </param>
    public static int GetTransitionsCount(Animator animator, int layer)
    {
        UnityEditorInternal.AnimatorController ac           = animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
        UnityEditorInternal.StateMachine       stateMachine = ac.GetLayer(layer).stateMachine;

        int counter = 0;

        for (int i = 0; i < stateMachine.stateCount; i++)
        {
            Transition[] trans = stateMachine.GetTransitionsFromState(stateMachine.GetState(i));
            counter += trans.Length;
        }

        return(counter);

//		return stateMachine.transitionCount;
    }
Ejemplo n.º 5
0
    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 100, 100, 40), new GUIContent("获取State")))
        {
            UnityEditorInternal.AnimatorController animController = Animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController;

            int layerCount = animController.layerCount;
            Debug.Log(string.Format("动画层数量: {0}", layerCount));

            // 动画层名称
            for (int layer = 0; layer < layerCount; layer++)
            {
                Debug.Log(string.Format("Layer {0}: {1}", layer, animController.GetLayer(layer).name));
            }

            // 动画层0上面的State
            UnityEditorInternal.StateMachine sm = animController.GetLayer(0).stateMachine;
            for (int i = 0; i < sm.stateCount; i++)
            {
                UnityEditorInternal.State state = sm.GetState(i);
                Debug.Log(string.Format("State: {0}, 唯一名称: {1}", state.name, state.uniqueName));
            }
        }
    }
	// Create and assign controller and animation
	void SetAnimator()
	{
		// Add the component if it's not already there
		if (gameObject.GetComponent<Animator>() == null)
			gameObject.AddComponent<Animator>();
			
		charAnimator = (Animator)gameObject.GetComponent<Animator>();

		// Set speed to 0 so no real animation takes place
		charAnimator.speed = 0.0f;
		
		UnityEditorInternal.AnimatorController aController = new UnityEditorInternal.AnimatorController();
		aController.name = "animation_controller";
		
		if (aController.GetLayerCount() == 0)
			aController.AddLayer("Base");
		
		StateMachine sm = new StateMachine();
		sm.AddState("default");
		
		// Add clip
		sm.GetState(0).SetMotion(0, (AnimationClip)animations[animCount]);
		animCount++;
		
		aController.SetLayerStateMachine(0, sm);
		
		UnityEditorInternal.AnimatorController.SetAnimatorController(charAnimator, aController);
		
		// Set time
		currTime = charAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime;
	}