From 83482e22e0c2965bdeddfdcdb09d838bfee7d0fb Mon Sep 17 00:00:00 2001 From: magicjar <9734293+magicjar@users.noreply.github.com> Date: Sat, 3 Apr 2021 16:20:15 +0700 Subject: [PATCH] Initial commit --- .gitattributes | 2 + LICENSE | 21 +++++++++ LICENSE.meta | 7 +++ README.md | 2 + README.md.meta | 7 +++ Runtime.meta | 8 ++++ Runtime/Agraris.Tools.Core.asmdef | 3 ++ Runtime/Agraris.Tools.Core.asmdef.meta | 7 +++ Runtime/Singletons.meta | 8 ++++ Runtime/Singletons/PresistentSingleton.cs | 45 +++++++++++++++++++ .../Singletons/PresistentSingleton.cs.meta | 11 +++++ Runtime/Singletons/Singleton.cs | 43 ++++++++++++++++++ Runtime/Singletons/Singleton.cs.meta | 11 +++++ package.json | 26 +++++++++++ package.json.meta | 7 +++ 15 files changed, 208 insertions(+) create mode 100644 .gitattributes create mode 100644 LICENSE create mode 100644 LICENSE.meta create mode 100644 README.md create mode 100644 README.md.meta create mode 100644 Runtime.meta create mode 100644 Runtime/Agraris.Tools.Core.asmdef create mode 100644 Runtime/Agraris.Tools.Core.asmdef.meta create mode 100644 Runtime/Singletons.meta create mode 100644 Runtime/Singletons/PresistentSingleton.cs create mode 100644 Runtime/Singletons/PresistentSingleton.cs.meta create mode 100644 Runtime/Singletons/Singleton.cs create mode 100644 Runtime/Singletons/Singleton.cs.meta create mode 100644 package.json create mode 100644 package.json.meta diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c5d50db --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 magicjar + +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. diff --git a/LICENSE.meta b/LICENSE.meta new file mode 100644 index 0000000..f585dfe --- /dev/null +++ b/LICENSE.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c13afe9dd3f20244aaa604ff365a4e24 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md b/README.md new file mode 100644 index 0000000..3ce390e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# com.agraris.core + Agraris tools core components. diff --git a/README.md.meta b/README.md.meta new file mode 100644 index 0000000..fef2417 --- /dev/null +++ b/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 689237e681aeb1145b636c015d6dea36 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime.meta b/Runtime.meta new file mode 100644 index 0000000..ec05d1d --- /dev/null +++ b/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 341ef8f757c1c904589d272224583332 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Agraris.Tools.Core.asmdef b/Runtime/Agraris.Tools.Core.asmdef new file mode 100644 index 0000000..eca5267 --- /dev/null +++ b/Runtime/Agraris.Tools.Core.asmdef @@ -0,0 +1,3 @@ +{ + "name": "Agraris.Tools.Core" +} diff --git a/Runtime/Agraris.Tools.Core.asmdef.meta b/Runtime/Agraris.Tools.Core.asmdef.meta new file mode 100644 index 0000000..a6a9ba3 --- /dev/null +++ b/Runtime/Agraris.Tools.Core.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 6e4427926c53f2544b4c5c20d43e126d +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Singletons.meta b/Runtime/Singletons.meta new file mode 100644 index 0000000..54192d8 --- /dev/null +++ b/Runtime/Singletons.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1c4d13221beb49041a9b89668c6a1912 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Singletons/PresistentSingleton.cs b/Runtime/Singletons/PresistentSingleton.cs new file mode 100644 index 0000000..f058714 --- /dev/null +++ b/Runtime/Singletons/PresistentSingleton.cs @@ -0,0 +1,45 @@ +using UnityEngine; + +namespace Agraris.Tools.Core +{ + /// + /// Persistent singleton. + /// + public class PersistentSingleton : MonoBehaviour where T : Component + { + protected static T _instance; + + /// + /// The singleton instance. + /// + public static T Instance + { + get + { + if (_instance == null) + { + GameObject obj = new GameObject(); + _instance = obj.AddComponent(); + } + return _instance; + } + } + + /// + /// On awake, we check if there's already a copy of the object in the scene. If there's one, we destroy it. + /// + protected virtual void Awake() + { + if (_instance != null) + { + //there is already an instance: + Destroy(gameObject); + return; + } + + //If I am the first instance, make me immortal + _instance = this as T; + DontDestroyOnLoad(transform.gameObject); + } + } +} diff --git a/Runtime/Singletons/PresistentSingleton.cs.meta b/Runtime/Singletons/PresistentSingleton.cs.meta new file mode 100644 index 0000000..f202e7e --- /dev/null +++ b/Runtime/Singletons/PresistentSingleton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b2fe08efbb7b3d2469444a59d9b536c5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Singletons/Singleton.cs b/Runtime/Singletons/Singleton.cs new file mode 100644 index 0000000..fa0b66f --- /dev/null +++ b/Runtime/Singletons/Singleton.cs @@ -0,0 +1,43 @@ +using UnityEngine; + +namespace Agraris.Tools.Core +{ + /// + /// Singleton pattern. + /// + public class Singleton : MonoBehaviour where T : Component + { + protected static T _instance; + + /// + /// The singleton instance. + /// + public static T Instance + { + get + { + if (_instance == null) + { + GameObject obj = new GameObject(); + _instance = obj.AddComponent(); + } + return _instance; + } + } + + /// + /// On awake, we check if there's already a copy of the object in the scene. If there's one, we destroy it. + /// + protected virtual void Awake() + { + if (_instance != null) + { + //there is already an instance: + Destroy(gameObject); + return; + } + + _instance = this as T; + } + } +} diff --git a/Runtime/Singletons/Singleton.cs.meta b/Runtime/Singletons/Singleton.cs.meta new file mode 100644 index 0000000..b65ac37 --- /dev/null +++ b/Runtime/Singletons/Singleton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 675d16f19a8cb5141a726fbd6a9c1d6d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json new file mode 100644 index 0000000..6200265 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "com.agraris.core", + "version": "1.0.0", + "displayName": "Agraris Core", + "description": "Agraris tools core components.", + "unity": "2020.3", + "keywords": [ + "utility", + "tool", + "unity", + "singleton" + ], + "homepage": "https://agraris.github.io/", + "bugs": { + "url": "https://github.com//issues", + "email": "agrarisentertainment@gmail.com" + }, + "repository": { + "type": "git", + "url": "" + }, + "license": "MIT", + "author": { + "name": "Agraris Entertainment" + } +} \ No newline at end of file diff --git a/package.json.meta b/package.json.meta new file mode 100644 index 0000000..3c0d2c3 --- /dev/null +++ b/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 33d2586033b34324c8a0082e974c815e +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: