Browse Source

字典类型

main
coffee 5 months ago
parent
commit
5fdfc13783
  1. 2
      Assets/Plugins/Sirenix/Odin Inspector/Config/Resources/Sirenix/GlobalSerializationConfig.asset
  2. 20
      Assets/Unity-Tools/Core/Editor/UnityEditorUtil.cs
  3. 3
      Assets/Unity-Tools/Core/Editor/UnityEditorUtil.cs.meta
  4. 66
      Assets/Unity-Tools/ExcelResolver/Editor/Core/Types/TDict.cs
  5. 3
      Assets/Unity-Tools/ExcelResolver/Editor/Core/Types/TDict.cs.meta
  6. 6
      Assets/Unity-Tools/ExcelResolver/Editor/Core/Util/ExcelResolverUtil.Cell.cs
  7. 9
      Assets/Unity-Tools/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs
  8. 2
      Assets/Unity-Tools/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteUtilCode.cs
  9. 13
      Assets/Unity-Tools/ExcelResolver/Editor/ExcelResolverEditorWindow.cs
  10. 2
      Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOData.cs
  11. 0
      Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOData.cs.meta
  12. 2
      Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOUtil.cs
  13. 0
      Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOUtil.cs.meta
  14. BIN
      Assets/_Project/ExcelResolver/Excel/hero.xlsx
  15. 61
      Assets/_Project/ScriptableObject/Excel/Hero_1.asset
  16. 29
      Assets/_Project/ScriptableObject/Excel/Hero_2.asset
  17. 43
      Assets/_Project/ScriptableObject/Excel/Hero_3.asset
  18. 10
      Assets/_Project/Scripts/Generator/Excel/Hero.cs
  19. 2
      Assets/_Project/Scripts/Generator/Excel/HeroUtil.cs

2
Assets/Plugins/Sirenix/Odin Inspector/Config/Resources/Sirenix/GlobalSerializationConfig.asset

@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 1549551891, guid: 74721b9f0af448f5ae2e91102a1a5edd, type: 3}
m_Name: GlobalSerializationConfig
m_EditorClassIdentifier:
HideSerializationCautionaryMessage: 0
HideSerializationCautionaryMessage: 1
HidePrefabCautionaryMessage: 0
HideOdinSerializeAttributeWarningMessages: 0
HideNonSerializedShowInInspectorWarningMessages: 0

20
Assets/Unity-Tools/Core/Editor/UnityEditorUtil.cs

@ -0,0 +1,20 @@
using System.Reflection;
namespace Tools.Editor
{
public static class UnityEditorUtil
{
static MethodInfo clearMethod;
public static void ClearConsole()
{
if (clearMethod == null)
{
Assembly assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
System.Type logEntries = assembly.GetType("UnityEditor.LogEntries");
clearMethod = logEntries.GetMethod("Clear");
}
clearMethod.Invoke(new object(), null);
}
}
}

3
Assets/Unity-Tools/Core/Editor/UnityEditorUtil.cs.meta

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b4f042c2bcf24f2494edc41df37ff88f
timeCreated: 1737803701

66
Assets/Unity-Tools/ExcelResolver/Editor/Core/Types/TDict.cs

@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace Tools.ExcelResolver.Editor
{
internal class TDict : TType
{
internal override Type RealType => typeof(Dictionary<,>).MakeGenericType(KeyType?.RealType ?? typeof(object), ValueType?.RealType ?? typeof(object));
internal override string FieldWriteFormat => $"Dictionary<{KeyType?.RealType.Name ?? "object"}, {ValueType?.RealType.Name ?? "object"}>";
internal override object DefaultValue => Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(KeyType?.RealType ?? typeof(object), ValueType?.RealType ?? typeof(object)));
// 键和值的类型
internal TType KeyType { get; set; }
internal TType ValueType { get; set; }
internal override bool String2TType(string typeText)
{
var split = typeText.Split(',', StringSplitOptions.RemoveEmptyEntries);
if (split.Length != 3 || !string.Equals(split[0], "dict", StringComparison.OrdinalIgnoreCase))
{
return false;
}
// 解析键和值的类型
KeyType = ExcelResolverUtil.GetTTypeByString(split[1]);
ValueType = ExcelResolverUtil.GetTTypeByString(split[2]);
return KeyType != null && ValueType != null;
}
internal override object TryParseFrom(string cellText)
{
if (KeyType == null || ValueType == null)
{
return null; // 如果未指定键和值类型,无法解析
}
var dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(KeyType.RealType, ValueType.RealType));
var entries = cellText.Split('|'); // 假定每个键值对用 "|" 分隔
foreach (var entry in entries)
{
var keyValue = entry.Split(':'); // 假定键和值用 ":" 分隔
if (keyValue.Length != 2)
{
return null; // 任意键值对格式不正确时,返回 null
}
var key = KeyType.TryParseFrom(keyValue[0]);
var value = ValueType.TryParseFrom(keyValue[1]);
if (key != null && value != null)
{
dict.Add(key, value);
}
else
{
return null; // 键或值解析失败时,返回 null
}
}
return dict;
}
}
}

3
Assets/Unity-Tools/ExcelResolver/Editor/Core/Types/TDict.cs.meta

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ee795236344b4595bafcdb6222b93798
timeCreated: 1737802386

6
Assets/Unity-Tools/ExcelResolver/Editor/Core/Util/ExcelResolverUtil.Cell.cs

@ -64,7 +64,11 @@ namespace Tools.ExcelResolver.Editor
return result;
}
throw new Exception($"单元格转换失败 className: '{className}' FullAddress: {cell.FullAddress} Text: '{cell.Text}'");
throw new Exception($"单元格转换失败 " +
$"className: '<color=darkblue>{className}</color>' " +
$"FullAddress: <color=darkblue>{cell.FullAddress}</color> " +
$"Type: '<color=darkblue>{type.FieldWriteFormat}</color>' " +
$"Text: '<color=darkblue>{cell.Text}</color>' ");
}
/// <summary>

9
Assets/Unity-Tools/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs

@ -3,7 +3,9 @@ using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
/* 使用CodeCompileUnit需要将Edit->Project Settings->Player->Other Settings->Api Compatibility Level改为.NET 4.x(或.NET Framework) */
/* 使CodeCompileUnit需要将Edit->Project Settings->Player->Other Settings->Api Compatibility Level改为.NET 4.x.NET Framework
使
*/
namespace Tools.ExcelResolver.Editor
@ -30,6 +32,7 @@ namespace Tools.ExcelResolver.Editor
"System.Collections",
"System.Collections.Generic",
"UnityEngine",
"Sirenix.OdinInspector",
};
foreach (var import in classImports)
{
@ -50,8 +53,8 @@ namespace Tools.ExcelResolver.Editor
// },
BaseTypes =
{
new CodeTypeReference("ScriptableObject"),
new CodeTypeReference("IExcelData")
new CodeTypeReference("SerializedScriptableObject"),
new CodeTypeReference("IExcelSOData")
}
};
codeNamespace.Types.Add(classType);

2
Assets/Unity-Tools/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteUtilCode.cs

@ -48,7 +48,7 @@ namespace Tools.ExcelResolver.Editor
BaseTypes =
{
new CodeTypeReference("SerializedScriptableObject"),
new CodeTypeReference("IExcelSO"),
new CodeTypeReference("IExcelSOUtil"),
}
};
codeNamespace.Types.Add(classType);

13
Assets/Unity-Tools/ExcelResolver/Editor/ExcelResolverEditorWindow.cs

@ -12,6 +12,10 @@ namespace Tools.ExcelResolver.Editor
{
[SerializeField] private ExcelResolverEditorConfig excelResolverConfig;
[FoldoutGroup("Hide Setting"), LabelText("导表前是否清空Console")]
[SerializeField] private bool isClearConsole = true;
[MenuItem("\u272dExcelResolver\u272d/ExcelResolverEditorWindow")]
private static void OpenWindow()
{
@ -59,7 +63,14 @@ namespace Tools.ExcelResolver.Editor
[Button("导表", ButtonSizes.Gigantic)]
private void StartExportExcel() => ReadExcel();
private void StartExportExcel()
{
if (isClearConsole)
{
UnityEditorUtil.ClearConsole();
}
ReadExcel();
}
// [ButtonGroup("Generate")]
// private void GenerateSO() => WriteSOData();

2
Assets/Unity-Tools/ExcelResolver/Interface/IExcelData.cs → Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOData.cs

@ -1,6 +1,6 @@
namespace Tools.ExcelResolver
{
public interface IExcelData
public interface IExcelSOData
{
}

0
Assets/Unity-Tools/ExcelResolver/Interface/IExcelData.cs.meta → Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOData.cs.meta

2
Assets/Unity-Tools/ExcelResolver/Interface/IExcelSO.cs → Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOUtil.cs

@ -1,6 +1,6 @@
namespace Tools.ExcelResolver
{
public interface IExcelSO
public interface IExcelSOUtil
{
}

0
Assets/Unity-Tools/ExcelResolver/Interface/IExcelSO.cs.meta → Assets/Unity-Tools/ExcelResolver/Interface/IExcelSOUtil.cs.meta

BIN
Assets/_Project/ExcelResolver/Excel/hero.xlsx

Binary file not shown.

61
Assets/_Project/ScriptableObject/Excel/Hero_1.asset

@ -12,6 +12,59 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e8aa3c1560079b84cafbabe4bc0d2c8d, type: 3}
m_Name: Hero_1
m_EditorClassIdentifier:
serializationData:
SerializedFormat: 2
SerializedBytes:
ReferencedUnityObjects: []
SerializedBytesString:
Prefab: {fileID: 0}
PrefabModificationsReferencedUnityObjects: []
PrefabModifications: []
SerializationNodes:
- Name: attribute
Entry: 7
Data: 0|System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.Int32,
mscorlib]], mscorlib
- Name: comparer
Entry: 7
Data: 1|System.Collections.Generic.GenericEqualityComparer`1[[System.Int32,
mscorlib]], mscorlib
- Name:
Entry: 8
Data:
- Name:
Entry: 12
Data: 2
- Name:
Entry: 7
Data:
- Name: $k
Entry: 3
Data: 1
- Name: $v
Entry: 3
Data: 500
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 3
Data: 2
- Name: $v
Entry: 3
Data: 300
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
id: 1
name: "\u9053\u58EB"
icon: hero_1
@ -22,7 +75,7 @@ MonoBehaviour:
pos: {x: 0, y: 1, z: 4}
ches: {x: 0, y: 1}
attack_target:
- 0.5
- -2.6
- 3
- 4.5
- {x: 1, y: 2}
- {x: 1, y: 2}
- {x: 1, y: 2}
- {x: 1, y: 2}

29
Assets/_Project/ScriptableObject/Excel/Hero_2.asset

@ -12,6 +12,35 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e8aa3c1560079b84cafbabe4bc0d2c8d, type: 3}
m_Name: Hero_2
m_EditorClassIdentifier:
serializationData:
SerializedFormat: 2
SerializedBytes:
ReferencedUnityObjects: []
SerializedBytesString:
Prefab: {fileID: 0}
PrefabModificationsReferencedUnityObjects: []
PrefabModifications: []
SerializationNodes:
- Name: attribute
Entry: 7
Data: 0|System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.Int32,
mscorlib]], mscorlib
- Name: comparer
Entry: 7
Data: 1|System.Collections.Generic.GenericEqualityComparer`1[[System.Int32,
mscorlib]], mscorlib
- Name:
Entry: 8
Data:
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
id: 2
name:
icon: hero_2

43
Assets/_Project/ScriptableObject/Excel/Hero_3.asset

@ -12,6 +12,47 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e8aa3c1560079b84cafbabe4bc0d2c8d, type: 3}
m_Name: Hero_3
m_EditorClassIdentifier:
serializationData:
SerializedFormat: 2
SerializedBytes:
ReferencedUnityObjects: []
SerializedBytesString:
Prefab: {fileID: 0}
PrefabModificationsReferencedUnityObjects: []
PrefabModifications: []
SerializationNodes:
- Name: attribute
Entry: 7
Data: 0|System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.Int32,
mscorlib]], mscorlib
- Name: comparer
Entry: 7
Data: 1|System.Collections.Generic.GenericEqualityComparer`1[[System.Int32,
mscorlib]], mscorlib
- Name:
Entry: 8
Data:
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data:
- Name: $k
Entry: 3
Data: 2
- Name: $v
Entry: 3
Data: 200
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
id: 2
name: "\u5973\u5DEB"
icon: hero_3
@ -22,4 +63,4 @@ MonoBehaviour:
pos: {x: 3, y: 2, z: 4}
ches: {x: 5, y: 12}
attack_target:
- 2.5
- {x: 1, y: 2}

10
Assets/_Project/Scripts/Generator/Excel/Hero.cs

@ -14,9 +14,10 @@ namespace Tools.ExcelResolver
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
public class Hero : ScriptableObject, IExcelData
public class Hero : SerializedScriptableObject, IExcelSOData
{
/// <summary>
@ -67,6 +68,11 @@ namespace Tools.ExcelResolver
/// <summary>
/// 攻击优先级
/// </summary>
public List<Single> attack_target;
public List<Vector2> attack_target;
/// <summary>
///
/// </summary>
public Dictionary<Int32, Int32> attribute;
}
}

2
Assets/_Project/Scripts/Generator/Excel/HeroUtil.cs

@ -17,7 +17,7 @@ namespace Tools.ExcelResolver
using Sirenix.OdinInspector;
public class HeroUtil : SerializedScriptableObject, IExcelSO
public class HeroUtil : SerializedScriptableObject, IExcelSOUtil
{
public Dictionary<int, Hero> Data;

Loading…
Cancel
Save