2.when create a new editor ,Generated node data now will be referenced correctlymain
parent
3f8daf5be9
commit
6a4e8150f2
16 changed files with 288 additions and 0 deletions
@ -0,0 +1,7 @@ |
||||
fileFormatVersion: 2 |
||||
guid: 82765577abc7c5346892518f8e696f79 |
||||
DefaultImporter: |
||||
externalObjects: {} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,8 @@ |
||||
fileFormatVersion: 2 |
||||
guid: 18f38c39496afae47ab40fb512c3ce7c |
||||
folderAsset: yes |
||||
DefaultImporter: |
||||
externalObjects: {} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,8 @@ |
||||
fileFormatVersion: 2 |
||||
guid: dc673adb081e1f841bdf4998bdb7b50a |
||||
folderAsset: yes |
||||
DefaultImporter: |
||||
externalObjects: {} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,27 @@ |
||||
%YAML 1.1 |
||||
%TAG !u! tag:unity3d.com,2011: |
||||
--- !u!114 &11400000 |
||||
MonoBehaviour: |
||||
m_ObjectHideFlags: 0 |
||||
m_CorrespondingSourceObject: {fileID: 0} |
||||
m_PrefabInstance: {fileID: 0} |
||||
m_PrefabAsset: {fileID: 0} |
||||
m_GameObject: {fileID: 0} |
||||
m_Enabled: 1 |
||||
m_EditorHideFlags: 0 |
||||
m_Script: {fileID: 11500000, guid: cde084f079a7426daa86ed86cb80ed1b, type: 3} |
||||
m_Name: HelloWorld |
||||
m_EditorClassIdentifier: |
||||
nodeData: |
||||
rid: -2 |
||||
nodePos: |
||||
serializedVersion: 2 |
||||
x: 0 |
||||
y: 0 |
||||
width: 0 |
||||
height: 0 |
||||
references: |
||||
version: 2 |
||||
RefIds: |
||||
- rid: -2 |
||||
type: {class: , ns: , asm: } |
@ -0,0 +1,8 @@ |
||||
fileFormatVersion: 2 |
||||
guid: ad64ccb31efdc9c4082380856cd58efc |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 11400000 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,10 @@ |
||||
using TNode.Editor; |
||||
using UnityEditor; |
||||
using UnityEditor.Callbacks; |
||||
using UnityEditor.Experimental.GraphView; |
||||
using UnityEngine; |
||||
using UnityEngine.UIElements; |
||||
|
||||
public class HelloWorld : GraphEditor<HelloWorldGraph>{ |
||||
|
||||
} |
@ -0,0 +1,12 @@ |
||||
fileFormatVersion: 2 |
||||
guid: 7b39119946f2f83458e3c2bafb200552 |
||||
MonoImporter: |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
defaultReferences: |
||||
- nodeEditorData: {fileID: 11400000, guid: ad64ccb31efdc9c4082380856cd58efc, type: 2} |
||||
executionOrder: 0 |
||||
icon: {instanceID: 0} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,4 @@ |
||||
using TNode.Models; |
||||
public class HelloWorldGraph : GraphData{ |
||||
|
||||
} |
@ -0,0 +1,11 @@ |
||||
fileFormatVersion: 2 |
||||
guid: 8b641f4bc28454e4aa2c5ddc629b07c2 |
||||
MonoImporter: |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
defaultReferences: [] |
||||
executionOrder: 0 |
||||
icon: {instanceID: 0} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,137 @@ |
||||
using System.IO; |
||||
using TNode.Editor.Model; |
||||
using UnityEditor; |
||||
using UnityEngine; |
||||
using UnityEngine.UIElements; |
||||
|
||||
//add an attribute right click asset panel and select "TNode/Create/Create New Graph Editor" to call this editor |
||||
|
||||
|
||||
namespace TNode.Editor.Tools.GraphEditorCreator{ |
||||
|
||||
public class GraphEditorCreator : EditorWindow |
||||
{ |
||||
[SerializeField] |
||||
private VisualTreeAsset m_VisualTreeAsset = default; |
||||
|
||||
[MenuItem("Assets/Create/TNode/Create New Graph Editor")] |
||||
[MenuItem("TNode/Create New Graph Editor")] |
||||
public static void ShowExample() |
||||
{ |
||||
GraphEditorCreator wnd = GetWindow<GraphEditorCreator>(); |
||||
wnd.titleContent = new GUIContent("GraphEditorCreator"); |
||||
//Set position to the center of the screen |
||||
wnd.position = new(Screen.width / 2, Screen.height / 2, 500, 300); |
||||
//set this window non resizable |
||||
wnd.minSize = new Vector2(500, 300); |
||||
wnd.maxSize = new Vector2(500, 300); |
||||
|
||||
} |
||||
|
||||
public void CreateGUI() |
||||
{ |
||||
// Each editor window contains a root VisualElement object |
||||
VisualElement root = rootVisualElement; |
||||
|
||||
// VisualElements objects can contain other VisualElement following a tree hierarchy. |
||||
VisualElement label = new Label("Hello World! From C#"); |
||||
root.Add(label); |
||||
|
||||
// Instantiate UXML |
||||
VisualElement labelFromUXML = m_VisualTreeAsset.Instantiate(); |
||||
root.Add(labelFromUXML); |
||||
|
||||
//Register a callback when Create Button is clicked |
||||
Button createButton = root.Q<Button>("CreateButton"); |
||||
createButton.clickable.clicked += OnCreateButtonClicked; |
||||
|
||||
} |
||||
|
||||
private void OnCreateButtonClicked(){ |
||||
//Create a new .cs file at current opened asset folder |
||||
string path = AssetDatabase.GetAssetPath(Selection.activeObject); |
||||
if (path == "") |
||||
{ |
||||
path = "Assets"; |
||||
} |
||||
else if (Path.GetExtension(path) != "") |
||||
{ |
||||
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), ""); |
||||
} |
||||
//if the path is not named with editor, create a new folder called editor at the path |
||||
if (!path.EndsWith("Editor")) |
||||
{ |
||||
AssetDatabase.CreateFolder(path, "Editor"); |
||||
path = path + "/Editor"; |
||||
} |
||||
//Query the name of the graph editor |
||||
string editorName = rootVisualElement.Q<TextField>("EditorClassNameTextField").text; |
||||
string graphName = rootVisualElement.Q<TextField>("GraphClassNameTextField").text; |
||||
if (editorName == "") |
||||
{ |
||||
editorName = "NewGraphEditor"; |
||||
} |
||||
|
||||
SourceGeneratorForGraphEditor sourceGeneratorForGraphEditor = new SourceGeneratorForGraphEditor(); |
||||
|
||||
var source = sourceGeneratorForGraphEditor.GenerateGraphEditor(editorName,graphName); |
||||
|
||||
var sourceGraph = sourceGeneratorForGraphEditor.GenerateGraph(graphName); |
||||
string editorPath = Path.Combine(path, editorName + ".cs"); |
||||
string graphPath = Path.Combine(path, graphName + ".cs"); |
||||
File.WriteAllText(editorPath, source); |
||||
|
||||
File.WriteAllText(graphPath, sourceGraph); |
||||
|
||||
|
||||
|
||||
//Refresh the AssetDatabase to import the new file |
||||
|
||||
AssetDatabase.Refresh(); |
||||
|
||||
//Wait for the new file to be imported |
||||
while (!AssetDatabase.LoadAssetAtPath<MonoScript>(editorPath)) |
||||
{ |
||||
EditorUtility.DisplayProgressBar("Generating Graph Editor", "Please wait while the new graph editor is being imported", 0.5f); |
||||
EditorApplication.update(); |
||||
} |
||||
//Create an Node Editor Data Instance for the new graph editor |
||||
NodeEditorData nodeEditorData = ScriptableObject.CreateInstance<NodeEditorData>(); |
||||
nodeEditorData.name = editorName; |
||||
|
||||
EditorUtility.SetDirty(nodeEditorData); |
||||
|
||||
//Save it at the same folder as the new graph editor |
||||
string nodeEditorDataPath = Path.Combine(path, editorName + ".asset"); |
||||
AssetDatabase.CreateAsset(nodeEditorData, nodeEditorDataPath); |
||||
AssetDatabase.SaveAssets(); |
||||
AssetDatabase.Refresh(); |
||||
//Wait for the new file to be imported |
||||
while (!AssetDatabase.LoadAssetAtPath<NodeEditorData>(nodeEditorDataPath)) |
||||
{ |
||||
EditorUtility.DisplayProgressBar("Generating Graph Editor", "Please wait while the new graph editor is being imported", 0.5f); |
||||
EditorApplication.update(); |
||||
} |
||||
var script = AssetDatabase.LoadAssetAtPath<MonoScript>(editorPath); |
||||
|
||||
|
||||
//Set the mono importer to the current graph editor script |
||||
MonoImporter monoImporter = AssetImporter.GetAtPath(editorPath) as MonoImporter; |
||||
|
||||
if (monoImporter != null) |
||||
monoImporter.SetDefaultReferences(new string[]{"nodeEditorData"}, new Object[]{nodeEditorData}); |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//Refresh the asset ann close it |
||||
//Mark it dirty |
||||
|
||||
AssetDatabase.SaveAssets(); |
||||
AssetDatabase.Refresh(); |
||||
Close(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
fileFormatVersion: 2 |
||||
guid: ace2b72120aa22947a4ce38ee3f538f6 |
||||
MonoImporter: |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
defaultReferences: |
||||
- m_VisualTreeAsset: {fileID: 9197481963319205126, guid: 26bedf756b2e7ee4fa660657461ab3dc, type: 3} |
||||
executionOrder: 0 |
||||
icon: {instanceID: 0} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,5 @@ |
||||
.custom-label { |
||||
font-size: 20px; |
||||
-unity-font-style: bold; |
||||
color: rgb(68, 138, 255); |
||||
} |
@ -0,0 +1,11 @@ |
||||
fileFormatVersion: 2 |
||||
guid: 7cf956c2afedd274db0f9b45392e90f5 |
||||
ScriptedImporter: |
||||
internalIDToNameTable: [] |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
||||
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0} |
||||
disableValidation: 0 |
@ -0,0 +1,14 @@ |
||||
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True"> |
||||
<Style src="project://database/Assets/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.uss?fileID=7433441132597879392&guid=7cf956c2afedd274db0f9b45392e90f5&type=3#GraphEditorCreator" /> |
||||
<ui:VisualElement name="Root"> |
||||
<ui:VisualElement name="EditorName" style="flex-direction: row;"> |
||||
<ui:TextField picking-mode="Ignore" label="Your Editor Class Name" value="filler text" text="filler text" name="EditorClassNameTextField" style="width: 306px;" /> |
||||
<ui:Label tabindex="-1" text=".cs" display-tooltip-when-elided="true" style="-unity-text-align: middle-left;" /> |
||||
</ui:VisualElement> |
||||
<ui:VisualElement name="GraphName" style="flex-direction: row;"> |
||||
<ui:TextField picking-mode="Ignore" label="Your Editor Class Name" value="filler text" text="filler text" name="GraphClassNameTextField" style="width: 306px;" /> |
||||
<ui:Label tabindex="-1" text=".cs" display-tooltip-when-elided="true" style="-unity-text-align: middle-left;" /> |
||||
</ui:VisualElement> |
||||
<ui:Button tabindex="-1" text="Create Editor" display-tooltip-when-elided="true" name="CreateButton" /> |
||||
</ui:VisualElement> |
||||
</ui:UXML> |
@ -0,0 +1,10 @@ |
||||
fileFormatVersion: 2 |
||||
guid: 26bedf756b2e7ee4fa660657461ab3dc |
||||
ScriptedImporter: |
||||
internalIDToNameTable: [] |
||||
externalObjects: {} |
||||
serializedVersion: 2 |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
||||
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0} |
Loading…
Reference in new issue