这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

70 行
1.7 KiB

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
/// <summary>
/// This class contains the function to call when play button is pressed
/// </summary>
public class StartGame : MonoBehaviour
{
[SerializeField]
private GameSceneSO _locationsToLoad;
[SerializeField]
private bool _showLoadScreen = default;
[SerializeField]
private SaveSystem _saveSystem = default;
private bool _hasSaveData;
[Header("Broadcasting on ")]
[SerializeField]
private LoadEventChannelSO _startGameEvent = default;
[Header("Listening to")]
[SerializeField]
private VoidEventChannelSO _startNewGameEvent = default;
[SerializeField]
private VoidEventChannelSO _continueGameEvent = default;
private void Start()
{
_hasSaveData = _saveSystem.LoadSaveDataFromDisk();
_startNewGameEvent.OnEventRaised += StartNewGame;
_continueGameEvent.OnEventRaised += ContinuePreviousGame;
}
void StartNewGame()
{
_hasSaveData = false;
_saveSystem.WriteEmptySaveFile();
//Start new game
_startGameEvent.RaiseEvent(_locationsToLoad, _showLoadScreen);
}
void ContinuePreviousGame()
{
StartCoroutine(LoadSaveGame());
}
void OnResetSaveDataPress()
{
_hasSaveData = false;
}
IEnumerator LoadSaveGame()
{
yield return StartCoroutine(_saveSystem.LoadSavedInventory());
var locationGuid = _saveSystem.saveData._locationId;
var asyncOperationHandle = Addressables.LoadAssetAsync<LocationSO>(locationGuid);
yield return asyncOperationHandle;
if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
{
LocationSO locationSO = asyncOperationHandle.Result;
_startGameEvent.RaiseEvent(locationSO, _showLoadScreen);
}
}
}