67
62

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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

67
62
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
67
62

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?