feat: initial commit
This commit is contained in:
61
Assets/Common/Serialization/.gitignore
vendored
Normal file
61
Assets/Common/Serialization/.gitignore
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
# This .gitignore file should be placed at the root of your Unity project directory
|
||||
#
|
||||
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
|
||||
#
|
||||
/[Ll]ibrary/
|
||||
/[Tt]emp/
|
||||
/[Oo]bj/
|
||||
/[Bb]uild/
|
||||
/[Bb]uilds/
|
||||
/[Ll]ogs/
|
||||
/[Mm]emoryCaptures/
|
||||
|
||||
# Asset meta data should only be ignored when the corresponding asset is also ignored
|
||||
!/[Aa]ssets/**/*.meta
|
||||
|
||||
# Uncomment this line if you wish to ignore the asset store tools plugin
|
||||
# /[Aa]ssets/AssetStoreTools*
|
||||
|
||||
# Autogenerated Jetbrains Rider plugin
|
||||
[Aa]ssets/Plugins/Editor/JetBrains*
|
||||
|
||||
# Visual Studio cache directory
|
||||
.vs/
|
||||
.config
|
||||
|
||||
# Gradle cache directory
|
||||
.gradle/
|
||||
|
||||
# Autogenerated VS/MD/Consulo solution and project files
|
||||
ExportedObj/
|
||||
.consulo/
|
||||
*.csproj
|
||||
*.unityproj
|
||||
*.sln
|
||||
*.suo
|
||||
*.tmp
|
||||
*.user
|
||||
*.userprefs
|
||||
*.pidb
|
||||
*.booproj
|
||||
*.svd
|
||||
*.pdb
|
||||
*.mdb
|
||||
*.opendb
|
||||
*.VC.db
|
||||
|
||||
# Unity3D generated meta files
|
||||
*.pidb.meta
|
||||
*.pdb.meta
|
||||
*.mdb.meta
|
||||
|
||||
# Unity3D generated file on crash reports
|
||||
sysinfo.txt
|
||||
|
||||
# Builds
|
||||
*.apk
|
||||
*.unitypackage
|
||||
|
||||
# Crashlytics generated file
|
||||
crashlytics-build.properties
|
||||
|
||||
12
Assets/Common/Serialization/CHANGELOG.md
Normal file
12
Assets/Common/Serialization/CHANGELOG.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.0.0] - 2025-11-24
|
||||
|
||||
### Added
|
||||
|
||||
- Initial release of the Unity package.
|
||||
7
Assets/Common/Serialization/CHANGELOG.md.meta
Normal file
7
Assets/Common/Serialization/CHANGELOG.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6aab4fa9da7c1124a96e9c0838a1b12e
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Assets/Common/Serialization/LICENSE.md
Normal file
21
Assets/Common/Serialization/LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Joe Siu
|
||||
|
||||
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.
|
||||
7
Assets/Common/Serialization/LICENSE.md.meta
Normal file
7
Assets/Common/Serialization/LICENSE.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6333dc7bfd551ae4c82c1586a89dbc41
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Serialization/README.md
Normal file
8
Assets/Common/Serialization/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# com.joesiu.common.serialization
|
||||
|
||||
## Installation
|
||||
|
||||
1. Open `Package Manager`
|
||||
2. Click `+`
|
||||
3. Select `Add package from git URL...`
|
||||
4. Paste `https://github.com/JoeSiu/UnityPackages.git?path=Assets/Common/Serialization`
|
||||
7
Assets/Common/Serialization/README.md.meta
Normal file
7
Assets/Common/Serialization/README.md.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9396f0bf3b325ff48ba7029d107c30fa
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Common/Serialization/Runtime.meta
Normal file
8
Assets/Common/Serialization/Runtime.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98bbeb42106339546b8f985f042e2a8f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Common.Serialization.Runtime",
|
||||
"rootNamespace": "JoeSiu",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7f6647327e7fa54faec2c8e2a49621d
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
138
Assets/Common/Serialization/Runtime/SerializableInterface.cs
Normal file
138
Assets/Common/Serialization/Runtime/SerializableInterface.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
#region License and Information
|
||||
|
||||
/*****
|
||||
* Provides a serializable wrapper that allows you to store a reference to
|
||||
* a serialized UnityEngine.Object that implement a certain interface.
|
||||
* The "Instance" property provides direct easy access to the serialized reference.
|
||||
* This should work with Components as well as ScriptableObjects.
|
||||
* It ships with a convenient property drawer that should streamline the usage.
|
||||
*
|
||||
* Copyright (c) 2023 Bunny83
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*****/
|
||||
|
||||
#endregion License and Information
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace JoeSiu.Common.Serialization.Runtime
|
||||
{
|
||||
[Serializable]
|
||||
public class SerializableInterface<T> where T : class
|
||||
{
|
||||
[SerializeField]
|
||||
private Object _obj;
|
||||
private T _value;
|
||||
|
||||
public SerializableInterface()
|
||||
{
|
||||
}
|
||||
|
||||
public SerializableInterface(T value)
|
||||
{
|
||||
SetValue(value);
|
||||
}
|
||||
|
||||
public T Value
|
||||
{
|
||||
get => GetValue();
|
||||
set => SetValue(value);
|
||||
}
|
||||
|
||||
public T GetValue()
|
||||
{
|
||||
if (_value != null && (object)_value == _obj) return _value;
|
||||
if (_obj == null) SetValue(null);
|
||||
else if (_obj is T inst || _obj is GameObject go && go.TryGetComponent(out inst)) _value = inst;
|
||||
else SetValue(null);
|
||||
|
||||
return _value;
|
||||
}
|
||||
|
||||
private void SetValue(T value)
|
||||
{
|
||||
_value = value;
|
||||
_obj = _value as Object;
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[CustomPropertyDrawer(typeof(SerializableInterface<>), true)]
|
||||
public class SerializableInterfacePropertyDrawer : PropertyDrawer
|
||||
{
|
||||
private Type _genericType;
|
||||
private readonly List<Component> _list = new();
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (_genericType == null)
|
||||
{
|
||||
var fieldType = fieldInfo.FieldType;
|
||||
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
|
||||
// when used in a List<>, grab the actual type from the generic argument of the List
|
||||
fieldType = fieldInfo.FieldType.GetGenericArguments()[0];
|
||||
else if (fieldType.IsArray)
|
||||
// when used in an array, grab the actual type from the element type.
|
||||
fieldType = fieldType.GetElementType();
|
||||
|
||||
var types = fieldType?.GetGenericArguments();
|
||||
if (types is { Length: 1 }) _genericType = types[0];
|
||||
}
|
||||
|
||||
var obj = property.FindPropertyRelative("_obj");
|
||||
EditorGUI.BeginChangeCheck();
|
||||
var newObj = EditorGUI.ObjectField(position, label, obj.objectReferenceValue, typeof(Object), true);
|
||||
if (!EditorGUI.EndChangeCheck()) return;
|
||||
if (newObj == null) obj.objectReferenceValue = null;
|
||||
else if (_genericType != null && _genericType.IsAssignableFrom(newObj.GetType())) obj.objectReferenceValue = newObj;
|
||||
else if (newObj is GameObject go)
|
||||
{
|
||||
_list.Clear();
|
||||
go.GetComponents(_genericType, _list);
|
||||
if (_list.Count == 1)
|
||||
{
|
||||
obj.objectReferenceValue = _list[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
var m = new GenericMenu();
|
||||
var n = 1;
|
||||
foreach (var item in _list)
|
||||
m.AddItem(new GUIContent(n++ + " " + item.GetType().Name), false, a =>
|
||||
{
|
||||
obj.objectReferenceValue = (Object)a;
|
||||
obj.serializedObject.ApplyModifiedProperties();
|
||||
}, item);
|
||||
m.ShowAsContext();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Dragged object is not compatible with " + _genericType?.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8df361f2765f214eba36fc0fb21a063
|
||||
16
Assets/Common/Serialization/package.json
Normal file
16
Assets/Common/Serialization/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "com.joesiu.common.serialization",
|
||||
"version": "1.0.0",
|
||||
"description": "Common scripts for serialization.",
|
||||
"displayName": "Serialization",
|
||||
"unity": "2019.4",
|
||||
"author": {
|
||||
"name": "Joe Siu",
|
||||
"url": "https://github.com/JoeSiu"
|
||||
},
|
||||
"changelogUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/CHANGELOG.md",
|
||||
"dependencies": {},
|
||||
"documentationUrl": "https://github.com/JoeSiu/UnityPackages/",
|
||||
"keywords": [],
|
||||
"licensesUrl": "https://github.com/JoeSiu/UnityPackages/blob/main/LICENSE.md"
|
||||
}
|
||||
7
Assets/Common/Serialization/package.json.meta
Normal file
7
Assets/Common/Serialization/package.json.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10f90d0d9e83df748a3118d0fd3a1560
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user