您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

138 行
5.2 KiB

using System;
using System.Reflection;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Logic/If")]
public class IfNode : CodeFunctionNode
{
public enum ComparisonOperationType
{
Equal = 0,
NotEqual,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual
}
[SerializeField]
private ComparisonOperationType m_comparisonOperation = ComparisonOperationType.Equal;
public ComparisonOperationType ComparisonOperation
{
get { return m_comparisonOperation; }
set
{
if (m_comparisonOperation == value)
return;
m_comparisonOperation = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public IfNode()
{
name = "If";
}
protected override MethodInfo GetFunctionToConvert()
{
switch (ComparisonOperation)
{
case ComparisonOperationType.Equal:
return GetType().GetMethod("Unity_IfEqual", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonOperationType.NotEqual:
return GetType().GetMethod("Unity_IfNotEqual", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonOperationType.GreaterThan:
return GetType().GetMethod("Unity_IfGreaterThan", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonOperationType.GreaterThanOrEqual:
return GetType().GetMethod("Unity_IfGreaterThanOrEqual", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonOperationType.LessThan:
return GetType().GetMethod("Unity_IfLessThan", BindingFlags.Static | BindingFlags.NonPublic);
case ComparisonOperationType.LessThanOrEqual:
return GetType().GetMethod("Unity_IfLessThanOrEqual", BindingFlags.Static | BindingFlags.NonPublic);
default:
throw new ArgumentOutOfRangeException();
}
}
const string functionTemplate = @"
{
if({comparitor})
{
result = trueValue;
}
else
{
result = falseValue;
}
}
";
static string Unity_IfEqual(
[Slot(0, Binding.None)] DynamicDimensionVector a,
[Slot(1, Binding.None)] DynamicDimensionVector b,
[Slot(2, Binding.None)] DynamicDimensionVector trueValue,
[Slot(3, Binding.None)] DynamicDimensionVector falseValue,
[Slot(4, Binding.None)] DynamicDimensionVector result )
{
return functionTemplate.Replace("{comparitor}", "a == b");
}
static string Unity_IfNotEqual(
[Slot(0, Binding.None)] DynamicDimensionVector a,
[Slot(1, Binding.None)] DynamicDimensionVector b,
[Slot(2, Binding.None)] DynamicDimensionVector trueValue,
[Slot(3, Binding.None)] DynamicDimensionVector falseValue,
[Slot(4, Binding.None)] DynamicDimensionVector result )
{
return functionTemplate.Replace("{comparitor}", "a != b");
}
static string Unity_IfGreaterThan(
[Slot(0, Binding.None)] DynamicDimensionVector a,
[Slot(1, Binding.None)] DynamicDimensionVector b,
[Slot(2, Binding.None)] DynamicDimensionVector trueValue,
[Slot(3, Binding.None)] DynamicDimensionVector falseValue,
[Slot(4, Binding.None)] DynamicDimensionVector result )
{
return functionTemplate.Replace("{comparitor}", "a > b");
}
static string Unity_IfGreaterThanOrEqual(
[Slot(0, Binding.None)] DynamicDimensionVector a,
[Slot(1, Binding.None)] DynamicDimensionVector b,
[Slot(2, Binding.None)] DynamicDimensionVector trueValue,
[Slot(3, Binding.None)] DynamicDimensionVector falseValue,
[Slot(4, Binding.None)] DynamicDimensionVector result )
{
return functionTemplate.Replace("{comparitor}", "a >= b");
}
static string Unity_IfLessThan(
[Slot(0, Binding.None)] DynamicDimensionVector a,
[Slot(1, Binding.None)] DynamicDimensionVector b,
[Slot(2, Binding.None)] DynamicDimensionVector trueValue,
[Slot(3, Binding.None)] DynamicDimensionVector falseValue,
[Slot(4, Binding.None)] DynamicDimensionVector result )
{
return functionTemplate.Replace("{comparitor}", "a < b");
}
static string Unity_IfLessThanOrEqual(
[Slot(0, Binding.None)] DynamicDimensionVector a,
[Slot(1, Binding.None)] DynamicDimensionVector b,
[Slot(2, Binding.None)] DynamicDimensionVector trueValue,
[Slot(3, Binding.None)] DynamicDimensionVector falseValue,
[Slot(4, Binding.None)] DynamicDimensionVector result )
{
return functionTemplate.Replace("{comparitor}", "a <= b");
}
}
}