fix:fix a performance issue

main
taoria 3 years ago
parent e8774a39f4
commit e5fd18ef2d
  1. 3
      TNodeCore/Attribute/GraphUsageAttribute.cs
  2. 4
      TNodeCore/DataWrapper.cs
  3. 8
      TNodeCore/Editor/NodeGraphView/IBaseDataGraphView.cs
  4. 18
      TNodeCore/Runtime/RuntimeGraph.cs
  5. 6
      TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs
  6. 2
      TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardPropertyField.cs
  7. 7
      TNodeGraphViewImpl/Editor/Inspector/NodeInspectorInNode.cs
  8. 35
      TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs
  9. 15
      TNodeGraphViewImpl/Editor/NodeViews/DragNodeView.cs
  10. 6
      TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs
  11. 2
      TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs

@ -1,6 +1,7 @@
using System;
using JetBrains.Annotations;
using TNodeCore.Models;
using TNodeCore.Runtime.Interfaces;
namespace TNodeCore.Attribute{
/// <summary>
@ -11,8 +12,8 @@ namespace TNodeCore.Attribute{
/// </example>
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
[BaseTypeRequired(typeof(IModel))]
[UsedImplicitly]
[MeansImplicitUse]
public class GraphUsageAttribute:System.Attribute{
public readonly Type GraphDataType;
public string Category;

@ -30,7 +30,9 @@ namespace TNodeCore{
fieldInfo.SetValue(data,value);
OnValueChanged?.Invoke(this);
}
public void ForceNotify(){
OnValueChanged?.Invoke(this);
}
public object GetValue(string path){
var fieldInfo = data.GetType().GetField(path);
return fieldInfo.GetValue(data);

@ -1,4 +1,5 @@
using TNodeCore.Models;
using System;
using TNodeCore.Models;
using TNodeCore.Runtime;
using UnityEngine;
@ -23,6 +24,9 @@ namespace TNodeCore.Editor.NodeGraphView{
public RuntimeGraph GetRuntimeGraph();
public void SetGraphData(GraphData graph);
void NotifyRuntimeUpdate();
public Action AfterRuntimeGraphUpdate{ get; set; }
}
}

@ -20,6 +20,16 @@ namespace TNodeCore.Runtime{
/// elements are read only ,do not modify them
/// </summary>
public readonly Dictionary<string, RuntimeNode> RuntimeNodes;
public void DirectlyTraversal(){
foreach (var node in TopologicalOrder){
var links = node.OutputLink;
foreach (var link in links){
var targetNode = RuntimeNodes[link.inPort.nodeDataId];
HandlingLink(link);
targetNode.NodeData.Process();
}
}
}
public void DependencyTraversal(RuntimeNode runtimeNode){
var links = runtimeNode.InputLink;
foreach (var link in links){
@ -118,6 +128,14 @@ namespace TNodeCore.Runtime{
_graphTool.DependencyTraversal(Get(startNode));
return true;
}
public bool ResolveDependency(){
if(!_build)
Build();
if (_graphTool == null)
return false;
_graphTool.DirectlyTraversal();
return true;
}
private void ModifyOrCreateInNode(NodeLink linkData){
var inNodeId = linkData.inPort.nodeDataId;
var inNode = graphData.NodeDictionary[inNodeId];

@ -30,11 +30,11 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{
//skip if the field is a list or Ilist
if (!typeof(IList).IsAssignableFrom(field.FieldType)){
VisualElement visualElement = new VisualElement();
var propertyField = new BlackboardField(new BlackboardProperty.BlackboardProperty(field.Name,field.FieldType));
var propertyField = new BlackboardField(new BlackboardProperty.BlackboardProperty(ObjectNames.NicifyVariableName(field.Name),field.FieldType));
var foldoutData = new Foldout{
text = field.Name
text = ObjectNames.NicifyVariableName(field.Name)
};
var drawer = new GraphBlackboardPropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name,isRuntimeGraph);
var drawer = new GraphBlackboardPropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),isRuntimeGraph);
drawer.Bind(serializedObject);
foldoutData.Add(drawer);
visualElement.Add(propertyField);

@ -6,7 +6,7 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{
public class GraphBlackboardPropertyField:PropertyField{
private readonly bool _runtime;
public GraphBlackboardPropertyField(SerializedProperty findPropertyRelative, string fieldName,bool runtime):base(findPropertyRelative, fieldName){
public GraphBlackboardPropertyField(SerializedProperty findPropertyRelative,bool runtime):base(findPropertyRelative){
_runtime = runtime;
}

@ -51,7 +51,12 @@ namespace TNode.Editor.Inspector{
var showInNodeViewAttribute = field.GetCustomAttribute<ShowInNodeViewAttribute>() != null;
if (!showInNodeViewAttribute)
continue;
var drawer = new PropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name);
var drawer = new PropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name));
drawer.RegisterValueChangeCallback((evt) => {
serializedObject.Update();
serializedObject.ApplyModifiedProperties();
((NodeDataWrapper)_data).ForceNotify();
});
drawer.Bind(serializedObject);
Add(drawer);

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using TNode.Editor.Inspector;
using TNodeCore.Editor.Blackboard;
using TNodeCore.Editor.EditorPersistence;
@ -14,6 +15,7 @@ using TNodeEditor.Editor;
using TNodeGraphViewImpl.Editor.Cache;
using TNodeGraphViewImpl.Editor.NodeViews;
using TNodeGraphViewImpl.Editor.Search;
using Unity.VisualScripting;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
@ -32,6 +34,8 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
private NodeInspector _nodeInspector;
private Dictionary<string,Node> _nodeDict = new();
private IBlackboardView _blackboard;
private bool _runtimeGraphUpdate;
public T Data{
get{ return _data; }
set{
@ -106,8 +110,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
_runtimeGraph = gameObject.GetComponent<RuntimeGraph>();
IsRuntimeGraph = true;
BuildRuntimeGraphBehaviour();
Data = gameObject.GetComponent<RuntimeGraph>().graphData as T;
if(Data==null){
Debug.LogError($"Dragged a wrong graph data to editor,expected {typeof(T)} but got {gameObject.GetComponent<RuntimeGraph>().graphData.GetType()}");
@ -143,6 +146,27 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
}
};
}
private void BuildRuntimeGraphBehaviour(){
EditorApplication.update+= UpdateRuntimeGraphBehaviour;
}
private void UpdateRuntimeGraphBehaviour(){
if(_runtimeGraph != null){
if (_runtimeGraphUpdate){
_runtimeGraph.ResolveDependency();
_runtimeGraphUpdate = false;
AfterRuntimeGraphUpdate?.Invoke();
}
}
else{
EditorApplication.update -= UpdateRuntimeGraphBehaviour;
}
}
private void CheckDataAfterInit(){
if(Data == null){
WaitingForAGraph();
@ -521,6 +545,13 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{
Data = graph as T;
}
public void NotifyRuntimeUpdate(){
_runtimeGraphUpdate = true;
}
public Action AfterRuntimeGraphUpdate{ get; set; }
#endregion
}

@ -26,19 +26,10 @@ namespace TNode.Editor.NodeViews{
var serializedData = new SerializedObject(blackboardWrapper);
var serializedProperty = serializedData.FindProperty("data").FindPropertyRelative(obj.blackDragData);
//
// field.Bind(serializedData);
// var p = label.parent.parent.parent;
// p.Add(field);
// field.SendToBack();
// field.SetEnabled(false);
// p.style.alignItems = Align.Center;
// p.style.justifyContent = Justify.Center;
// p.style.paddingTop = 0;
// p.style.paddingBottom = 0;
label.text = obj.blackDragData;
label.text = ObjectNames.NicifyVariableName(obj.blackDragData);
//Get serialized property's icon
var icon = AssetPreview.GetMiniThumbnail(serializedProperty.objectReferenceValue);
var icon = AssetPreview.GetMiniThumbnail(serializedProperty.boxedValue as UnityEngine.Object);
label.parent.Add(new Image(){
image = icon

@ -7,6 +7,7 @@ using TNodeCore.Attribute.Ports;
using TNodeCore.Editor.NodeGraphView;
using TNodeCore.Editor.Serialization;
using TNodeCore.Models;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
@ -38,6 +39,9 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
private void OnDataValueChanged(DataWrapper<NodeDataWrapper, NodeData> obj){
Refresh();
if (BaseDataGraphView.IsRuntimeGraph){
BaseDataGraphView.NotifyRuntimeUpdate();
}
}
public sealed override string title{
get => base.title;
@ -103,7 +107,7 @@ namespace TNodeGraphViewImpl.Editor.NodeViews{
if (propertyInfo.GetCustomAttributes(typeof(OutputAttribute),true).FirstOrDefault() is OutputAttribute attribute){
Port port = InstantiatePort(Orientation.Horizontal, Direction.Output,Port.Capacity.Multi,BuildPortType(attribute,propertyInfo));
this.outputContainer.Add(port);
var portName = BuildPortName(attribute,propertyInfo);
var portName = ObjectNames.NicifyVariableName(BuildPortName(attribute,propertyInfo));
port.portName = portName;
port.name = propertyInfo.Name;

@ -54,7 +54,7 @@ namespace TNodeGraphViewImpl.Editor.Search{
if (typeof(NodeData).IsAssignableFrom(type)){
//Make an instance of the type
if (NodeCreator.InstantiateNodeData(type) is { } nodeData){
nodeData.nodeName = $"New {type.Name}";
nodeData.nodeName = $"{type.Name}";
((IBaseDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100));
}
}

Loading…
Cancel
Save