コード例 #1
0
    /// <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);
    }
コード例 #2
0
	// 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;
	}