【サバイバルホラーゲームを作りたい。その41】
今回はセーブ機能を実装します。
今回はPlayerPrefsが重要になります。概要は下記参照。
www.sejuku.net
・Scene001内で空オブジェクトを作り、下記スクリプトをアタッチします。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Auto01 : MonoBehaviour //EP41追加 AutoSavaオブジェクトに追加 { // Start is called before the first frame update void Start() { PlayerPrefs.SetInt("AutoSave", 2); } }
・Scene002内で空オブジェクトを作り、下記スクリプトをアタッチします。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Auto02 : MonoBehaviour //EP41追加 AutoSavaオブジェクトに追加 { // Start is called before the first frame update void Start() { PlayerPrefs.SetInt("AutoSave", 5); } }
・MainMenuシーンのMainMenuFunctionスクリプトを変更します。
public class MainMenuFunction : MonoBehaviour //EP25追加 { public GameObject fadeOut; public GameObject loadText; public AudioSource buttonClick; public GameObject loadButton; //EP41追加 [SerializeField] int loadInt; //EP41追加 // Start is called before the first frame update void Start() //EP41追加 { loadInt = PlayerPrefs.GetInt("AutoSave"); if (loadInt > 0) { loadButton.SetActive(true); } } public void NewGameButton() { StartCoroutine(NewGameStart()); } IEnumerator NewGameStart() { fadeOut.SetActive(true); buttonClick.Play(); yield return new WaitForSeconds(3); loadText.SetActive(true); SceneManager.LoadScene(4); } //EP41追加 public void LoadGameButton() { StartCoroutine(LoadGameStart()); } //EP41追加 IEnumerator LoadGameStart() { fadeOut.SetActive(true); buttonClick.Play(); yield return new WaitForSeconds(3); loadText.SetActive(true); SceneManager.LoadScene(loadInt); } }
今回は以上です。