您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
55 行
1.2 KiB
55 行
1.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace RMGUI.GraphView
|
|
{
|
|
public class GraphViewDataMapper
|
|
{
|
|
private readonly Dictionary<Type, Type> m_DataToViewDict = new Dictionary<Type, Type>();
|
|
|
|
public Type this[Type t]
|
|
{
|
|
get
|
|
{
|
|
return m_DataToViewDict[t];
|
|
}
|
|
set
|
|
{
|
|
if (!t.IsSubclassOf(typeof(GraphElementData)))
|
|
{
|
|
throw new ArgumentException("The type passed as key does not derive from UnityEngine.Object.");
|
|
}
|
|
|
|
if (!value.IsSubclassOf(typeof(GraphElement)))
|
|
{
|
|
throw new ArgumentException("The type passed as value does not derive from DataContainer.");
|
|
}
|
|
|
|
m_DataToViewDict[t] = value;
|
|
}
|
|
}
|
|
|
|
public GraphElement Create(GraphElementData data)
|
|
{
|
|
Type viewType = null;
|
|
Type dataType = data.GetType();
|
|
|
|
while (viewType == null && dataType != typeof(GraphElementData))
|
|
{
|
|
if (!m_DataToViewDict.TryGetValue(dataType, out viewType))
|
|
{
|
|
dataType = dataType.BaseType;
|
|
}
|
|
}
|
|
|
|
if (viewType == null)
|
|
{
|
|
viewType = typeof(FallbackGraphElement);
|
|
}
|
|
|
|
var dataContainer = (GraphElement)Activator.CreateInstance(viewType);
|
|
dataContainer.dataProvider = data;
|
|
return dataContainer;
|
|
}
|
|
}
|
|
}
|