更改控制器动画师和精灵渲染器

时间:2016-05-06 00:35:54

标签: c# animation unity3d unityscript unity5

如何通过简单的按钮“使用”允许用户启用更改动画控制器&精灵渲染器?

在为角色购买新皮肤后,他点击“使用”,当他进入游戏时,皮肤就会启用。

我已经有5个准备好的动画师控制器(Bee1,Bee2,Bee3,Bee4)和Bee as defaut。

精灵渲染已准备就绪(Bee1_0,Bee2_0,Bee3_0,Bee4_0)和Bee_0为默认值。

请尽可能使用C#(使用Unity3D)

如有必要,在脚本播放器下方

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;

public class Bee : MonoBehaviour {

    public float moveSpeed;

    public Transform bee;
    private Animator animator;

    public bool isGrounded = true;
    public float force;

    public float jumpTime = 0.1f;
    public float jumpDelay = 0.1f;
    public bool jumped = false;
    public Transform ground;


    // Use this for initialization
    void Start ()
    {
        animator = bee.GetComponent<Animator> ();
    }

    void Update ()
    {
        Move ();


    }


    void Move ()
    {

        isGrounded = Physics2D.Linecast (this.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor"));
        animator.SetFloat ("runB", Mathf.Abs (CrossPlatformInputManager.GetAxis ("Horizontal")));
        if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") > 0) {

            transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 0);
        }
        if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") < 0) {

            transform.Translate (Vector2.right * moveSpeed * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 180);
        }

        if (CrossPlatformInputManager.GetButtonDown ("Vertical") && isGrounded && !jumped) {

            //  rigidbody2D.AddForce (transform.up * force);
            GetComponent<Rigidbody2D> ().AddForce (transform.up * force);
            jumpTime = jumpDelay;
            animator.SetTrigger ("jumpB");
            jumped = true;
        }

        jumpTime -= Time.deltaTime;

        if (jumpTime <= 0 && isGrounded && jumped) {

            animator.SetTrigger ("groundB");
            jumped = false;
         }
    }
}   

1 个答案:

答案 0 :(得分:0)

您要找的是RuntimeAnimatorController。没有测试代码,但用法是

animator = bee.GetComponent< Animator >();
animator.runtimeAnimatorController = Resources.Load("Bee1") as RuntimeAnimatorController;
//or:
RuntimeAnimatorController animator = (RuntimeAnimatorController)RuntimeAnimatorController.Instantiate(Resources.Load("Bee1", typeof(RuntimeAnimatorController)));
bee.GetComponent< Animator >().runtimeAnimatorController = animator;


但如果我是你,我会为所有蜜蜂类型创建预制件并将它们换成父母&#34;玩家游戏对象&#34;当玩家购买新产品并且是时候应用更改了。
更大(文件大小)但更安全(你看它在运行之前是如何工作的,Unity可以优化它,因为它& #39;预制,如果出于某种原因出现故障,可以解决问题,可以加油等等,也可能会更快(特别是如果你缓存不同的蜜蜂类型,以防变化可以很快)。 p>