This commit is contained in:
2024-10-16 00:03:41 +08:00
commit 897058435c
5033 changed files with 1009728 additions and 0 deletions

View File

@@ -0,0 +1,400 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using IcecreamView;
using IcecreamView.Editor;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.UIElements;
using Object = System.Object;
namespace IcecreamView
{
public class IceCreamUIBox : EditorWindow
{
private Label _viewTitle;
private Label _viewDesc;
private VisualElement _customUIInfo;
private VisualElement _customUIWork;
private VisualElement _createInfo;
private VisualElement _createWork;
private Label _customUITitle;
private Label _customUIDesc;
private Button _createBtn;
private IC_ModuleConnector connector;
private CanvasNode _baseCanvasNode;
//---------------------------create work------------------------------------
private Button _loadUISceneBtn;
private Label _createSceneInfoLabel;
private Label _createSceneInfoDesc;
private ListView _createPanelListView;
private TextField _createPanelName;
private SliderInt _createPanelLayer;
private Toggle _createPanelToggle;
private Button _createPanelCreateBtn;
// private Label
//---------------------------create work------------------------------------
private Transform _onSelectionTransform;
[MenuItem("IceCream/IceCream UI Box")]
public static void ShowBoxTool()
{
IceCreamUIBox wnd = GetWindow<IceCreamUIBox>();
wnd.titleContent = new GUIContent("IceCream UI Box");
}
public void CreateGUI()
{
// Debug.Log("创建 IceCream UI Box");
// return;
UnityEditor.Selection.selectionChanged += OnSelectionChangeScene;
VisualElement root = rootVisualElement;
var fromScriptableObject = MonoScript.FromScriptableObject(this);
var path = AssetDatabase.GetAssetPath(fromScriptableObject);
//将指定的path改为同路径下后缀为.uxml的路径
path = path.Replace(".cs", ".uxml");
var window = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(path);
if (window == null)
{
Debug.LogError("无法加载uxml文件 " + path + " 请检查插件是否损坏!");
return;
}
VisualElement container = window.Instantiate();
_customUIInfo = container.Q<VisualElement>("BoxViewInfo");
_customUIWork = container.Q<VisualElement>("BoxViewCustom");
_createInfo = container.Q<VisualElement>("BoxPanelInfo");
_createWork = container.Q<VisualElement>("BoxPanelCreate");
_viewTitle = _customUIInfo.Q<Label>("ViewTitle");
_viewDesc = _customUIInfo.Q<Label>("Desc");
_createBtn = _customUIWork.Q<Button>("CreateBtn");
_customUITitle = _customUIWork.Q<Label>("CustomUITitle");
_customUIDesc = _customUIWork.Q<Label>("CustomUIDesc");
var uxmlButton = _customUIInfo.Q<Button>("UpdateViewBtn");
_loadUISceneBtn = _createInfo.Q<Button>("LoadSceneBtn");
_createSceneInfoLabel = _createInfo.Q<Label>("ViewTitle");
_createSceneInfoDesc = _createInfo.Q<Label>("Desc");
_createPanelName = _createWork.Q<TextField>("PanelName");
_createPanelLayer = _createWork.Q<SliderInt>("PanelLayer");
_createPanelToggle = _createWork.Q<Toggle>("FocusToggle");
_createPanelCreateBtn = _createWork.Q<Button>("CreateBtn");
_createPanelListView = _createWork.Q<ListView>("CustomUiListView");
uxmlButton.RegisterCallback<MouseUpEvent>(OnClick_UpdateViewInfo);
_createBtn.RegisterCallback<MouseUpEvent>(OnClick_Create);
_loadUISceneBtn.RegisterCallback<MouseUpEvent>(OnClick_LoadUIScene);
_createPanelCreateBtn.RegisterCallback<MouseUpEvent>(OnClick_CreatePanel);
_createPanelListView.onSelectionChange += OnPanelItemSelected;
_customUIInfo.SetEnabled(false);
root.Add(container);
UpdateCustomUiView();
}
private CustomUIPanelConfig curSelectPanelPrefab;
private void OnPanelItemSelected(IEnumerable<object> obj)
{
var enumerable = obj as object[] ?? obj.ToArray();
if (enumerable.Length == 0)
{
_createWork.SetEnabled(false);
return;
}
curSelectPanelPrefab = enumerable[0] as CustomUIPanelConfig;
_createWork.SetEnabled(true);
_createPanelCreateBtn.SetEnabled(true);
_createPanelName.SetEnabled(true);
_createPanelLayer.SetEnabled(true);
_createPanelToggle.SetEnabled(true);
// ReSharper disable once UsePatternMatching
var config = curSelectPanelPrefab;
if (config != null)
{
_createPanelName.SetValueWithoutNotify(config.Name);
_createPanelLayer.value = config.Prefab.PanelLayer;
_createPanelToggle.value = config.Prefab.HasFocusView;
}
}
private void OnClick_CreatePanel(MouseUpEvent evt)
{
if (curSelectPanelPrefab == null || curSelectPanelPrefab.Prefab == null)
{
Debug.LogError("待创建对象为空,无法创建!");
return;
}
var curCanvasNode = Selection.activeTransform.GetComponent<CanvasNode>() ?? _baseCanvasNode;
//构建到场景
var obj = (MonoBehaviour)PrefabUtility.InstantiatePrefab(curSelectPanelPrefab.Prefab,
curCanvasNode.transform);
var instantiatePrefab = obj.GetComponent<RectTransform>();
instantiatePrefab.anchoredPosition = Vector2.zero;
PrefabUtility.UnpackPrefabInstance(obj.gameObject, PrefabUnpackMode.Completely,
InteractionMode.AutomatedAction);
var icModuleConnector = instantiatePrefab.GetComponent<IC_ModuleConnector>();
// var serializedObject = new SerializedObject(icModuleConnector.gameObject);
// var sortOrder = serializedObject.FindProperty("SortOrder");
// sortOrder.intValue = _createPanelLayer.value;
// var hasFocusView = serializedObject.FindProperty("hasFocusView");
// hasFocusView.boolValue = _createPanelToggle.value;
var pl = icModuleConnector.GetType();
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var sortOrder = pl.GetField("DefaultStory" , bindingFlags);
var hasFocusView = pl.GetField("hasFocusView" , bindingFlags);
sortOrder?.SetValue(icModuleConnector , _createPanelLayer.value);
hasFocusView?.SetValue(icModuleConnector , _createPanelToggle.value);
icModuleConnector.name = _createPanelName.value;
Selection.activeTransform = obj.transform;
// EditorUtility.SetDirty(icModuleConnector);
// EditorSceneManager.SaveOpenScenes();
}
private void OnClick_LoadUIScene(MouseUpEvent evt)
{
var globalInstanceUiSceneAsset = IceCreamCodeUtils.GlobalInstance.UiSceneAsset;
if (globalInstanceUiSceneAsset != null)
{
EditorSceneManager.SaveOpenScenes();
EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(globalInstanceUiSceneAsset));
UpdateCustomUiView();
}
}
private void ShowWork(int state)
{
_createInfo.style.display = new StyleEnum<DisplayStyle>(state == 0 ? DisplayStyle.Flex : DisplayStyle.None);
_createWork.style.display = new StyleEnum<DisplayStyle>(state == 0 ? DisplayStyle.Flex : DisplayStyle.None);
_customUIInfo.style.display =
new StyleEnum<DisplayStyle>(state == 1 ? DisplayStyle.Flex : DisplayStyle.None);
_customUIWork.style.display =
new StyleEnum<DisplayStyle>(state == 1 ? DisplayStyle.Flex : DisplayStyle.None);
}
private void OnSelectionChangeScene()
{
UpdateCustomUiView();
}
private void UpdateCustomUiView()
{
var activeTransform = UnityEditor.Selection.activeTransform;
if (activeTransform == null)
{
ShowWork(-1);
rootVisualElement.SetEnabled(false);
return;
}
rootVisualElement.SetEnabled(true);
// Debug.Log("当前选中" + activeTransform.name);
//根据当前选中的物体获取对应的View通过方向遍历父节点查找挂载了IC_ModuleConnector的物体如果找到则显示对应的信息
connector = null;
var target = activeTransform;
while (target != null && target != target.root)
{
if (target.TryGetComponent(out connector))
{
_onSelectionTransform = activeTransform;
break;
}
target = target.parent;
}
if (connector != null)
{
ShowWork(1);
UpdateCustomWork();
}
else
{
ShowWork(0);
UpdateCreateWork();
// _baseCanvasNode
}
}
private void UpdateCustomWork()
{
//更新ViewTitle
_viewTitle.text = $"当前根页面:" + (connector != null ? connector.name : "无");
_viewDesc.text = $"页面层级:{(connector != null ? connector.PanelLayer : 0)}";
if (_currentSelected == null)
{
_customUITitle.text = "";
_customUIDesc.text = "";
_createBtn.SetEnabled(false);
}
ShowList();
}
private void UpdateCreateWork()
{
var canvasNodes = Transform.FindObjectsOfType<CanvasNode>();
if (canvasNodes != null && canvasNodes.Length > 0)
{
_baseCanvasNode = canvasNodes[0];
}
var isShowLoadBtn = _baseCanvasNode == null && IceCreamCodeUtils.GlobalInstance.UiSceneAsset != null;
_loadUISceneBtn.style.display =
new StyleEnum<DisplayStyle>((isShowLoadBtn ? DisplayStyle.Flex : DisplayStyle.None));
// ReSharper disable once Unity.NoNullPropagation
var curCanvasNode = Selection.activeTransform.GetComponent<CanvasNode>() ?? _baseCanvasNode;
if (curCanvasNode != null)
{
_createSceneInfoLabel.text = $"当前是否可以进行UI创建 <color=green>{curCanvasNode != null}</color>";
_createSceneInfoDesc.text = $"当前创建节点Canvas{curCanvasNode.name}";
_createWork.SetEnabled(true);
}
else
{
_createSceneInfoLabel.text = $"当前是否可以进行UI创建 <color=red>{curCanvasNode != null}</color>";
_createSceneInfoDesc.text = "";
_createWork.SetEnabled(false);
}
ShowPanelList();
}
private void ShowPanelList()
{
var items = IceCreamCodeUtils.GlobalInstance.CustomPanelConfigs;
if (items == null)
{
items = new List<CustomUIPanelConfig>();
}
Func<VisualElement> makeItem = () => new Label();
Action<VisualElement, int> bindItem = (e, i) => ((Label)e).text = items[i].Name;
_createPanelListView.makeItem = makeItem;
_createPanelListView.bindItem = bindItem;
_createPanelListView.itemsSource = items;
_createPanelListView.selectionType = SelectionType.Single;
_createPanelCreateBtn.SetEnabled(false);
_createPanelName.SetEnabled(false);
_createPanelLayer.SetEnabled(false);
_createPanelToggle.SetEnabled(false);
}
private void ShowList()
{
var items = IceCreamCodeUtils.GlobalInstance.CustomUIConfigs;
if (items == null)
{
items = new List<CustomUIConfig>();
}
Func<VisualElement> makeItem = () => new Label();
Action<VisualElement, int> bindItem = (e, i) => ((Label)e).text = items[i].Name;
var listView = _customUIWork.Q<ListView>("CustomUiListView");
listView.makeItem = makeItem;
listView.bindItem = bindItem;
listView.itemsSource = items;
listView.selectionType = SelectionType.Single;
listView.onSelectionChange += OnItemSelected;
}
private void OnClick_Create(MouseUpEvent evt)
{
if (connector == null)
{
Debug.LogError("当前选中物体没有挂载IC_ModuleConnector组件无法创建");
return;
}
if (_currentSelected == null)
{
Debug.LogError("当前没有选中任何自定义UI无法创建");
return;
}
var config = _currentSelected as CustomUIConfig;
if (config == null || config.Prefab == null)
{
Debug.LogError("当前选中的自定义UI配置有误无法创建");
return;
}
//构建到场景
var obj = (MonoBehaviour)PrefabUtility.InstantiatePrefab(config.Prefab, _onSelectionTransform);
var instantiatePrefab = obj.GetComponent<RectTransform>();
//设置位置,如果父节点是RectTransform则设置为居中
if (_onSelectionTransform is RectTransform)
{
instantiatePrefab.anchoredPosition = Vector2.zero;
}
else
{
instantiatePrefab.localPosition = Vector3.zero;
}
PrefabUtility.UnpackPrefabInstance(obj.gameObject, PrefabUnpackMode.Completely,
InteractionMode.AutomatedAction);
Selection.activeTransform = obj.transform;
Debug.Log("创建成功!");
}
private void OnClick_UpdateViewInfo(MouseUpEvent evt)
{
UpdateCustomUiView();
}
private Object _currentSelected;
private void OnItemSelected(IEnumerable<object> obj)
{
var enumerable = obj as object[] ?? obj.ToArray();
if (enumerable.Length == 0)
{
_customUIInfo.SetEnabled(false);
return;
}
_currentSelected = enumerable[0];
_createBtn.SetEnabled(true);
_customUIInfo.SetEnabled(true);
// ReSharper disable once UsePatternMatching
var config = _currentSelected as CustomUIConfig;
if (config != null)
{
_customUITitle.text = config.Name;
_customUIDesc.text = config.Desc;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1bbc1495191064b45b319b93211dd6e8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,33 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
<ui:VisualElement name="BoxPanelInfo" style="height: 108px; background-color: rgba(82, 80, 80, 0.45); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; position: relative; left: 0; right: 0; border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120);">
<ui:Label text="当前是否为UI场景" display-tooltip-when-elided="true" name="ViewTitle" tooltip="当前根页面信息" usage-hints="None" style="height: 23px; -unity-font-style: bold; -unity-text-align: middle-left; font-size: 20px; color: rgb(248, 248, 248);" />
<ui:Label text="Label" display-tooltip-when-elided="true" name="Desc" style="height: 43px; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; color: rgb(171, 171, 171);" />
<ui:Button text="跳转到UI制作场景" display-tooltip-when-elided="true" name="LoadSceneBtn" style="align-items: center; justify-content: flex-end; -unity-text-align: upper-center; white-space: nowrap; position: relative; top: auto; left: 0; height: 23px; width: auto; -unity-font-style: normal; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; right: 0; bottom: auto; overflow: visible; visibility: visible; display: flex; opacity: 1;" />
</ui:VisualElement>
<ui:VisualElement name="BoxPanelCreate" style="height: 257px; background-color: rgba(82, 80, 80, 0.45); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; position: relative; left: 0; right: 0; border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120);">
<ui:Label text="页面构建" display-tooltip-when-elided="true" name="PanelCreateTitle" usage-hints="None" style="height: 23px; -unity-font-style: normal; -unity-text-align: middle-left; font-size: 18px; color: rgb(248, 248, 248);" />
<ui:VisualElement style="height: 2px; background-color: rgb(46, 46, 46); margin-top: 5px; margin-bottom: 5px;" />
<ui:ListView focusable="true" reorderable="false" reorder-mode="Simple" name="CustomUiListView" style="padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; background-color: rgb(63, 63, 63); border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-top-left-radius: 5px; border-bottom-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; height: 108px; max-height: 108px;" />
<ui:VisualElement name="CustomUIInfo" style="border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; height: auto; border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; justify-content: flex-start; bottom: 0; position: relative; width: auto; left: 0; right: 0; align-items: stretch; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px;">
<ui:TextField picking-mode="Ignore" label="页面名称" text="New Panel" name="PanelName" />
<ui:SliderInt picking-mode="Ignore" label="页面层级" value="0" high-value="100" show-input-field="true" name="PanelLayer" low-value="-50" />
<ui:Toggle label="是否聚焦" name="FocusToggle" value="true" />
<ui:Button text="构建到场景" display-tooltip-when-elided="true" name="CreateBtn" style="white-space: nowrap; align-items: stretch; left: auto; right: auto; justify-content: flex-start; flex-direction: column; flex-wrap: nowrap; transform-origin: center;" />
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="BoxViewInfo" style="height: 108px; background-color: rgba(82, 80, 80, 0.45); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; position: relative; left: 0; right: 0; border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120); display: none;">
<ui:Label text="当前根页面xxxxx" display-tooltip-when-elided="true" name="ViewTitle" tooltip="当前根页面信息" usage-hints="None" style="height: 23px; -unity-font-style: bold; -unity-text-align: middle-left; font-size: 20px; color: rgb(248, 248, 248);" />
<ui:Label text="Label" display-tooltip-when-elided="true" name="Desc" style="height: 43px; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; color: rgb(171, 171, 171);" />
<ui:Button text="刷新" display-tooltip-when-elided="true" name="UpdateViewBtn" style="align-items: center; justify-content: flex-end; -unity-text-align: upper-center; white-space: nowrap; position: relative; top: auto; left: 0; height: 23px; width: auto; -unity-font-style: normal; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; right: 0; bottom: auto; overflow: visible; visibility: visible; display: none; opacity: 1;" />
</ui:VisualElement>
<ui:VisualElement name="BoxViewCustom" style="height: 257px; background-color: rgba(82, 80, 80, 0.45); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; position: relative; left: 0; right: 0; border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120); display: none;">
<ui:Label text="自定义组件库" display-tooltip-when-elided="true" name="ViewTitle" tooltip="当前根页面信息" usage-hints="None" style="height: 23px; -unity-font-style: normal; -unity-text-align: middle-left; font-size: 18px; color: rgb(248, 248, 248);" />
<ui:VisualElement style="height: 2px; background-color: rgb(46, 46, 46); margin-top: 5px; margin-bottom: 5px;" />
<ui:ListView focusable="true" reorderable="false" reorder-mode="Simple" name="CustomUiListView" style="padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px; margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; background-color: rgb(63, 63, 63); border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; border-top-left-radius: 5px; border-bottom-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; height: 108px; max-height: 108px;" />
<ui:VisualElement name="CustomUIInfo" style="border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; height: auto; border-left-color: rgb(120, 120, 120); border-right-color: rgb(120, 120, 120); border-top-color: rgb(120, 120, 120); border-bottom-color: rgb(120, 120, 120); border-left-width: 1px; border-right-width: 1px; border-top-width: 1px; border-bottom-width: 1px; justify-content: flex-start; bottom: 0; position: relative; width: auto; left: 0; right: 0; align-items: stretch; padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px;">
<ui:Label text="Label" display-tooltip-when-elided="true" name="CustomUITitle" style="margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; -unity-font-style: bold;" />
<ui:Label text="Label" display-tooltip-when-elided="true" name="CustomUIDesc" style="margin-left: 5px; margin-right: 5px; margin-top: 5px; margin-bottom: 5px; -unity-font-style: normal; color: rgb(168, 168, 168);" />
<ui:Button text="构建到场景" display-tooltip-when-elided="true" name="CreateBtn" style="white-space: nowrap; align-items: stretch; left: auto; right: auto; justify-content: flex-start; flex-direction: column; flex-wrap: nowrap; transform-origin: center;" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 4ec32f900ab7abb4ab911e12724e2dad
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}