23/11/21 15:33:49.92 KEuaTtq8.net
>>109
1 Assetsフォルダ内にResourcesという名前のフォルダを作ります。
2 Resourcesフォルダの中にスプライトエディターで分割した画像を入れます。
3 スクリプトからスプライト型の配列としてロードします。
public class Player : MonoBehaviour
{
Sprite[] sps; //スプライト画像の配列
SpriteRenderer spr; //スプライトレンダラーコンポネント取得変数
// Start is called before the first frame update
void Start()
{
//スプライトレンダラー取得
spr = GetComponent<SpriteRenderer>();
//Resourcesフォルダ内のPlayer画像取得
sps = Resources.LoadAll<Sprite>("Player");
}
// Update is called once per frame
void Update()
{
//キーが押されたら画像変更
if (Input.GetKeyDown("a")) { spr.sprite = sps[0]; }
if (Input.GetKeyDown("d")) { spr.sprite = sps[1]; }
}
}