浏览代码

Add drag and drop to graph for Texture2D and subgraphs

/main
Peter Bay Bastian 7 年前
当前提交
81d2a867
共有 3 个文件被更改,包括 99 次插入0 次删除
  1. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  2. 95
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs
  3. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs.meta

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


m_GraphView.AddManipulator(new SelectionDragger());
m_GraphView.AddManipulator(new ClickSelector());
m_GraphView.AddManipulator(new NodeCreator(graph));
m_GraphView.AddManipulator(new GraphDropTarget(graph));
content.Add(m_GraphView);
m_GraphInspectorView = new GraphInspectorView(asset.name, previewManager, graph) { name = "inspector" };

95
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs


using System.Linq;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing
{
public class GraphDropTarget : Manipulator
{
AbstractMaterialGraph m_Graph;
MaterialGraphView m_GraphView;
public GraphDropTarget(AbstractMaterialGraph graph)
{
m_Graph = graph;
}
protected override void RegisterCallbacksOnTarget()
{
m_GraphView = target as MaterialGraphView;
target.RegisterCallback<IMGUIEvent>(OnIMGUIEvent);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback<IMGUIEvent>(OnIMGUIEvent);
}
bool ValidateObject(Object obj)
{
return EditorUtility.IsPersistent(obj)
&& (obj is Texture2D || obj is MaterialSubGraphAsset);
}
void CreateNode(Object obj, Vector2 nodePosition)
{
var texture2D = obj as Texture2D;
if (texture2D != null)
{
m_Graph.owner.RegisterCompleteObjectUndo("Drag Texture");
var property = new TextureShaderProperty { displayName = texture2D.name, value = { texture = texture2D } };
m_Graph.AddShaderProperty(property);
var node = new PropertyNode();
var drawState = node.drawState;
drawState.position = new Rect(nodePosition, drawState.position.size);
node.drawState = drawState;
m_Graph.AddNode(node);
node.propertyGuid = property.guid;
}
var subGraphAsset = obj as MaterialSubGraphAsset;
if (subGraphAsset != null)
{
var node = new SubGraphNode();
var drawState = node.drawState;
drawState.position = new Rect(nodePosition, drawState.position.size);
node.drawState = drawState;
node.subGraphAsset = subGraphAsset;
m_Graph.AddNode(node);
}
}
void OnIMGUIEvent(IMGUIEvent evt)
{
if (evt.imguiEvent.type == EventType.DragUpdated || evt.imguiEvent.type == EventType.DragPerform)
{
var currentTarget = evt.currentTarget as VisualElement;
if (currentTarget == null)
return;
var objects = DragAndDrop.objectReferences;
Object draggedObject = null;
foreach (var obj in objects)
{
Debug.Log(obj.GetType().Name);
if (ValidateObject(obj))
{
draggedObject = obj;
break;
}
}
if (draggedObject == null)
return;
// Debug.LogFormat("{0}: {1}", draggedObject.GetType().Name, draggedObject.name);
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if (evt.imguiEvent.type == EventType.DragPerform)
{
var nodePosition = m_GraphView.contentViewContainer.transform.matrix.inverse.MultiplyPoint3x4(m_GraphView.panel.visualTree.ChangeCoordinatesTo(m_GraphView, Event.current.mousePosition));
CreateNode(draggedObject, nodePosition);
DragAndDrop.AcceptDrag();
}
}
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs.meta


fileFormatVersion: 2
guid: 9fd73fb7c6eb4a04978545358d5b55d5
timeCreated: 1509089285
正在加载...
取消
保存