浏览代码

Remove ScriptableObjectFactory and make fallback type optional in TypeMapper

/main
Peter Bay Bastian 7 年前
当前提交
f6a7d3b4
共有 4 个文件被更改,包括 18 次插入47 次删除
  1. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Presenters/GraphInspectorPresenter.cs
  2. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/TypeMapper.cs
  3. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/ScriptableObjectFactory.cs.meta
  4. 24
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/ScriptableObjectFactory.cs

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Presenters/GraphInspectorPresenter.cs


set { m_Title = value; }
}
ScriptableObjectFactory<INode, AbstractNodeInspector, BasicNodeInspector> m_InspectorFactory;
TypeMapper<INode, AbstractNodeInspector> m_InspectorMapper;
public void Initialize(string graphName)
{

m_InspectorFactory = new ScriptableObjectFactory<INode, AbstractNodeInspector, BasicNodeInspector>(new[]
m_InspectorMapper = new TypeMapper<INode, AbstractNodeInspector>(typeof(BasicNodeInspector))
new TypeMapping(typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeInspector)),
new TypeMapping(typeof(PropertyNode), typeof(PropertyNodeInspector)),
new TypeMapping(typeof(SubGraphInputNode), typeof(SubgraphInputNodeInspector)),
new TypeMapping(typeof(SubGraphOutputNode), typeof(SubgraphOutputNodeInspector))
});
{typeof(AbstractSurfaceMasterNode), typeof(SurfaceMasterNodeInspector)},
{typeof(PropertyNode), typeof(PropertyNodeInspector)},
{typeof(SubGraphInputNode), typeof(SubgraphInputNodeInspector)},
{typeof(SubGraphOutputNode), typeof(SubgraphOutputNodeInspector)}
};
}
public void UpdateSelection(IEnumerable<INode> nodes)

foreach (var node in nodes.OfType<SerializableNode>())
{
var inspector = m_InspectorFactory.Create(node);
var inspector = (AbstractNodeInspector) CreateInstance(m_InspectorMapper.MapType(node.GetType()));
inspector.Initialize(node);
m_Inspectors.Add(inspector);

13
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/TypeMapper.cs


namespace UnityEditor.Graphing.Util
{
public class TypeMapper<TFrom, TTo, TFallback> : IEnumerable<TypeMapping>
where TFallback : TTo
public class TypeMapper<TFrom, TTo> : IEnumerable<TypeMapping>
readonly Type m_FallbackType;
public TypeMapper(Type fallbackType = null)
{
if (fallbackType != null && !(fallbackType.IsSubclassOf(typeof(TFrom)) || fallbackType.GetInterfaces().Contains(typeof(TFrom))))
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fallbackType.Name, typeof(TFrom).Name), "fallbackType");
m_FallbackType = fallbackType;
}
public void Add(TypeMapping mapping)
{

fromType = fromType.BaseType;
}
return toType ?? typeof(TFallback);
return toType ?? m_FallbackType;
}
public IEnumerator<TypeMapping> GetEnumerator()

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/ScriptableObjectFactory.cs.meta


fileFormatVersion: 2
guid: 889e2994de7c9164eaf187757dee6bee
timeCreated: 1485164640
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

24
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Util/ScriptableObjectFactory.cs


using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.Graphing.Util
{
public class ScriptableObjectFactory<TFrom, TTo, TFallback>
where TTo : ScriptableObject
where TFallback : TTo
{
private readonly TypeMapper<TFrom, TTo, TFallback> m_TypeMapper = new TypeMapper<TFrom, TTo, TFallback>();
public ScriptableObjectFactory(IEnumerable<TypeMapping> typeMappings)
{
foreach (var typeMapping in typeMappings)
m_TypeMapper.Add(typeMapping);
}
public TTo Create(TFrom from)
{
var toType = m_TypeMapper.MapType(from.GetType());
return ScriptableObject.CreateInstance(toType) as TTo;
}
}
}
正在加载...
取消
保存