-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "BetterSingletons.Editor", | ||
"rootNamespace": "Better.Singletons", | ||
"references": [ | ||
"GUID:af7fffdf1d83bc842b0f6e3a01efda16" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Arcueid D'athemon (https://github.com/uurha) | ||
|
||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Better Singletons | ||
|
||
## Install | ||
[How to install](https://github.com/uurha/BetterPluginCollection/wiki/How-to-install) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "BetterSingletons.Runtime", | ||
"rootNamespace": "Better.Singletons", | ||
"references": [ | ||
"GUID:28da8d3b12e3efa47928e0c9070f853d" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Better.Extensions.Runtime; | ||
using UnityEngine; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace Better.Singletons.Runtime | ||
{ | ||
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T> | ||
{ | ||
private static T _instance; | ||
|
||
public static T Instance | ||
{ | ||
get | ||
{ | ||
ValidateInstance(); | ||
return _instance; | ||
} | ||
} | ||
|
||
private static void ValidateInstance() | ||
{ | ||
var typeName = typeof(T).Name; | ||
if (_instance != null) | ||
{ | ||
return; | ||
} | ||
|
||
var objects = FindObjectsOfType<T>(); | ||
|
||
if (objects.IsNullOrEmpty()) | ||
{ | ||
throw new InvalidOperationException($"[{typeName}] {nameof(ValidateInstance)}: no instance found"); | ||
} | ||
|
||
_instance = objects[0]; | ||
|
||
if (objects.Length > 1) | ||
{ | ||
Debug.LogWarning($"[{typeName}] {nameof(ValidateInstance)}: more than one instance found. Destroying other instances"); | ||
|
||
var others = new List<T>(objects); | ||
others.Remove(_instance); | ||
|
||
//TODO: Maybe add settings to destroy or reassign instance | ||
foreach (var item in others) | ||
{ | ||
Destroy(item.gameObject); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace Better.Singletons.Runtime | ||
{ | ||
[Serializable] | ||
public abstract class PocoSingleton<T> where T : PocoSingleton<T>, new() | ||
{ | ||
private static T _instance; | ||
|
||
public static T Instance | ||
{ | ||
get | ||
{ | ||
if (_instance == null) | ||
{ | ||
_instance = new T(); | ||
} | ||
|
||
return _instance; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using Better.Extensions.Runtime; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace Better.Singletons.Runtime | ||
{ | ||
public abstract class ScriptableSingletonAsset<T> : ScriptableObject where T : ScriptableSingletonAsset<T> | ||
{ | ||
#if UNITY_EDITOR | ||
|
||
private static T _assetInstance; | ||
|
||
public static T AssetInstance | ||
{ | ||
get | ||
{ | ||
if (_assetInstance == null) | ||
{ | ||
_assetInstance = FindAsset(); | ||
} | ||
|
||
return _assetInstance; | ||
} | ||
} | ||
|
||
private static T FindAsset() | ||
{ | ||
var typeName = typeof(T).Name; | ||
var filter = $"t:{typeName}"; | ||
var guids = AssetDatabase.FindAssets(filter); | ||
|
||
if (guids.IsNullOrEmpty()) | ||
{ | ||
throw new InvalidOperationException($"[{typeName}] {nameof(FindAsset)}: no asset found"); | ||
} | ||
|
||
if (guids.Length > 1) | ||
{ | ||
Debug.LogWarning($"[{typeName}] {nameof(FindAsset)}: more than one asset found"); | ||
} | ||
|
||
//TODO: Add BetterResources or AssetDatabaseUtility | ||
var assetPath = AssetDatabase.GUIDToAssetPath(guids[0]); | ||
return AssetDatabase.LoadAssetAtPath<T>(assetPath); | ||
} | ||
|
||
#endif | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "com.tdw.better.singletons", | ||
"displayName": "Better Singletons", | ||
"version": "0.0.1", | ||
"unity": "2021.3", | ||
"description": " ", | ||
"dependencies": { | ||
"com.uurha.betterextensions": "1.1.99" | ||
}, | ||
"author": { | ||
"name": "Better Plugins", | ||
"url": "https://github.com/techno-dwarf-works" | ||
}, | ||
"changelogUrl": "https://github.com/techno-dwarf-works/better-singletons", | ||
"documentationUrl": "https://github.com/techno-dwarf-works/better-singletons/tree/main#readme", | ||
"license": "MIT", | ||
"licensesUrl":"https://github.com/techno-dwarf-works/better-singletons/blob/main/LICENSE", | ||
"keywords": [ | ||
"data management", | ||
"referencing", | ||
"pattern", | ||
"patterns" | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.