BS 0.1 基础构建完成 / 0.2 Visual 同为Unity UI控件部分
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class ES3AutoSave : MonoBehaviour, ISerializationCallbackReceiver
|
||||
{
|
||||
public bool saveLayer = true;
|
||||
public bool saveTag = true;
|
||||
public bool saveName = true;
|
||||
public bool saveHideFlags = true;
|
||||
public bool saveActive = true;
|
||||
public bool saveChildren = false;
|
||||
|
||||
private bool isQuitting = false;
|
||||
|
||||
//[HideInInspector]
|
||||
public List<Component> componentsToSave = new List<Component>();
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
// Initialise saveLayer (etc) to false for all new Components.
|
||||
saveLayer = false;
|
||||
saveTag = false;
|
||||
saveName = false;
|
||||
saveHideFlags = false;
|
||||
saveActive = false;
|
||||
saveChildren = false;
|
||||
}
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
if (ES3AutoSaveMgr.Current == null)
|
||||
ES3Internal.ES3Debug.LogWarning("<b>No GameObjects in this scene will be autosaved</b> because there is no Easy Save 3 Manager. To add a manager to this scene, exit playmode and go to Assets > Easy Save 3 > Add Manager to Scene.", this);
|
||||
else
|
||||
ES3AutoSaveMgr.AddAutoSave(this);
|
||||
}
|
||||
|
||||
public void OnApplicationQuit()
|
||||
{
|
||||
isQuitting = true;
|
||||
}
|
||||
|
||||
public void OnDestroy()
|
||||
{
|
||||
// If this is being destroyed, but not because the application is quitting,
|
||||
// remove the AutoSave from the manager.
|
||||
if (!isQuitting)
|
||||
ES3AutoSaveMgr.RemoveAutoSave(this);
|
||||
}
|
||||
public void OnBeforeSerialize() { }
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
// Remove any null Components
|
||||
componentsToSave.RemoveAll(c => c == null || c.GetType() == typeof(Component));
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 132d687c0c0cb9e489632d423b4c376d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
160
Convention/[ES3]/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs
Normal file
160
Convention/[ES3]/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Linq;
|
||||
|
||||
#if UNITY_VISUAL_SCRIPTING
|
||||
[Unity.VisualScripting.IncludeInSettings(true)]
|
||||
#elif BOLT_VISUAL_SCRIPTING
|
||||
[Ludiq.IncludeInSettings(true)]
|
||||
#endif
|
||||
public class ES3AutoSaveMgr : MonoBehaviour
|
||||
{
|
||||
public static ES3AutoSaveMgr _current = null;
|
||||
public static ES3AutoSaveMgr Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_current == null /*|| _current.gameObject.scene != SceneManager.GetActiveScene()*/)
|
||||
{
|
||||
var scene = SceneManager.GetActiveScene();
|
||||
var roots = scene.GetRootGameObjects();
|
||||
|
||||
// First, look for Easy Save 3 Manager in the top-level.
|
||||
foreach (var root in roots)
|
||||
if (root.name == "Easy Save 3 Manager")
|
||||
return _current = root.GetComponent<ES3AutoSaveMgr>();
|
||||
|
||||
// If the user has moved or renamed the Easy Save 3 Manager, we need to perform a deep search.
|
||||
foreach (var root in roots)
|
||||
if ((_current = root.GetComponentInChildren<ES3AutoSaveMgr>()) != null)
|
||||
return _current;
|
||||
}
|
||||
return _current;
|
||||
}
|
||||
}
|
||||
|
||||
// Included for backwards compatibility.
|
||||
public static ES3AutoSaveMgr Instance
|
||||
{
|
||||
get { return Current; }
|
||||
}
|
||||
|
||||
public enum LoadEvent { None, Awake, Start }
|
||||
public enum SaveEvent { None, OnApplicationQuit, OnApplicationPause }
|
||||
|
||||
public string key = System.Guid.NewGuid().ToString();
|
||||
public SaveEvent saveEvent = SaveEvent.OnApplicationQuit;
|
||||
public LoadEvent loadEvent = LoadEvent.Awake;
|
||||
public ES3SerializableSettings settings = new ES3SerializableSettings("AutoSave.es3", ES3.Location.Cache);
|
||||
|
||||
public HashSet<ES3AutoSave> autoSaves = new HashSet<ES3AutoSave>();
|
||||
|
||||
public void Save()
|
||||
{
|
||||
if (autoSaves == null || autoSaves.Count == 0)
|
||||
return;
|
||||
|
||||
// If we're using caching and we've not already cached this file, cache it.
|
||||
if (settings.location == ES3.Location.Cache && !ES3.FileExists(settings))
|
||||
ES3.CacheFile(settings);
|
||||
|
||||
if (autoSaves == null || autoSaves.Count == 0)
|
||||
{
|
||||
ES3.DeleteKey(key, settings);
|
||||
}
|
||||
else
|
||||
{
|
||||
var gameObjects = new List<GameObject>();
|
||||
foreach (var autoSave in autoSaves)
|
||||
{
|
||||
// If the ES3AutoSave component is disabled, don't save it.
|
||||
if (autoSave != null && autoSave.enabled)
|
||||
gameObjects.Add(autoSave.gameObject);
|
||||
}
|
||||
// Save in the same order as their depth in the hierarchy.
|
||||
ES3.Save<GameObject[]>(key, gameObjects.OrderBy(x => GetDepth(x.transform)).ToArray(), settings);
|
||||
}
|
||||
|
||||
if(settings.location == ES3.Location.Cache && ES3.FileExists(settings))
|
||||
ES3.StoreCachedFile(settings);
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
// If we're using caching and we've not already cached this file, cache it.
|
||||
if (settings.location == ES3.Location.Cache && !ES3.FileExists(settings))
|
||||
ES3.CacheFile(settings);
|
||||
}
|
||||
catch { }
|
||||
|
||||
ES3.Load<GameObject[]>(key, new GameObject[0], settings);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
if(loadEvent == LoadEvent.Start)
|
||||
Load();
|
||||
}
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
GetAutoSaves();
|
||||
|
||||
if (loadEvent == LoadEvent.Awake)
|
||||
Load();
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
if(saveEvent == SaveEvent.OnApplicationQuit)
|
||||
Save();
|
||||
}
|
||||
|
||||
void OnApplicationPause(bool paused)
|
||||
{
|
||||
if( (saveEvent == SaveEvent.OnApplicationPause ||
|
||||
(Application.isMobilePlatform && saveEvent == SaveEvent.OnApplicationQuit)) && paused)
|
||||
Save();
|
||||
}
|
||||
|
||||
/* Register an ES3AutoSave with the ES3AutoSaveMgr, if there is one */
|
||||
public static void AddAutoSave(ES3AutoSave autoSave)
|
||||
{
|
||||
if(ES3AutoSaveMgr.Current != null)
|
||||
ES3AutoSaveMgr.Current.autoSaves.Add(autoSave);
|
||||
}
|
||||
|
||||
/* Remove an ES3AutoSave from the ES3AutoSaveMgr, for example if it's GameObject has been destroyed */
|
||||
public static void RemoveAutoSave(ES3AutoSave autoSave)
|
||||
{
|
||||
if(ES3AutoSaveMgr.Current != null)
|
||||
ES3AutoSaveMgr.Current.autoSaves.Remove(autoSave);
|
||||
}
|
||||
|
||||
/* Gathers all of the ES3AutoSave Components in the scene and registers them with the manager */
|
||||
public void GetAutoSaves()
|
||||
{
|
||||
autoSaves = new HashSet<ES3AutoSave>();
|
||||
|
||||
foreach (var go in this.gameObject.scene.GetRootGameObjects())
|
||||
autoSaves.UnionWith(go.GetComponentsInChildren<ES3AutoSave>(true));
|
||||
}
|
||||
|
||||
// Gets the depth of a Transform in the hierarchy.
|
||||
static int GetDepth(Transform t)
|
||||
{
|
||||
int depth = 0;
|
||||
|
||||
while (t.parent != null)
|
||||
{
|
||||
t = t.parent;
|
||||
depth++;
|
||||
}
|
||||
|
||||
return depth;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc4c084043dc014459977604947a0271
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user