Peter Bay Bastian
8 年前
当前提交
ab990b62
共有 8 个文件被更改,包括 151 次插入 和 16 次删除
-
25MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphInspector.cs
-
21MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphInspector.cs
-
24MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/ScriptableObjectFactory.cs
-
12MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/ScriptableObjectFactory.cs.meta
-
45MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs
-
12MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapper.cs.meta
-
16MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapping.cs
-
12MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Util/TypeMapping.cs.meta
|
|||
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.AddMapping(typeMapping); |
|||
} |
|||
|
|||
public TTo Create(TFrom from) |
|||
{ |
|||
var toType = m_TypeMapper.MapType(from.GetType()); |
|||
return ScriptableObject.CreateInstance(toType) as TTo; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 889e2994de7c9164eaf187757dee6bee |
|||
timeCreated: 1485164640 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
namespace UnityEditor.Graphing.Util |
|||
{ |
|||
public class TypeMapper<TFrom, TTo, TFallback> |
|||
where TFallback : TTo |
|||
{ |
|||
private readonly Dictionary<Type, Type> m_Mappings = new Dictionary<Type, Type>(); |
|||
|
|||
public void AddMapping(TypeMapping mapping) |
|||
{ |
|||
AddMapping(mapping.fromType, mapping.toType); |
|||
} |
|||
|
|||
public void AddMapping(Type fromType, Type toType) |
|||
{ |
|||
if (!fromType.IsSubclassOf(typeof(TFrom)) && !fromType.GetInterfaces().Contains(typeof(TFrom))) |
|||
{ |
|||
throw new ArgumentException(string.Format("{0} does not implement or derive from {1}.", fromType.Name, typeof(TFrom).Name), "fromType"); |
|||
} |
|||
|
|||
if (!toType.IsSubclassOf(typeof(TTo))) |
|||
{ |
|||
throw new ArgumentException(string.Format("{0} does not derive from {1}.", toType.Name, typeof(TTo).Name), "toType"); |
|||
} |
|||
|
|||
m_Mappings[fromType] = toType; |
|||
} |
|||
|
|||
public Type MapType(Type fromType) |
|||
{ |
|||
Type toType = null; |
|||
|
|||
while (toType == null && fromType != null && fromType != typeof(TFrom)) |
|||
{ |
|||
if (!m_Mappings.TryGetValue(fromType, out toType)) |
|||
fromType = fromType.BaseType; |
|||
} |
|||
|
|||
return toType ?? typeof(TFallback); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f9c0c57b328abb94982f26ec960996d2 |
|||
timeCreated: 1485161458 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
|
|||
namespace UnityEditor.Graphing.Util |
|||
{ |
|||
public class TypeMapping |
|||
{ |
|||
public Type fromType { get; private set; } |
|||
public Type toType { get; private set; } |
|||
|
|||
public TypeMapping(Type fromType, Type toType) |
|||
{ |
|||
this.fromType = fromType; |
|||
this.toType = toType; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 53ba6b433697da6468377c205573f728 |
|||
timeCreated: 1485164640 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue