File size: 16,743 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityObject = UnityEngine.Object;
namespace Unity.VisualScripting
{
internal class DropdownOptions
{
internal bool interactable = true;
internal string label;
internal string tooltip;
internal Action callback;
}
internal class SplitDropdown
{
private bool reset;
private List<DropdownOptions> listOfOptions;
internal SplitDropdown(List<DropdownOptions> listOfOptions)
{
this.listOfOptions = listOfOptions;
}
internal DropdownOptions GetOption(int index)
{
return listOfOptions[index];
}
internal void Reset()
{
reset = true;
}
internal bool Draw(Rect area, string label, ref bool toggleState)
{
GUI.Box(area, string.Empty, EmptyGraphWindow.buttonStyle);
if (GUI.Button(new Rect(area.x, area.y, area.width - area.height, area.height), label,
EmptyGraphWindow.labelStyleButton))
{
toggleState = false;
return true;
}
Rect toggleRect = new Rect(area.x + area.width - area.height - 2, area.y - 1, area.height + 3,
area.height + 3);
toggleState = GUI.Toggle(toggleRect, toggleState, "", EmptyGraphWindow.labelStyleButton);
GUI.Label(toggleRect, EmptyGraphWindow.dropdownIcon, EmptyGraphWindow.titleStyle);
if (toggleState)
{
GUI.BeginGroup(new Rect(area.x, area.y + area.height - 1, area.width, area.height * 2),
EmptyGraphWindow.buttonStyle);
Rect areaChild = new Rect(0, 0, EmptyGraphWindow.buttonWidth,
EmptyGraphWindow.buttonHeight);
GUIContent contentOption;
foreach (DropdownOptions dropdownOption in listOfOptions)
{
EditorGUI.BeginDisabledGroup(!dropdownOption.interactable);
if (dropdownOption.interactable)
{
contentOption = new GUIContent(dropdownOption.label);
}
else
{
contentOption = new GUIContent(dropdownOption.label, dropdownOption.tooltip);
}
if (GUI.Button(areaChild, contentOption, EmptyGraphWindow.labelStyleDropdownOptions))
{
toggleState = false;
dropdownOption.callback();
}
EditorGUI.EndDisabledGroup();
areaChild.y += areaChild.height;
}
GUI.EndGroup();
}
if (reset)
{
toggleState = false;
reset = false;
}
return false;
}
}
internal class EmptyGraphWindow : EditorWindow
{
internal static GUIContent dropdownIcon;
private bool toggleOnScript = false;
private bool toggleOnState = false;
internal static GUIStyle buttonStyle;
internal static GUIStyle labelStyleButton;
internal static GUIStyle labelStyleDropdownOptions;
internal static GUIStyle titleStyle;
internal static int titleHeight = 120;
internal static int buttonWidth = 200;
internal static int buttonHeight = 22;
private bool shouldCloseWindow;
private Vector2 scrollPosition;
private Rect scrollArea;
private SplitDropdown splitDropdownScriptGraph;
private SplitDropdown splitDropdownStateGraph;
const string k_OnSelectedGameObject = "...on selected GameObject";
const string k_OnNewGameObject = "...on new GameObject";
const string k_SelectedGameObject = "Please, select a GameObject";
[MenuItem("Window/Visual Scripting/Visual Scripting Graph", false, 3010)]
private static void ShowWindow()
{
EmptyGraphWindow window = GetWindow<EmptyGraphWindow>();
window.titleContent = new GUIContent("Visual Scripting");
}
private void OnEnable()
{
string pathRoot = PathUtility.GetPackageRootPath();
UnityObject icon = EditorGUIUtility.Load(Path.Combine(pathRoot,
"Editor/VisualScripting.Shared/EditorAssetResources/SplitButtonArrow.png"));
dropdownIcon = new GUIContent(icon as Texture2D);
scrollArea = new Rect(0, 0, 630, 300);
toggleOnScript = false;
toggleOnState = false;
shouldCloseWindow = false;
var listOfOptions = new List<DropdownOptions>
{
new DropdownOptions
{
label = k_OnSelectedGameObject,
tooltip = k_SelectedGameObject,
callback = CreateScriptGraphOnSelectedGameObject
},
new DropdownOptions
{
label = k_OnNewGameObject,
callback = CreateScriptGraphOnNewGameObject
}
};
splitDropdownScriptGraph = new SplitDropdown(listOfOptions);
listOfOptions = new List<DropdownOptions>
{
new DropdownOptions
{
label = k_OnSelectedGameObject,
tooltip = k_SelectedGameObject,
callback = CreateStateGraphOnSelectedGameObject
},
new DropdownOptions
{
label = k_OnNewGameObject,
callback = CreateStateGraphOnNewGameObject
}
};
splitDropdownStateGraph = new SplitDropdown(listOfOptions);
}
private void OpenGraphAsset(UnityObject unityObject, bool shouldSetSceneAsDirty)
{
shouldCloseWindow = true;
ScriptGraphAsset scriptGraphAsset = unityObject as ScriptGraphAsset;
GraphReference graphReference = null;
if (scriptGraphAsset != null)
{
graphReference = GraphReference.New(scriptGraphAsset, true);
}
else
{
StateGraphAsset stateGraphAsset = unityObject as StateGraphAsset;
if (stateGraphAsset != null)
{
graphReference = GraphReference.New(stateGraphAsset, true);
}
}
if (shouldSetSceneAsDirty)
{
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
}
GraphWindow.OpenActive(graphReference);
}
private void OpenGraphFromPath(string path, bool shouldSetSceneAsDirty)
{
path = path.Replace(Application.dataPath, "Assets");
UnityObject unityObject = AssetDatabase.LoadAssetAtPath(path, typeof(UnityObject));
OpenGraphAsset(unityObject, shouldSetSceneAsDirty);
}
private void OpenGraph()
{
EditorGUIUtility.ShowObjectPicker<MacroScriptableObject>(null, false, String.Empty,
EditorGUIUtility.GetControlID(FocusType.Passive));
}
private bool CreateScriptGraphAsset(GameObject gameObject = null, bool updateName = false)
{
var path = EditorUtility.SaveFilePanelInProject("Save Graph", "New Script Graph", "asset", null);
if (string.IsNullOrEmpty(path))
{
return false;
}
VSUsageUtility.isVisualScriptingUsed = true;
var macro = (IMacro)CreateInstance(typeof(ScriptGraphAsset));
var macroObject = (UnityObject)macro;
macro.graph = FlowGraph.WithStartUpdate();
if (gameObject != null)
{
ScriptMachine flowMachine = gameObject.AddComponent<ScriptMachine>();
flowMachine.nest.macro = (ScriptGraphAsset)macro;
}
string filename = Path.GetFileNameWithoutExtension(path);
if (updateName)
{
gameObject.name = filename;
}
macroObject.name = filename;
AssetDatabase.CreateAsset(macroObject, path);
bool shouldSetSceneAsDirty = gameObject != null;
OpenGraphFromPath(path, shouldSetSceneAsDirty);
return true;
}
private void CreateScriptGraph()
{
Selection.activeGameObject = null;
if (CreateScriptGraphAsset())
{
shouldCloseWindow = true;
}
}
private void CreateScriptGraphOnNewGameObject()
{
Selection.activeGameObject = null;
GameObject newGameObject = new GameObject();
if (!CreateScriptGraphAsset(newGameObject, true))
{
DestroyImmediate(newGameObject);
}
}
private void CreateScriptGraphOnSelectedGameObject()
{
if (Selection.activeGameObject != null)
{
if (CreateScriptGraphAsset(Selection.activeGameObject))
{
shouldCloseWindow = true;
}
}
}
private bool CreateStateGraphAsset(GameObject gameObject = null, bool updateName = false)
{
var path = EditorUtility.SaveFilePanelInProject("Save Graph", "New State Graph", "asset", null);
if (string.IsNullOrEmpty(path))
{
return false;
}
VSUsageUtility.isVisualScriptingUsed = true;
var macro = (IMacro)CreateInstance(typeof(StateGraphAsset));
var macroObject = (UnityObject)macro;
macro.graph = StateGraph.WithStart();
if (gameObject != null)
{
StateMachine stateMachine = gameObject.AddComponent<StateMachine>();
stateMachine.nest.macro = (StateGraphAsset)macro;
}
string filename = Path.GetFileNameWithoutExtension(path);
if (updateName)
{
gameObject.name = filename;
}
macroObject.name = filename;
AssetDatabase.CreateAsset(macroObject, path);
bool shouldSetSceneAsDirty = gameObject != null;
OpenGraphFromPath(path, shouldSetSceneAsDirty);
return true;
}
private void CreateStateGraph()
{
Selection.activeGameObject = null;
if (CreateStateGraphAsset())
{
shouldCloseWindow = true;
}
}
private void CreateStateGraphOnNewGameObject()
{
Selection.activeGameObject = null;
GameObject newGameObject = new GameObject();
if (!CreateStateGraphAsset(newGameObject, true))
{
DestroyImmediate(newGameObject);
}
}
private void CreateStateGraphOnSelectedGameObject()
{
if (Selection.activeGameObject != null)
{
if (CreateStateGraphAsset(Selection.activeGameObject))
{
shouldCloseWindow = true;
}
}
}
private void CreateStyles()
{
if (buttonStyle == null)
{
buttonStyle = new GUIStyle("Button");
}
if (labelStyleButton == null)
{
labelStyleButton = new GUIStyle("Label") { alignment = TextAnchor.MiddleCenter };
}
if (labelStyleDropdownOptions == null)
{
labelStyleDropdownOptions = new GUIStyle("ToolbarButton")
{
alignment = TextAnchor.MiddleLeft,
padding = new RectOffset(20, 0, 0, 0)
};
}
if (titleStyle == null)
{
titleStyle = new GUIStyle("Label") { alignment = TextAnchor.MiddleCenter, fontSize = 20 };
}
}
private void ResetToggle()
{
if (Event.current.rawType == EventType.MouseUp)
{
if (toggleOnScript)
{
splitDropdownScriptGraph.Reset();
Repaint();
}
if (toggleOnState)
{
splitDropdownStateGraph.Reset();
Repaint();
}
}
}
private void OpenGraphFromPicker()
{
if (Event.current.commandName == "ObjectSelectorUpdated")
{
UnityObject selectedObject = EditorGUIUtility.GetObjectPickerObject();
OpenGraphAsset(selectedObject, false);
}
}
private void OnGUI()
{
CreateStyles();
ResetToggle();
DropAreaGUI();
OpenGraphFromPicker();
scrollPosition = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), scrollPosition,
scrollArea);
GUILayout.BeginVertical(GUILayout.ExpandHeight(true));
Vector2 groupSize = new Vector2(scrollArea.width, 280);
GUI.BeginGroup(new Rect((position.width / 2) - (groupSize.x / 2), (position.height / 2) - (groupSize.y / 2),
groupSize.x, groupSize.y));
GUI.Label(new Rect(0, 0, groupSize.x, titleHeight), "Drag and drop a Visual Scripting Graph asset here\nor",
titleStyle);
int buttonX = 10;
if (GUI.Button(new Rect(buttonX, titleHeight, buttonWidth, buttonHeight), "Browse to open a Graph"))
{
OpenGraph();
}
buttonX += (buttonWidth + 10);
Rect area = new Rect(buttonX, titleHeight, buttonWidth, buttonHeight);
splitDropdownScriptGraph.GetOption(0).interactable = Selection.activeGameObject != null;
if (splitDropdownScriptGraph.Draw(area, "Create new Script Graph", ref toggleOnScript))
{
CreateScriptGraph();
}
if (toggleOnScript)
{
toggleOnState = false;
}
buttonX += (buttonWidth + 10);
area = new Rect(buttonX, titleHeight, buttonWidth, buttonHeight);
splitDropdownStateGraph.GetOption(0).interactable = Selection.activeGameObject != null;
if (splitDropdownStateGraph.Draw(area, "Create new State Graph", ref toggleOnState))
{
CreateStateGraph();
}
if (toggleOnState)
{
toggleOnScript = false;
}
GUI.EndGroup();
GUILayout.EndVertical();
GUI.EndScrollView();
if (shouldCloseWindow)
{
Close();
}
}
private void DropAreaGUI()
{
Event currentEvent = Event.current;
Rect activeArea = GUILayoutUtility.GetRect(0, 0, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
switch (currentEvent.type)
{
case EventType.DragUpdated:
case EventType.DragPerform:
if (!activeArea.Contains(currentEvent.mousePosition))
{
return;
}
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (currentEvent.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
foreach (UnityObject draggedObject in DragAndDrop.objectReferences)
{
ScriptGraphAsset scriptGraphAsset = draggedObject as ScriptGraphAsset;
if (scriptGraphAsset != null)
{
shouldCloseWindow = true;
GraphWindow.OpenActive(GraphReference.New(scriptGraphAsset, true));
break;
}
}
}
break;
}
}
}
}
|