Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
61
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

Organization

Unity オブジェクトの画像(Sprite)を動的に変更する方法

SpriteRendererのspriteを外部設定可能にし、またspriteの変更処理を書く


public class Player : MonoBehaviour {
    SpriteRenderer MainSpriteRenderer;
    // publicで宣言し、inspectorで設定可能にする
    public Sprite StandbySprite;
    public Sprite HoldSprite;
    public Sprite SlashSprite;

    void Start ()
    {
       // このobjectのSpriteRendererを取得
        MainSpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
    }

    // 何かしらのタイミングで呼ばれる
    void ChangeStateToHold()
    {
       // SpriteRenderのspriteを設定済みの他のspriteに変更
       // 例) HoldSpriteに変更
        MainSpriteRenderer.sprite = HoldSprite;
    }
}

inspectorで、変更したいscriptを設定

スクリーンショット 2014-06-08 22.45.39.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
61
Help us understand the problem. What are the problem?