浏览代码

Extracted type mapping functionality into own class such that it can be re-used for node inspector

/main
Peter Bay Bastian 8 年前
当前提交
8dd129c5
共有 5 个文件被更改,包括 74 次插入36 次删除
  1. 26
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphDataSource.cs
  2. 5
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/SerializedGraphDataSource.cs
  3. 27
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/DrawData/MaterialGraphDataSource.cs
  4. 40
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs
  5. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs.meta

26
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphDataSource.cs


using System.Collections.Generic;
using System.Linq;
using RMGUI.GraphView;
using UnityEditor.Graphing.Util;
using UnityEngine;
using UnityEngine.Graphing;

[SerializeField]
private List<GraphElementData> m_TempElements = new List<GraphElementData>();
private readonly Dictionary<Type, Type> m_DataMapper = new Dictionary<Type, Type>();
private readonly TypeMapper m_DataMapper = new TypeMapper(typeof(NodeDrawData));
public IGraphAsset graphAsset { get; private set; }

if (m_Elements.OfType<NodeDrawData>().Any(e => e.node == node))
continue;
var type = MapType(node.GetType());
var type = m_DataMapper.MapType(node.GetType());
var nodeData = (NodeDrawData)CreateInstance(type);
node.onModified += OnNodeChanged;

m_Elements.AddRange(drawableEdges.OfType<GraphElementData>());
}
private Type MapType(Type type)
{
Type found = null;
while (type != null)
{
if (m_DataMapper.TryGetValue(type, out found))
break;
type = type.BaseType;
}
return found ?? typeof(NodeDrawData);
}
protected abstract void AddTypeMappings();
public void AddTypeMapping(Type node, Type drawData)
{
m_DataMapper[node] = drawData;
}
protected abstract void AddTypeMappings(Action<Type, Type> map);
AddTypeMappings();
AddTypeMappings(m_DataMapper.AddMapping);
this.graphAsset = graphAsset;

5
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/SerializedGraphDataSource.cs


using System;
using UnityEngine.Graphing;
namespace UnityEditor.Graphing.Drawing

protected SerializedGraphDataSource()
{}
protected override void AddTypeMappings()
protected override void AddTypeMappings(Action<Type, Type> map)
AddTypeMapping(typeof(SerializableNode), typeof(NodeDrawData));
map(typeof(SerializableNode), typeof(NodeDrawData));
}
}
}

27
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/DrawData/MaterialGraphDataSource.cs


using System;
using UnityEditor.Graphing.Drawing;
using UnityEngine.MaterialGraph;
using RMGUI.GraphView;

protected MaterialGraphDataSource()
{}
protected override void AddTypeMappings()
protected override void AddTypeMappings(Action<Type, Type> map)
AddTypeMapping(typeof(AbstractMaterialNode), typeof(MaterialNodeDrawData));
AddTypeMapping(typeof(ColorNode), typeof(ColorNodeDrawData));
AddTypeMapping(typeof(TextureNode), typeof(TextureNodeDrawData));
AddTypeMapping(typeof(Vector1Node), typeof(Vector1NodeDrawData));
AddTypeMapping(typeof(Vector2Node), typeof(Vector2NodeDrawData));
AddTypeMapping(typeof(Vector3Node), typeof(Vector3NodeDrawData));
AddTypeMapping(typeof(Vector4Node), typeof(Vector4NodeDrawData));
AddTypeMapping(typeof(SubGraphNode), typeof(SubgraphNodeDrawData));
AddTypeMapping(typeof(RemapMasterNode), typeof(RemapMasterNodeDrawData));
AddTypeMapping(typeof(MasterRemapInputNode), typeof(RemapInputNodeDrawData));
AddTypeMapping(typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterDrawData));
AddTypeMapping(typeof(EdgeDrawData), typeof(Edge));
map(typeof(AbstractMaterialNode), typeof(MaterialNodeDrawData));
map(typeof(ColorNode), typeof(ColorNodeDrawData));
map(typeof(TextureNode), typeof(TextureNodeDrawData));
map(typeof(Vector1Node), typeof(Vector1NodeDrawData));
map(typeof(Vector2Node), typeof(Vector2NodeDrawData));
map(typeof(Vector3Node), typeof(Vector3NodeDrawData));
map(typeof(Vector4Node), typeof(Vector4NodeDrawData));
map(typeof(SubGraphNode), typeof(SubgraphNodeDrawData));
map(typeof(RemapMasterNode), typeof(RemapMasterNodeDrawData));
map(typeof(MasterRemapInputNode), typeof(RemapInputNodeDrawData));
map(typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterDrawData));
map(typeof(EdgeDrawData), typeof(Edge));
}
}
}

40
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs


using System;
using System.Collections.Generic;
namespace UnityEditor.Graphing.Util
{
public class TypeMapper
{
private readonly Dictionary<Type, Type> m_Mappings = new Dictionary<Type, Type>();
private readonly Type m_Default;
public TypeMapper() : this(null) {}
public TypeMapper(Type defaultType)
{
m_Default = defaultType;
}
public void Clear()
{
m_Mappings.Clear();
}
public void AddMapping(Type from, Type to)
{
m_Mappings[from] = to;
}
public Type MapType(Type type)
{
Type found = null;
while (type != null)
{
if (m_Mappings.TryGetValue(type, out found))
break;
type = type.BaseType;
}
return found ?? m_Default;
}
}
}

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs.meta


fileFormatVersion: 2
guid: efaba3c359b73408c9888998f9a4c87d
timeCreated: 1482407708
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存