Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

98 行
3.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine.Experimental.UIElements.StyleEnums;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine.Experimental.UIElements;
using UnityEngine;
using System.Reflection;
using System.Linq;
namespace GraphProcessor
{
[CustomPortBehavior(typeof(MultiPorts))]
public class MultiPortBehavior
{
MultiPorts multiPorts;
BaseNodeView node;
Dictionary< Edge, PortView > portViews = new Dictionary< Edge, PortView >();
FieldInfo fieldInfo;
Direction direction;
EdgeConnectorListener listener;
bool isMultiple;
string name;
public MultiPortBehavior(BaseNodeView nodeView, FieldInfo fieldInfo, Direction direction, EdgeConnectorListener listener, bool isMultiple, string name)
{
this.multiPorts = fieldInfo.GetValue(nodeView.nodeTarget) as MultiPorts;
this.node = nodeView;
this.fieldInfo = fieldInfo;
this.direction = direction;
this.listener = listener;
this.isMultiple = isMultiple;
this.name = name;
// Initialize the MultiPort field if null
if (multiPorts == null)
{
multiPorts = new MultiPorts();
fieldInfo.SetValue(nodeView.nodeTarget, multiPorts);
}
// Instantiate all ports needed to create the serialized connections
// Minus one because we count our current instance
for (int i = 0; i < multiPorts.portCount; i++)
AddPort();
}
void AddPort()
{
PortView pv = node.AddPort(fieldInfo, direction, listener, isMultiple, name);
// We force the AddPort in the BaseNode class because the port list is not updated except at the construction of the class
node.nodeTarget.AddPort(direction == Direction.Input, pv.fieldName);
pv.OnConnected += OnPortConnected;
pv.OnDisconnected += OnPortDisconnected;
}
public void OnPortConnected(PortView pv, Edge edge)
{
// Fix port datas
if (pv.direction == Direction.Input)
edge.input = pv;
else
edge.output = pv;
// If the edge is already connected, ignore it
if (portViews.ContainsKey(edge))
return ;
portViews[edge] = pv;
if (pv.GetEdges().Count == 0)
{
multiPorts.AddUniqueId(multiPorts.GetUniqueId());
AddPort();
}
}
public void OnPortDisconnected(PortView pv, Edge edge)
{
if (pv.GetEdges().Count == 0)
{
if ((edge as EdgeView).isConnected && portViews.ContainsKey(edge))
{
var portToRemove = portViews[edge];
node.RemovePort(portToRemove);
node.nodeTarget.RemovePort(direction == Direction.Input, portToRemove.fieldName);
portViews.Remove(edge);
multiPorts.RemoveUniqueId(0);
}
}
}
}
}