fix(ShaderBake): 兼容 2021.3——数字/枚举输入框改用本项目命名空间下的子类

2021.3 把 FloatField / IntegerField / EnumField 放在 UnityEditor.UIElements,
2022.1 才挪到 UnityEngine.UIElements。UXML 的 xmlns 前缀只能指一个命名空间,
写在任何一个版本上另一个版本都会报 "has no registered factory method"。
包一层同名子类 + 自己的 UxmlFactory,UXML 统一用 xer: 前缀,版本差异只留在 using 别名里。
This commit is contained in:
2026-11-05 17:27:43 +08:00
parent 1555ef658d
commit 0db9bd5462
3 changed files with 83 additions and 42 deletions
+8 -8
View File
@@ -1,4 +1,4 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:x="UnityEditor.UIElements">
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:x="UnityEditor.UIElements" xmlns:xer="XericUI.ShaderBake.Editor">
<!--
材质属性表的一行模板:结构写在这里,控件种类摊开成兄弟节点,
BakeShaderPropertyTable.cs 负责"留哪一个、其余收起来"。
@@ -8,21 +8,21 @@
<ui:VisualElement name="prop-row" class="prop-row">
<ui:Label name="prop-label" class="prop-label" text="属性" />
<ui:VisualElement name="prop-field" class="prop-field">
<ui:FloatField name="prop-float" class="prop-inline" />
<ui:IntegerField name="prop-int" class="prop-inline" />
<xer:FloatField name="prop-float" class="prop-inline" />
<xer:IntegerField name="prop-int" class="prop-inline" />
<ui:Toggle name="prop-toggle" class="prop-inline" />
<ui:DropdownField name="prop-enum" class="prop-inline" />
<x:ColorField name="prop-color" class="prop-inline" />
<x:ObjectField name="prop-texture" class="prop-inline" />
<ui:VisualElement name="prop-range" class="prop-range">
<ui:Slider name="prop-slider" class="prop-slider" />
<ui:FloatField name="prop-range-value" class="prop-range-value" />
<xer:FloatField name="prop-range-value" class="prop-range-value" />
</ui:VisualElement>
<ui:VisualElement name="prop-vector" class="prop-vector">
<ui:FloatField name="prop-vec-x" label="X" class="prop-vec-part" />
<ui:FloatField name="prop-vec-y" label="Y" class="prop-vec-part" />
<ui:FloatField name="prop-vec-z" label="Z" class="prop-vec-part" />
<ui:FloatField name="prop-vec-w" label="W" class="prop-vec-part" />
<xer:FloatField name="prop-vec-x" label="X" class="prop-vec-part" />
<xer:FloatField name="prop-vec-y" label="Y" class="prop-vec-part" />
<xer:FloatField name="prop-vec-z" label="Z" class="prop-vec-part" />
<xer:FloatField name="prop-vec-w" label="W" class="prop-vec-part" />
</ui:VisualElement>
</ui:VisualElement>
</ui:VisualElement>
+34 -34
View File
@@ -1,4 +1,4 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:x="UnityEditor.UIElements">
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:x="UnityEditor.UIElements" xmlns:xer="XericUI.ShaderBake.Editor">
<!--
Xeric UI Action Vessel — Bake Shader As Image 面板结构。
这一份只管"有哪些块、块里有哪些控件、叫什么名字",
@@ -69,14 +69,14 @@
<ui:Toggle name="sec-output-head" label="输出" value="true" class="section-head" />
<ui:VisualElement name="sec-output-body" class="section-body">
<ui:VisualElement class="field-row">
<ui:IntegerField name="out-width" label="宽" class="field-half" />
<ui:IntegerField name="out-height" label="高" class="field-half" />
<xer:IntegerField name="out-width" label="宽" class="field-half" />
<xer:IntegerField name="out-height" label="高" class="field-half" />
</ui:VisualElement>
<ui:VisualElement class="field-row">
<ui:IntegerField name="out-depth" label="深度" class="field-half" />
<xer:IntegerField name="out-depth" label="深度" class="field-half" />
<ui:DropdownField name="out-msaa" label="采样" class="field-half" />
</ui:VisualElement>
<ui:IntegerField name="out-pass" label="Pass 下标" class="field-full" />
<xer:IntegerField name="out-pass" label="Pass 下标" class="field-full" />
<x:ColorField name="out-clear" label="背景色" class="field-full" />
<ui:Toggle name="out-hdr" label="HDR(浮点目标)" class="field-toggle" />
<ui:Toggle name="out-srgb" label="sRGB 存储" class="field-toggle" />
@@ -88,43 +88,43 @@
<ui:VisualElement name="sec-surface" class="section">
<ui:Toggle name="sec-surface-head" label="面片" value="true" class="section-head" />
<ui:VisualElement name="sec-surface-body" class="section-body">
<ui:EnumField name="surf-kind" label="种类" class="field-full" />
<xer:EnumField name="surf-kind" label="种类" class="field-full" />
<ui:VisualElement class="field-row">
<ui:FloatField name="surf-size-x" label="宽" class="field-half" />
<ui:FloatField name="surf-size-y" label="高" class="field-half" />
<xer:FloatField name="surf-size-x" label="宽" class="field-half" />
<xer:FloatField name="surf-size-y" label="高" class="field-half" />
</ui:VisualElement>
<ui:VisualElement class="field-row">
<ui:IntegerField name="surf-seg-x" label="列" class="field-half" />
<ui:IntegerField name="surf-seg-y" label="行" class="field-half" />
<xer:IntegerField name="surf-seg-x" label="列" class="field-half" />
<xer:IntegerField name="surf-seg-y" label="行" class="field-half" />
</ui:VisualElement>
<ui:Label class="field-note" text="UV" />
<ui:VisualElement class="field-row">
<ui:FloatField name="surf-uv-off-x" label="偏移X" class="field-half" />
<ui:FloatField name="surf-uv-off-y" label="偏移Y" class="field-half" />
<xer:FloatField name="surf-uv-off-x" label="偏移X" class="field-half" />
<xer:FloatField name="surf-uv-off-y" label="偏移Y" class="field-half" />
</ui:VisualElement>
<ui:VisualElement class="field-row">
<ui:FloatField name="surf-uv-tile-x" label="重复X" class="field-half" />
<ui:FloatField name="surf-uv-tile-y" label="重复Y" class="field-half" />
<xer:FloatField name="surf-uv-tile-x" label="重复X" class="field-half" />
<xer:FloatField name="surf-uv-tile-y" label="重复Y" class="field-half" />
</ui:VisualElement>
<ui:Label class="field-note" text="面片变换" />
<ui:VisualElement class="field-row">
<ui:FloatField name="surf-pos-x" label="X" class="field-third" />
<ui:FloatField name="surf-pos-y" label="Y" class="field-third" />
<ui:FloatField name="surf-pos-z" label="Z" class="field-third" />
<xer:FloatField name="surf-pos-x" label="X" class="field-third" />
<xer:FloatField name="surf-pos-y" label="Y" class="field-third" />
<xer:FloatField name="surf-pos-z" label="Z" class="field-third" />
</ui:VisualElement>
<ui:VisualElement class="field-row">
<ui:FloatField name="surf-rot-x" label="X" class="field-third" />
<ui:FloatField name="surf-rot-y" label="Y" class="field-third" />
<ui:FloatField name="surf-rot-z" label="Z" class="field-third" />
<xer:FloatField name="surf-rot-x" label="X" class="field-third" />
<xer:FloatField name="surf-rot-y" label="Y" class="field-third" />
<xer:FloatField name="surf-rot-z" label="Z" class="field-third" />
</ui:VisualElement>
<ui:VisualElement class="field-row">
<ui:FloatField name="surf-scale-x" label="X" class="field-third" />
<ui:FloatField name="surf-scale-y" label="Y" class="field-third" />
<ui:FloatField name="surf-scale-z" label="Z" class="field-third" />
<xer:FloatField name="surf-scale-x" label="X" class="field-third" />
<xer:FloatField name="surf-scale-y" label="Y" class="field-third" />
<xer:FloatField name="surf-scale-z" label="Z" class="field-third" />
</ui:VisualElement>
<ui:Label class="field-note" text="外部网格" />
<x:ObjectField name="surf-mesh" label="网格" class="field-full" />
<ui:IntegerField name="surf-submesh" label="SubMesh" class="field-full" />
<xer:IntegerField name="surf-submesh" label="SubMesh" class="field-full" />
<ui:Toggle name="surf-normalize" label="缩放到面片尺寸" class="field-toggle" />
<ui:Toggle name="surf-uv-st" label="UV 再写进 _MainTex_ST" class="field-toggle" />
<ui:VisualElement class="field-row">
@@ -145,23 +145,23 @@
<ui:Toggle name="sec-camera-head" label="相机" value="true" class="section-head" />
<ui:VisualElement name="sec-camera-body" class="section-body">
<ui:Toggle name="cam-ortho" label="正交" class="field-toggle" />
<ui:FloatField name="cam-ortho-size" label="正交半高" class="field-full" />
<ui:FloatField name="cam-fov" label="视野角" class="field-full" />
<xer:FloatField name="cam-ortho-size" label="正交半高" class="field-full" />
<xer:FloatField name="cam-fov" label="视野角" class="field-full" />
<ui:VisualElement class="field-row">
<ui:FloatField name="cam-near" label="近" class="field-half" />
<ui:FloatField name="cam-far" label="远" class="field-half" />
<xer:FloatField name="cam-near" label="近" class="field-half" />
<xer:FloatField name="cam-far" label="远" class="field-half" />
</ui:VisualElement>
<ui:Label class="field-note" text="位置" />
<ui:VisualElement class="field-row">
<ui:FloatField name="cam-pos-x" label="X" class="field-third" />
<ui:FloatField name="cam-pos-y" label="Y" class="field-third" />
<ui:FloatField name="cam-pos-z" label="Z" class="field-third" />
<xer:FloatField name="cam-pos-x" label="X" class="field-third" />
<xer:FloatField name="cam-pos-y" label="Y" class="field-third" />
<xer:FloatField name="cam-pos-z" label="Z" class="field-third" />
</ui:VisualElement>
<ui:Label class="field-note" text="旋转(欧拉角)" />
<ui:VisualElement class="field-row">
<ui:FloatField name="cam-rot-x" label="X" class="field-third" />
<ui:FloatField name="cam-rot-y" label="Y" class="field-third" />
<ui:FloatField name="cam-rot-z" label="Z" class="field-third" />
<xer:FloatField name="cam-rot-x" label="X" class="field-third" />
<xer:FloatField name="cam-rot-y" label="Y" class="field-third" />
<xer:FloatField name="cam-rot-z" label="Z" class="field-third" />
</ui:VisualElement>
<ui:Button name="cam-fit" text="框住面片" class="field-full" />
</ui:VisualElement>
+41
View File
@@ -0,0 +1,41 @@
#if UNITY_2022_1_OR_NEWER
using BaseFloatField = UnityEngine.UIElements.FloatField;
using BaseIntegerField = UnityEngine.UIElements.IntegerField;
using BaseEnumField = UnityEngine.UIElements.EnumField;
#else
using BaseFloatField = UnityEditor.UIElements.FloatField;
using BaseIntegerField = UnityEditor.UIElements.IntegerField;
using BaseEnumField = UnityEditor.UIElements.EnumField;
#endif
using UnityEngine.UIElements;
namespace XericUI.ShaderBake.Editor
{
/// <summary>
/// 这三个数字/枚举输入框在 2021.3 与 2022.1 分属两个命名空间:
/// 2021.3 在 <c>UnityEditor.UIElements</c>,2022.1 挪到了 <c>UnityEngine.UIElements</c>。
///
/// <para><b>为什么还要包一层</b>:UXML 的标签前缀只能指向一个固定命名空间,写
/// <c>ui:FloatField</c> 在 2021.3 里会报 "has no registered factory method",写
/// <c>x:FloatField</c> 又在 2022.1 里失效。包一个自己命名空间下的同名子类,
/// 两份 UXML 就能共用同一个 <c>xer:FloatField</c> 标签,版本差异只留在 C# 的 using 别名里。</para>
///
/// <para>子类不做任何事,只负责把基类的 <c>UxmlTraits</c>(label/value 等)和工厂带过来。</para>
/// </summary>
public class FloatField : BaseFloatField
{
public new class UxmlFactory : UxmlFactory<FloatField, UxmlTraits> { }
}
/// <inheritdoc cref="FloatField"/>
public class IntegerField : BaseIntegerField
{
public new class UxmlFactory : UxmlFactory<IntegerField, UxmlTraits> { }
}
/// <inheritdoc cref="FloatField"/>
public class EnumField : BaseEnumField
{
public new class UxmlFactory : UxmlFactory<EnumField, UxmlTraits> { }
}
}