2016-05-06 1 views
1

ユーザーが単純なボタンで「使用」できるようにするには、アニメーターコントローラーを変更してください&スプライトレンダラー?コントローラーのアニメーターとスプライトレンダーを変更する

新しいスキンをキャラクターに購入した後、彼は「使用」をクリックし、ゲームに入るとスキンが有効になります。

私は既に5つの準備ができているアニメータコントローラ(Bee1、Bee2、Bee3、Bee4)とBeeをdefautとして持っています。

スプライトレンダリングは、デフォルトでBee1_0、Bee2_0、Bee3_0、Bee4_0とBee_0の準備ができています。

あなたが探しているものを、必要な

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; 
     } 
    } 
} 

答えて

0

RuntimeAnimatorControllerであればC#のスクリプトプレーヤーの下

(Unity3Dを使用)可能であればしてください。コードをテストしませんでしたが、使用法は

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;


ですが、私があなただったら、すべての蜂の種類のプレハブを作成し、新しいものを購入したときに親の "プレイヤーゲームオブジェクト"にスワップアウトします変更を適用する時間。
より大きい(ファイルサイズで)、より安全です(実行前にどのように動作するかを見ると、Unityはそれをプレハブとして最適化でき、何らかの理由で不具合があった場合に修正できます。やや速いかもしれません(特に、変更が迅速な場合に備えて、異なる種類の蜂をキャッシュする場合)。

関連する問題