AlicizaX/Client/Packages/com.alicizax.uxtool/Editor/UXGUI/SceneView/ControllerEdit/UXControllerAddWindow.cs
陈思海 5862fb2af1 add
1.新增手柄相关适配
2.新增设备重映射绑定
3.新增设备映射图标更新
2025-12-09 20:31:44 +08:00

108 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AlicizaX.UI;
using AlicizaX.UI.Runtime;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.UIElements;
namespace AlicizaX.UXTool
{
public class UXControllerAddWindow : EditorWindow
{
private UXController _ux;
private TextField _nameField;
private IntegerField _lengthField;
public static void ShowWindow(UXController ux)
{
var w = CreateInstance<UXControllerAddWindow>();
w.titleContent = new GUIContent("添加控制器");
w._ux = ux;
w.position = new Rect(Screen.width / 2f - 150f, Screen.height / 2f - 60f, 320, 120);
w.ShowUtility();
}
private void OnEnable()
{
var root = rootVisualElement;
root.style.paddingLeft = 8;
root.style.paddingTop = 8;
root.style.paddingRight = 8;
root.style.paddingBottom = 8;
var title = new Label("添加控制器");
title.style.unityFontStyleAndWeight = FontStyle.Bold;
title.style.marginBottom = 6;
root.Add(title);
_nameField = new TextField("Name");
_nameField.value = _ux != null ? $"Controller{_ux.Controllers.Count}" : "Controller";
root.Add(_nameField);
_lengthField = new IntegerField("Length");
_lengthField.value = 2;
root.Add(_lengthField);
var row = new VisualElement();
row.style.flexDirection = FlexDirection.Row;
row.style.justifyContent = Justify.FlexEnd;
row.style.marginTop = 8;
var add = new Button(() =>
{
if (_ux != null)
{
Undo.RecordObject(_ux, "Add Controller");
// 使用 SerializedObject 能保证序列化
var so = new SerializedObject(_ux);
var controllersProp = so.FindProperty("_controllers");
int newIndex = controllersProp.arraySize;
controllersProp.InsertArrayElementAtIndex(newIndex);
var el = controllersProp.GetArrayElementAtIndex(newIndex);
var nameProp = el.FindPropertyRelative("Name");
var lengthProp = el.FindPropertyRelative("Length");
nameProp.stringValue = _nameField.value;
lengthProp.intValue = Mathf.Max(1, _lengthField.value);
so.ApplyModifiedProperties();
EditorUtility.SetDirty(_ux);
}
else
{
// 如果外部没有提供 UXController尝试在 prefab root 上添加
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage != null && prefabStage.prefabContentsRoot != null)
{
var comp = Undo.AddComponent<UXController>(prefabStage.prefabContentsRoot);
EditorUtility.SetDirty(prefabStage.prefabContentsRoot);
if (comp != null)
{
Undo.RecordObject(comp, "Add Controller");
var so = new SerializedObject(comp);
var controllersProp = so.FindProperty("_controllers");
controllersProp.InsertArrayElementAtIndex(0);
var el = controllersProp.GetArrayElementAtIndex(0);
var nameProp = el.FindPropertyRelative("Name");
var lengthProp = el.FindPropertyRelative("Length");
nameProp.stringValue = _nameField.value;
lengthProp.intValue = Mathf.Max(1, _lengthField.value);
so.ApplyModifiedProperties();
EditorUtility.SetDirty(comp);
}
}
}
Close();
});
add.text = "添加";
row.Add(add);
var cancel = new Button(() => Close());
cancel.text = "取消";
cancel.style.marginLeft = 8;
row.Add(cancel);
root.Add(row);
}
}
}