Trạng thái trò chơi

0
228

Trạng thái trò chơi
Đã kiểm tra với phiên bản: 5.5
– –
Độ khó: Trung cấp
Giai đoạn 6 dạy bạn làm thế nào để tạo ra một hệ thống để tải các cảnh trong khi bảo quản trạng thái trò chơi bằng cách sử dụng Scene Manager, ScriptableObjects như lưu trữ dữ liệu thời gian chạy tạm thời, các đại biểu và các biểu thức lambda. Tải xuống gói nội dung cho Giai đoạn 6 tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.57590442.1082230255.1536856804-659907597.1536596969#!/content/76791
Tải xuống gói nội dung cho Giai đoạn 6 tại Cửa hàng tài sản hợp nhất tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.158294330.1082230255.1536856804-659907597.1536596969#!/content/76791

SceneController

Expand view

Copy code
using System;
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SceneController : MonoBehaviour
{
public event Action BeforeSceneUnload;
public event Action AfterSceneLoad;
public CanvasGroup faderCanvasGroup;
public float fadeDuration = 1f;
public string startingSceneName = "SecurityRoom";
public string initialStartingPositionName = "DoorToMarket";
public SaveData playerSaveData;


private bool isFading;
private IEnumerator Start ()
{
faderCanvasGroup.alpha = 1f;
playerSaveData.Save (PlayerMovement.startingPositionKey, initialStartingPositionName);
yield return StartCoroutine (LoadSceneAndSetActive (startingSceneName));
StartCoroutine (Fade (0f));
}
public void FadeAndLoadScene (SceneReaction sceneReaction)
{
if (!isFading)
{
StartCoroutine (FadeAndSwitchScenes (sceneReaction.sceneName));
}
}
private IEnumerator FadeAndSwitchScenes (string sceneName)
{
yield return StartCoroutine (Fade (1f));
if (BeforeSceneUnload != null)
BeforeSceneUnload ();
yield return SceneManager.UnloadSceneAsync (SceneManager.GetActiveScene ().buildIndex);
yield return StartCoroutine (LoadSceneAndSetActive (sceneName));
if (AfterSceneLoad != null)
AfterSceneLoad ();

yield return StartCoroutine (Fade (0f));
}
private IEnumerator LoadSceneAndSetActive (string sceneName)
{
yield return SceneManager.LoadSceneAsync (sceneName, LoadSceneMode.Additive);
Scene newlyLoadedScene = SceneManager.GetSceneAt (SceneManager.sceneCount - 1);
SceneManager.SetActiveScene (newlyLoadedScene);
}
private IEnumerator Fade (float finalAlpha)
{
isFading = true;
faderCanvasGroup.blocksRaycasts = true;
float fadeSpeed = Mathf.Abs (faderCanvasGroup.alpha - finalAlpha) / fadeDuration;
while (!Mathf.Approximately (faderCanvasGroup.alpha, finalAlpha))
{
faderCanvasGroup.alpha = Mathf.MoveTowards (faderCanvasGroup.alpha, finalAlpha,
fadeSpeed * Time.deltaTime);
yield return null;
}
isFading = false;
faderCanvasGroup.blocksRaycasts = false;
}
}


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38


39


40


41


42


43


44


45


46


47


48


49


50


51


52


53


54


55


56


57


58


59


60


61


62


63


SaveData

Expand view

Copy code
using System;
using UnityEngine;
using System.Collections.Generic;
[CreateAssetMenu]
public class SaveData : ResettableScriptableObject
{
[Serializable]
public class KeyValuePairLists<T>
{
public List<string> keys = new List<string>();
public List<T> values = new List<T>();
public void Clear ()
{
keys.Clear ();
values.Clear ();
}
public void TrySetValue (string key, T value)
{
int index = keys.FindIndex(x => x == key);
if (index > -1)
{
values[index] = value;
}
else
{
keys.Add (key);
values.Add (value);
}
}
public bool TryGetValue (string key, ref T value)
{
int index = keys.FindIndex(x => x == key);
if (index > -1)
{
value = values[index];
return true;
}
return false;
}
}
public KeyValuePairLists<bool> boolKeyValuePairLists = new KeyValuePairLists<bool> ();
public KeyValuePairLists<int> intKeyValuePairLists = new KeyValuePairLists<int>();
public KeyValuePairLists<string> stringKeyValuePairLists = new KeyValuePairLists<string>();
public KeyValuePairLists<Vector3> vector3KeyValuePairLists = new KeyValuePairLists<Vector3>();
public KeyValuePairLists<Quaternion> quaternionKeyValuePairLists = new KeyValuePairLists<Quaternion>();
public override void Reset ()
{
boolKeyValuePairLists.Clear ();
intKeyValuePairLists.Clear ();
stringKeyValuePairLists.Clear ();
vector3KeyValuePairLists.Clear ();
quaternionKeyValuePairLists.Clear ();
}
private void Save<T>(KeyValuePairLists<T> lists, string key, T value)
{
lists.TrySetValue(key, value);
}
private bool Load<T>(KeyValuePairLists<T> lists, string key, ref T value)
{
return lists.TryGetValue(key, ref value);
}
public void Save (string key, bool value)
{
Save(boolKeyValuePairLists, key, value);
}
public void Save (string key, int value)
{
Save(intKeyValuePairLists, key, value);
}
public void Save (string key, string value)
{
Save(stringKeyValuePairLists, key, value);
}
public void Save (string key, Vector3 value)
{
Save(vector3KeyValuePairLists, key, value);
}
public void Save (string key, Quaternion value)
{
Save(quaternionKeyValuePairLists, key, value);
}
public bool Load (string key, ref bool value)
{
return Load(boolKeyValuePairLists, key, ref value);
}
public bool Load (string key, ref int value)
{
return Load (intKeyValuePairLists, key, ref value);
}
public bool Load (string key, ref string value)
{
return Load (stringKeyValuePairLists, key, ref value);
}
public bool Load (string key, ref Vector3 value)
{
return Load(vector3KeyValuePairLists, key, ref value);
}
public bool Load (string key, ref Quaternion value)
{
return Load (quaternionKeyValuePairLists, key, ref value);
}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here