Created
April 2, 2018 13:07
-
-
Save shana/1ce483049fb5a67503c1464e5d418f9f to your computer and use it in GitHub Desktop.
ScriptObjectSingleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MIT License | |
Copyright (c) 2016-2018 GitHub | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
using System.Linq; | |
using UnityEditorInternal; | |
using UnityEngine; | |
[AttributeUsage(AttributeTargets.Class)] | |
public sealed class LocationAttribute : Attribute | |
{ | |
public enum Location { PreferencesFolder, ProjectFolder, LibraryFolder, UserFolder } | |
public string filepath { get; set; } | |
public LocationAttribute(string relativePath, Location location) | |
{ | |
//Guard.ArgumentNotNullOrWhiteSpace(relativePath, "relativePath"); | |
if (relativePath[0] == '/') | |
relativePath = relativePath.Substring(1); | |
if (location == Location.PreferencesFolder) | |
filepath = InternalEditorUtility.unityPreferencesFolder + "/" + relativePath; | |
//else if (location == Location.UserFolder) | |
// filepath = EntryPoint.Environment.UserCachePath.Combine(relativePath).ToString(SlashMode.Forward); | |
else if (location == Location.LibraryFolder) | |
filepath = Application.dataPath.Substring(0, Application.dataPath.IndexOf("Assets")) + "Library/" + relativePath; | |
} | |
} | |
public class ScriptObjectSingleton<T> : ScriptableObject where T : ScriptableObject | |
{ | |
protected static T instance; | |
public static T Instance | |
{ | |
get | |
{ | |
if (instance == null) | |
CreateAndLoad(); | |
return instance; | |
} | |
} | |
protected ScriptObjectSingleton() | |
{ | |
if (instance != null) | |
{ | |
//LogHelper.Instance.Error("Singleton already exists!"); | |
} | |
else | |
{ | |
instance = this as T; | |
System.Diagnostics.Debug.Assert(instance != null); | |
} | |
} | |
private static void CreateAndLoad() | |
{ | |
System.Diagnostics.Debug.Assert(instance == null); | |
string filePath = GetFilePath(); | |
if (!string.IsNullOrEmpty(filePath)) | |
{ | |
InternalEditorUtility.LoadSerializedFileAndForget(filePath); | |
} | |
if (instance == null) | |
{ | |
var inst = CreateInstance<T>() as ScriptObjectSingleton<T>; | |
inst.hideFlags = HideFlags.HideAndDontSave; | |
inst.Save(true); | |
} | |
System.Diagnostics.Debug.Assert(instance != null); | |
} | |
protected virtual void Save(bool saveAsText) | |
{ | |
if (instance == null) | |
{ | |
//LogHelper.Instance.Error("Cannot save singleton, no instance!"); | |
return; | |
} | |
string locationFilePath = GetFilePath(); | |
if (locationFilePath != null) | |
{ | |
if (!System.IO.Directory.Exists(locationFilePath)) | |
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(locationFilePath)); | |
InternalEditorUtility.SaveToSerializedFileAndForget(new[] { instance }, locationFilePath, saveAsText); | |
} | |
} | |
private static string GetFilePath() | |
{ | |
var attr = typeof(T).GetCustomAttributes(true) | |
.Select(t => t as LocationAttribute) | |
.FirstOrDefault(t => t != null); | |
//LogHelper.Instance.Debug("FilePath {0}", attr != null ? attr.filepath : null); | |
if (attr == null) | |
return null; | |
return attr.filepath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment