This commit is contained in:
2026-07-07 03:34:56 +08:00
parent e7891d7e00
commit ecb20cf370
81 changed files with 2108 additions and 293 deletions
@@ -0,0 +1,72 @@
using UnityEngine;
using UnityEditor;
using Architecture.Core;
using Architecture.Variables;
namespace Architecture.Editor
{
/// <summary>
/// 一键生成 P1/P2 所需的 SO 通信资产(变量 / 事件 / 运行时集合)。
/// 运行方式:Unity 顶部菜单 Architecture > Bootstrap Core Assets。
/// 生成的资产位于 Assets/Architecture/Assets/,可安全删改、重新生成(已存在则跳过)。
///
/// 注意:本脚本仅生成"空壳资产",具体系统需在 Inspector 中将对应字段拖入这些资产实例,
/// 才能建立解耦链路(详见 Docs/解耦架构重构方案.md)。
/// </summary>
public static class AssetBootstrap
{
private const string Root = "Assets/Architecture/Assets";
[MenuItem("Architecture/Bootstrap Core Assets")]
public static void Bootstrap()
{
if (!AssetDatabase.IsValidFolder(Root))
AssetDatabase.CreateFolder("Assets/Architecture", "Assets");
// 共享变量(同时设置 _value 与 _defaultValue,避免跨 Play Mode 状态泄漏)
CreateInt("PlayerHealth", 5);
CreateInt("Score", 0);
// 无参事件通道
Create<GameEvent>("OnPlayerDied");
Create<GameEvent>("OnGameOver");
Create<GameEvent>("OnGameWin");
Create<GameEvent>("OnPlayerDamaged");
// 带载荷事件通道
Create<Vector3Event>("EchoReleased");
Create<IntEvent>("ScoreChanged");
Create<IntEvent>("ScoreSettled");
// 运行时集合(当前场景实体注册表)
Create<TransformRuntimeSet>("Players");
Create<TransformRuntimeSet>("Enemies");
AssetDatabase.SaveAssets();
Debug.Log($"[AssetBootstrap] 已生成核心 SO 资产到 {Root}");
}
private static T Create<T>(string name) where T : ScriptableObject
{
var path = $"{Root}/{name}.asset";
var existing = AssetDatabase.LoadAssetAtPath<T>(path);
if (existing != null)
{
Debug.Log($"[AssetBootstrap] 已存在,跳过:{path}");
return existing;
}
var asset = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(asset, path);
return asset;
}
private static void CreateInt(string name, int value)
{
var asset = Create<IntVariable>(name);
var so = new SerializedObject(asset);
so.FindProperty("_value").intValue = value;
so.FindProperty("_defaultValue").intValue = value;
so.ApplyModifiedProperties();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: Dn4fvSL+Bi5qpJQPKTiLAFU3fgR+KLt4w0Ql57XB14ROp4pNfqn9n84=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,43 @@
using UnityEditor;
using UnityEngine;
using Architecture.Variables;
namespace Architecture.Editor
{
/// <summary>
/// 变量资产的 Inspector 绘制器:在字段右侧实时显示当前值(含 Play 模式运行时值),
/// 设计师无需打开脚本即可看到数值,降低沟通成本。
/// </summary>
[CustomPropertyDrawer(typeof(FloatVariable))]
[CustomPropertyDrawer(typeof(IntVariable))]
[CustomPropertyDrawer(typeof(BoolVariable))]
[CustomPropertyDrawer(typeof(Vector3Variable))]
public class VariableDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
var target = property.objectReferenceValue as ScriptableObject;
if (target != null)
{
var so = new SerializedObject(target);
so.Update();
var valueProp = so.FindProperty("_value");
Rect objRect = new Rect(position.x, position.y, position.width * 0.58f, position.height);
Rect valRect = new Rect(position.x + position.width * 0.60f, position.y, position.width * 0.40f, position.height);
EditorGUI.ObjectField(objRect, property, GUIContent.none);
if (valueProp != null)
{
EditorGUI.BeginDisabledGroup(true);
EditorGUI.PropertyField(valRect, valueProp, GUIContent.none);
EditorGUI.EndDisabledGroup();
}
}
else
{
EditorGUI.ObjectField(position, property, label);
}
EditorGUI.EndProperty();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: BnMa4CioUihgI9MvBm9J06Z6TNiVXXGlVT3vne3vk2CEiePGAFaObuc=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: