迁移剩下的 UI 到 Serialization 方式

This commit is contained in:
2026-09-14 17:19:31 +08:00
parent 091a29db63
commit b628a8537c
107 changed files with 4147 additions and 3680 deletions
@@ -72,7 +72,6 @@ namespace SepCore.Editor
private sealed class Binding
{
public Component component;
public UISerializationRoot nestedRoot;
public string variableName;
public string csharpTypeName;
}
@@ -129,7 +128,7 @@ namespace SepCore.Editor
List<UISerializationRoot> generationRoots = CollectGenerationRoots(root);
List<GenerationUnit> units = BuildGenerationUnits(generationRoots);
GenerationUnit primaryUnit = units[units.Count - 1];
GenerationUnit primaryUnit = units[0];
List<string> changedExistingFiles = units
.Where(unit => unit.SourceChanged)
@@ -382,35 +381,8 @@ namespace SepCore.Editor
private static List<UISerializationRoot> CollectGenerationRoots(UISerializationRoot selectedRoot)
{
bool persistentContext = EditorUtility.IsPersistent(selectedRoot);
var result = new List<UISerializationRoot>();
var visited = new HashSet<UISerializationRoot>();
CollectGenerationRootsRecursive(selectedRoot, persistentContext, visited, result);
return result;
}
private static void CollectGenerationRootsRecursive(
UISerializationRoot current,
bool persistentContext,
HashSet<UISerializationRoot> visited,
List<UISerializationRoot> result)
{
current = GetCanonicalRoot(current, persistentContext);
if (current == null || !visited.Add(current))
{
return;
}
current.RefreshItems();
for (int i = 0; i < current.NestedFormReferences.Count; i++)
{
UISerializationRoot nestedRoot = current.NestedFormReferences[i].Root;
if (nestedRoot != null)
{
CollectGenerationRootsRecursive(nestedRoot, persistentContext, visited, result);
}
}
result.Add(current);
UISerializationRoot generationRoot = GetCanonicalRoot(selectedRoot, persistentContext);
return new List<UISerializationRoot> { generationRoot };
}
private static UISerializationRoot GetCanonicalRoot(UISerializationRoot root, bool persistentContext)
@@ -533,18 +505,51 @@ namespace SepCore.Editor
throw new InvalidOperationException("A nested UI Form reference is missing below " + GetHierarchyPath(root.transform) + ".");
}
UGuiForm nestedForm = FindNestedForm(nestedRoot, out string nestedFormError);
if (nestedForm == null)
{
throw new InvalidOperationException(nestedFormError);
}
string variableName = NormalizeAndValidateVariableName(reference.VariableName, nestedRoot.transform, names);
bindings.Add(new Binding
{
nestedRoot = nestedRoot,
component = nestedForm,
variableName = variableName,
csharpTypeName = "global::" + GeneratedNamespace + "." + GetFormClassName(nestedRoot)
csharpTypeName = GetCSharpTypeName(nestedForm.GetType())
});
}
return bindings;
}
public static UGuiForm FindNestedForm(UISerializationRoot nestedRoot, out string error)
{
error = null;
if (nestedRoot == null)
{
error = "The nested UI root is missing.";
return null;
}
UGuiForm[] forms = nestedRoot.GetComponents<UGuiForm>();
if (forms.Length == 0)
{
error = "Nested UI root '" + GetHierarchyPath(nestedRoot.transform) +
"' does not have a UGuiForm component. Generate its View & Form scripts first.";
return null;
}
if (forms.Length > 1)
{
error = "Nested UI root '" + GetHierarchyPath(nestedRoot.transform) +
"' has multiple UGuiForm components.";
return null;
}
return forms[0];
}
private static string NormalizeAndValidateVariableName(string value, Transform owner, HashSet<string> names)
{
string variableName = value == null ? string.Empty : value.Trim();
@@ -755,26 +760,7 @@ namespace SepCore.Editor
throw new InvalidOperationException("Generated field was not found: " + binding.variableName + ".");
}
UnityEngine.Object target = binding.component;
if (binding.nestedRoot != null)
{
string nestedFormFullName = GeneratedNamespace + "." + GetFormClassName(binding.nestedRoot);
Type nestedFormType = FindGeneratedType(nestedFormFullName);
if (nestedFormType == null)
{
throw new InvalidOperationException("Nested UI form type was not found: " + nestedFormFullName + ".");
}
target = binding.nestedRoot.GetComponent(nestedFormType);
if (target == null)
{
throw new InvalidOperationException(
"Nested UI root '" + GetHierarchyPath(binding.nestedRoot.transform) +
"' does not have its generated Form component '" + nestedFormFullName + "'.");
}
}
property.objectReferenceValue = target;
property.objectReferenceValue = binding.component;
}
serializedView.ApplyModifiedProperties();