This commit is contained in:
CortexCore
2025-03-24 14:42:40 +08:00
parent 18239a5ae4
commit 9845d20f7f
99 changed files with 5418 additions and 5512 deletions

View File

@@ -0,0 +1,19 @@
{
"name": "Net.Project.B.BitMask.Unity",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 1f7ecae3bbd1640418dd25dd62e6a9d3
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,17 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4bc860277386c244aac5bc8ec6cbb54b, type: 3}
m_Name: NewScriptableBitMask
m_EditorClassIdentifier:
implements: {fileID: 11400000, guid: 1e642bf42ffa8bd429cbfdeeefabaecd, type: 2}
dictionary:
_serializedList: []

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8411467f1adb3754ba46f890d7be5ff4
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,38 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9fab0368c5976f24dafd43f829e91aab, type: 3}
m_Name: NewScriptableBitMaskSample
m_EditorClassIdentifier:
implements:
- rid: 7572707602352832517
- rid: 7572707602352832518
- rid: 7572707602352832519
- rid: 7572707602352832520
- rid: 7572707602352832521
references:
version: 2
RefIds:
- rid: 7572707602352832517
type: {class: SampleInterface1, ns: Net.BITKit.BitMask, asm: Net.Project.B.BitMask.Unity}
data:
- rid: 7572707602352832518
type: {class: SampleInterface2, ns: Net.BITKit.BitMask, asm: Net.Project.B.BitMask.Unity}
data:
- rid: 7572707602352832519
type: {class: SampleInterface3, ns: Net.BITKit.BitMask, asm: Net.Project.B.BitMask.Unity}
data:
- rid: 7572707602352832520
type: {class: SampleInterface4, ns: Net.BITKit.BitMask, asm: Net.Project.B.BitMask.Unity}
data:
- rid: 7572707602352832521
type: {class: SampleInterface5, ns: Net.BITKit.BitMask, asm: Net.Project.B.BitMask.Unity}
data:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1e642bf42ffa8bd429cbfdeeefabaecd
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,192 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AYellowpaper.SerializedCollections;
using BITKit;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UIElements;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Net.BITKit.BitMask
{
public class ScriptableBitMask : ScriptableObject,ISerializationCallbackReceiver
{
[SerializeField] public ScriptableImplements implements;
internal readonly Dictionary<string, List<string>> Dictionary=new();
[SerializeField] public string yaml;
public Type Type => implements.Type;
public Type[] Types => implements ? implements.Types : Array.Empty<Type>();
private void OnEnable()
{
OnBeforeSerialize();
}
public void OnBeforeSerialize()
{
if(string.IsNullOrEmpty(yaml))return;
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance) // 驼峰命名
.Build();
Dictionary.Clear();
var newDictionary = deserializer.Deserialize<Dictionary<string, List<string>>>(yaml);
foreach (var pair in newDictionary)
{
Dictionary[pair.Key] = pair.Value;
}
}
public void OnAfterDeserialize()
{
if(Dictionary.Count is 0)return;
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
yaml = serializer.Serialize(Dictionary);
}
public IDictionary<Type,HashSet<Type>> FlagMask
{
get
{
var typeDictionary = implements.Types.ToDictionary(x => x.Name, x => x);
var nextDictionary = new Dictionary<Type, HashSet<Type>>();
foreach (var (key, d) in Dictionary)
{
if (typeDictionary.TryGetValue(key, out var type) is false) continue;
var hashSet = new HashSet<Type>();
foreach (var x in d)
{
if(typeDictionary.TryGetValue(x,out var t1) is false)continue;
hashSet.Add(t1);
}
nextDictionary[type]=hashSet;
}
foreach (var type in Types)
{
if (nextDictionary.TryAdd(type, new HashSet<Type>())) ;
}
return nextDictionary;
}
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(ScriptableBitMask),true)]
public sealed class ScriptableBitMaskEditor:Editor
{
private const int CellSize = 24;
public override VisualElement CreateInspectorGUI()
{
if (serializedObject.targetObject is not ScriptableBitMask scriptableBitMask)
return base.CreateInspectorGUI();
var inspector = new VisualElement
{
style =
{
flexDirection = FlexDirection.Column
}
};
var defaultPropertyFields = inspector.Create<VisualElement>();
BITInspectorExtensions.FillDefaultInspector(defaultPropertyFields.Create<VisualElement>(),serializedObject,false);
var root = inspector.Create<VisualElement>();
root.style.flexDirection = new StyleEnum<FlexDirection>(FlexDirection.Row);
var allFlags = scriptableBitMask.Types;
var labelRow = root.Create<VisualElement>();
var toggleRot = root.Create<VisualElement>();
labelRow.style.alignSelf = new StyleEnum<Align>(Align.FlexEnd);
for (var x = -1; x < allFlags.Length; x++)
{
if (x is -1)
{
var container = toggleRot.Create<VisualElement>();
container.style.flexDirection = FlexDirection.Row;
foreach (var type in scriptableBitMask.Types.Reverse())
{
var text = container.Create<Label>();
text.style.width = CellSize;
text.text = string.Join("\n", type.Name.ToArray());
}
continue;
}
{
var xEnum = allFlags[x];
var label = labelRow.Create<Label>();
label.text = xEnum.Name;
label.style.height = CellSize;
label.style.unityTextAlign = TextAnchor.MiddleRight;
var toggles = toggleRot.Create<VisualElement>();
toggles.style.flexDirection = new StyleEnum<FlexDirection>(FlexDirection.Row);
for (var y = allFlags.Length-1; y >= 0+x; y--)
{
var yEnum = allFlags[y];
var toggle = toggles.Create<Toggle>();
toggle.tooltip = $"{xEnum.Name}/{yEnum.Name}";
toggle.style.width = toggle.style.height = CellSize;
toggle.style.marginBottom
= toggleRot.style.marginLeft
= toggleRot.style.marginRight
= toggleRot.style.marginTop
= toggleRot.style.paddingBottom
=toggleRot.style.paddingLeft
=toggleRot.style.paddingRight
=toggleRot.style.paddingTop
=-1;
toggle.SetValueWithoutNotify(scriptableBitMask.Dictionary.GetOrCreate(xEnum.Name).Contains(yEnum.Name));
toggle.RegisterValueChangedCallback(changeEvent =>
{
if (changeEvent.newValue)
{
scriptableBitMask.Dictionary.GetOrCreate(xEnum.Name).TryAdd(yEnum.Name);
scriptableBitMask.Dictionary.GetOrCreate(yEnum.Name).TryAdd(xEnum.Name);
}
else
{
scriptableBitMask.Dictionary.GetOrCreate(xEnum.Name).TryRemove(yEnum.Name);
scriptableBitMask.Dictionary.GetOrCreate(yEnum.Name).TryRemove(xEnum.Name);
}
scriptableBitMask.OnAfterDeserialize();
EditorUtility.SetDirty(serializedObject.targetObject);
});
}
}
}
return root;
}
}
#endif
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4bc860277386c244aac5bc8ec6cbb54b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Net.BITKit.BitMask
{
public interface ISampleInterface
{
}
[Serializable]
public struct SampleInterface1:ISampleInterface{}
[Serializable]
public struct SampleInterface2:ISampleInterface{}
[Serializable]
public struct SampleInterface3:ISampleInterface{}
[Serializable]
public struct SampleInterface4:ISampleInterface{}
[Serializable]
public struct SampleInterface5:ISampleInterface{}
public class ScriptableBitMaskSample : ScriptableImplements<ISampleInterface>
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9fab0368c5976f24dafd43f829e91aab
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Net.BITKit.BitMask
{
public abstract class ScriptableImplements : ScriptableObject
{
public abstract Type[] Types { get; }
public abstract Type Type { get; }
}
public class ScriptableImplements<T> : ScriptableImplements
{
[SerializeReference,SubclassSelector] private T[] implements;
public override Type[] Types => Array.ConvertAll(implements, x => x.GetType());
public override Type Type => typeof(T);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9277a745f0b4ebc4c9d0759e9e56de77
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: