Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

101 行
2.7 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
using UnityEngine.Experimental.UIElements;
using UnityEditor;
using System;
using StatusFlags = UnityEngine.Experimental.UIElements.DropdownMenu.MenuAction.StatusFlags;
namespace GraphProcessor
{
public class ToolbarView : VisualElement
{
class ToolbarButtonData
{
public string name;
public bool toggle;
public bool value;
public Action buttonCallback;
public Action< bool > toggleCallback;
}
List< ToolbarButtonData > leftButtonDatas = new List< ToolbarButtonData >();
List< ToolbarButtonData > rightButtonDatas = new List< ToolbarButtonData >();
protected BaseGraphView graphView;
public ToolbarView(BaseGraphView graphView)
{
name = "ToolbarView";
this.graphView = graphView;
graphView.initialized += AddButtons;
Add(new IMGUIContainer(DrawImGUIToolbar));
}
protected void AddButton(string name, Action callback, bool left = true)
{
((left) ? leftButtonDatas : rightButtonDatas).Add(new ToolbarButtonData{
name = name,
toggle = false,
buttonCallback = callback
});
}
protected void AddToggle(string name, bool defaultValue, Action< bool > callback, bool left = true)
{
((left) ? leftButtonDatas : rightButtonDatas).Add(new ToolbarButtonData{
name = name,
toggle = true,
value = defaultValue,
toggleCallback = callback
});
}
protected virtual void AddButtons()
{
AddButton("Center", graphView.ResetPositionAndZoom);
bool processorVisible = graphView.GetPinnedElementStatus< ProcessorView >() != StatusFlags.Hidden;
AddToggle("Show Processor", processorVisible, (v) => graphView.ToggleView< ProcessorView>());
AddButton("Show In Project", () => EditorGUIUtility.PingObject(graphView.graph), false);
}
void DrawImGUIButtonList(List< ToolbarButtonData > buttons)
{
foreach (var button in buttons)
{
if (button.toggle)
{
EditorGUI.BeginChangeCheck();
button.value = GUILayout.Toggle(button.value, button.name, EditorStyles.toolbarButton);
if (EditorGUI.EndChangeCheck() && button.toggleCallback != null)
button.toggleCallback(button.value);
}
else
{
if (GUILayout.Button(button.name, EditorStyles.toolbarButton) && button.buttonCallback != null)
button.buttonCallback();
}
}
}
void DrawImGUIToolbar()
{
GUILayout.BeginHorizontal(EditorStyles.toolbar);
DrawImGUIButtonList(leftButtonDatas);
GUILayout.FlexibleSpace();
DrawImGUIButtonList(rightButtonDatas);
GUILayout.EndHorizontal();
}
}
}