Unity graph tool solution based on different implementation now focused on Unity.Experimental.Graphview
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

using System;
using System.Collections.Generic;
using System.Reflection;
using JetBrains.Annotations;
using TNode.Models;
using Unity.VisualScripting;
using UnityEngine.UI;
namespace TNode.RuntimeCache{
public class RuntimeCache{
//Singleton instance for the runtime cache
private static RuntimeCache _instance;
public static RuntimeCache Instance{
get{ return _instance ??= new RuntimeCache(); }
}
//delegate return a value from a nodedata
public delegate object GetValueDelegate(NodeData nodeData);
public readonly Dictionary<Type, List<GetValueDelegate>> CachedDelegatesForGettingValue =
new ();
public void ExecuteOutput<T>(T nodeData) where T:NodeData{
var type = typeof(T);
if(!CachedDelegatesForGettingValue.ContainsKey(type)){
return;
}
var delegates = CachedDelegatesForGettingValue[type];
foreach(var delegateInstance in delegates){
var value = delegateInstance(nodeData);
}
}
public void RegisterRuntimeNode<T>() where T:NodeData{
var type = typeof(T);
if(!CachedDelegatesForGettingValue.ContainsKey(type)){
CachedDelegatesForGettingValue.Add(type, new List<GetValueDelegate>());
var properties = type.GetProperties();
foreach(var property in properties){
var getValueDelegate = GetValueDelegateForProperty(property);
CachedDelegatesForGettingValue[type].Add(getValueDelegate);
}
}
else{
//Cache already exists for this type
}
}
private GetValueDelegate GetValueDelegateForProperty(PropertyInfo property){
var getValueDelegate = (GetValueDelegate)Delegate.CreateDelegate(typeof(GetValueDelegate), property.GetGetMethod());
return getValueDelegate;
}
}
}