Skip to content

Commit

Permalink
Update README.md and LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Feb 17, 2024
0 parents commit 5838615
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Editor/BetterSingletons.Editor.asmdef
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
}
7 changes: 7 additions & 0 deletions Editor/BetterSingletons.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
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.
4 changes: 4 additions & 0 deletions README.md
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)
8 changes: 8 additions & 0 deletions Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/BetterSingletons.Runtime.asmdef
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
}
7 changes: 7 additions & 0 deletions Runtime/BetterSingletons.Runtime.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions Runtime/MonoSingleton.cs
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);
}
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/MonoSingleton.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Runtime/PocoSingleton.cs
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;
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/PocoSingleton.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions Runtime/ScriptableSingletonAsset.cs
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
}
}
11 changes: 11 additions & 0 deletions Runtime/ScriptableSingletonAsset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions package.json
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"
]
}
7 changes: 7 additions & 0 deletions package.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5838615

Please sign in to comment.