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.

70 lines
2.6 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using TNode.Editor.BaseViews;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
namespace TNode.Editor{
public class BlackboardSearchWindowProvider:ISearchWindowProvider{
private Type _graphType;
private IDataGraphView _graphView;
private EditorWindow _editor;
private struct InternalSearchTreeUserData{
public IList List;
public Type Type;
}
public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context){
var blackboardData = _graphView.GetBlackboardData();
var type = blackboardData.GetType();
var entries = new List<SearchTreeEntry>();
if (entries == null) throw new ArgumentNullException(nameof(entries));
//search fields with List type
Texture2D icon = new Texture2D(2,2);
foreach (var field in type.GetFields()){
if (field.FieldType.IsGenericType){
var genericType = field.FieldType.GetGenericTypeDefinition();
if (genericType == typeof(List<>)){
entries.Add(new SearchTreeEntry(new GUIContent(field.Name,icon)){
level = 1,
userData = new InternalSearchTreeUserData(){
List = field.GetValue(blackboardData) as IList,
Type = field.FieldType.GetGenericArguments()[0]
}
});
}
}
}
return entries;
}
public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context){
var userData = SearchTreeEntry.userData;
var relativePos = context.screenMousePosition - _editor.position.position;
var blackboardData = _graphView.GetBlackboardData();
if (userData is InternalSearchTreeUserData){
var list = ((InternalSearchTreeUserData) userData).List;
var type = ((InternalSearchTreeUserData) userData).Type;
var newItem = Activator.CreateInstance(type);
list.Add(newItem);
return true;
}
return false;
}
public void Setup(Type graph,IDataGraphView graphView,EditorWindow editor){
_graphType = graph;
_graphView = graphView;
_editor = editor;
}
3 years ago
}
3 years ago
}