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.

54 lines
2.6 KiB

3 years ago
using System;
using System.Collections.Generic;
using TNode.Attribute;
using TNode.BaseViews;
using TNode.Editor.BaseViews;
3 years ago
using UnityEngine;
3 years ago
namespace TNode.Cache{
3 years ago
internal class NodeEditorSingleton{
private static NodeEditorSingleton _instance;
public readonly Dictionary<Type,Type> FromGenericToSpecific = new Dictionary<Type, Type>();
public static NodeEditorSingleton Instance{
get{ return _instance ??= new NodeEditorSingleton(); }
}
private NodeEditorSingleton(){
foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies()){
foreach(var type in assembly.GetTypes()){
if(type.IsClass && !type.IsAbstract){
foreach(var attribute in type.GetCustomAttributes(typeof(NodeComponentAttribute), false)){
//fetch this type 's parent class
var parent = type.BaseType;
//Check if this type is a generic type and is a generic type of NodeView or DataGraphView
if(parent is{IsGenericType: true} && (parent.GetGenericTypeDefinition() == typeof(NodeView<>) || parent.GetGenericTypeDefinition() == typeof(DataGraphView<>))){
//Get the generic type of this type
//Add this type to the dictionary
Debug.Log($"Find a component named {type} and its parent is {parent}" );
FromGenericToSpecific.Add(parent, type);
}
}
}
}
}
}
}
public static class NodeEditorExtensions{
public static T CreateInstance<T>(){
Debug.Log($"Create A instance of {typeof(T)}");
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[typeof(T)];
var instance = (T)Activator.CreateInstance(implementedType);
return instance;
}
public static object CreateInstance(Type t){
Debug.Log($"Create A instance of {t}");
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[t];
var instance = Activator.CreateInstance(implementedType);
return instance;
}
public static bool HasSpecificType<T>() where T : class{
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[typeof(T)] as T;
return (T)implementedType!=null;
}
3 years ago
}
}