public void PasteNode(Vector3 position) { if (nodeToCopy == null || nodeToCopy.Count == 0) { return; } if (uNodeEditorUtility.IsPrefab(editorData.graph.gameObject)) { throw new Exception("Editing graph prefab dirrectly is not supported."); } uNodeEditorUtility.RegisterUndo(editorData.graph, "Paste Node"); uNodeRoot UNR = editorData.graph; float progress = 0; int loopIndex = 0; if (nodeToCopy.Count > 5) { EditorUtility.DisplayProgressBar("Loading", "", progress); } Vector2 center = Vector2.zero; int centerLength = 0; Dictionary <uNodeComponent, uNodeComponent> CopyedObjectMap = new Dictionary <uNodeComponent, uNodeComponent>(EqualityComparer <uNodeComponent> .Default); foreach (uNodeComponent comp in nodeToCopy) { if (comp == null || comp is EventNode && editorData.selectedGroup != null) { continue; } Node node = comp as Node; if (!CopyedObjectMap.ContainsKey(comp)) { uNodeComponent com = Object.Instantiate(comp); com.gameObject.name = com.gameObject.name.Replace("(Clone)", ""); if (editorData.selectedGroup == null) { if (editorData.selectedRoot) { com.transform.parent = editorData.selectedRoot.transform; } else { com.transform.parent = NodeEditorUtility.GetNodeRoot(UNR).transform; } } else { com.transform.parent = editorData.selectedGroup.transform; } int index = 0; string nm = com.gameObject.name.TrimEnd(numberChar); while (true) { bool flag = false; string gName = com.gameObject.name; foreach (Transform t in com.transform.parent) { if (t != com.transform) { if (t.gameObject.name.Equals(gName)) { flag = true; break; } } } if (flag) { com.gameObject.name = nm + index.ToString(); index++; continue; } break; } CopyedObjectMap.Add(comp, com); if (comp is IMacro || comp is ISuperNode) { var fields = EditorReflectionUtility.GetFields(comp.GetType()); foreach (var field in fields) { if (field.FieldType == typeof(List <Nodes.MacroPortNode>)) { var value = field.GetValueOptimized(comp) as List <Nodes.MacroPortNode>; if (value != null) { var sourceValue = field.GetValueOptimized(com) as List <Nodes.MacroPortNode>; for (int i = 0; i < value.Count; i++) { if (value[i] == null) { continue; } CopyedObjectMap.Add(value[i], sourceValue[i]); } } } } } } if (node != null) { center.x += node.editorRect.x; center.y += node.editorRect.y; centerLength++; } else { BaseEventNode met = comp as BaseEventNode; if (met != null) { center.x += met.editorRect.x; center.y += met.editorRect.y; centerLength++; } } loopIndex++; progress = (float)loopIndex / (float)nodeToCopy.Count; if (nodeToCopy.Count > 5) { EditorUtility.DisplayProgressBar("Loading", "", progress); } } progress = 0; center /= centerLength; HashSet <uNodeComponent> needReflectedComponent = new HashSet <uNodeComponent>(); uNodeRoot compEvent = null; foreach (uNodeComponent com in nodeToCopy) { uNodeComponent comp = null; if (CopyedObjectMap.ContainsKey(com)) { comp = CopyedObjectMap[com]; if (comp == null) { loopIndex++; progress = (float)loopIndex / (float)nodeToCopy.Count; if (nodeToCopy.Count > 5) { EditorUtility.DisplayProgressBar("Loading", "", progress); } continue; } if (comp as Node) { Node node = comp as Node; Func <object, bool> validation = delegate(object o) { if (o is MemberData) { MemberData member = o as MemberData; if (member.IsTargetingPinOrNode) { NodeComponent n = member.GetInstance() as NodeComponent; if (n && n is uNodeComponent) { if (CopyedObjectMap.ContainsKey(n)) { member.instance = CopyedObjectMap[n] as NodeComponent; n.owner = UNR; return(true); } else if (n.owner != UNR || n.transform.parent != node.transform.parent) { member.instance = null; n.owner = UNR; return(true); } //return true; } } } return(false); }; if (node as StateNode) { StateNode eventNode = node as StateNode; TransitionEvent[] TE = eventNode.GetTransitions(); foreach (TransitionEvent n in TE) { var tn = n.GetTargetNode(); if (tn == null) { continue; } if (CopyedObjectMap.ContainsKey(tn)) { n.target = MemberData.CreateConnection(CopyedObjectMap[tn] as Node, true); n.owner = UNR; } else if (n.owner != UNR || tn != null && tn.owner != UNR || tn != null && tn.transform.parent != node.transform.parent) { n.target = MemberData.none; n.owner = UNR; } } } else if (node is IMacro || node is ISuperNode) { var fields = EditorReflectionUtility.GetFields(comp.GetType()); foreach (var field in fields) { if (field.FieldType == typeof(List <Nodes.MacroPortNode>)) { var value = field.GetValueOptimized(comp) as List <Nodes.MacroPortNode>; if (value != null) { foreach (var v in value) { AnalizerUtility.AnalizeObject(v, validation); } } } } } AnalizerUtility.AnalizeObject(node, validation); node.editorRect = new Rect(node.editorRect.x - center.x + position.x, node.editorRect.y - center.y + position.y, node.editorRect.width, node.editorRect.height); if (node.owner != UNR) { node.owner = UNR; } } else if (comp is BaseEventNode) { BaseEventNode method = comp as BaseEventNode; var flows = method.GetFlows(); for (int i = 0; i < flows.Count; i++) { var tn = flows[i].GetTargetNode(); if (tn != null && CopyedObjectMap.ContainsKey(tn)) { flows[i] = new MemberData(CopyedObjectMap[flows[i].GetTargetNode()], MemberData.TargetType.FlowNode); } else if (method.owner != UNR) { flows[i] = MemberData.none; } } method.owner = UNR; method.editorRect = new Rect(method.editorRect.x - center.x + position.x, method.editorRect.y - center.y + position.y, method.editorRect.width, method.editorRect.height); } } loopIndex++; progress = (float)loopIndex / (float)nodeToCopy.Count; if (nodeToCopy.Count > 5) { EditorUtility.DisplayProgressBar("Loading", "", progress); } } if (nodeToCopy.Count > 5) { EditorUtility.ClearProgressBar(); } if (needReflectedComponent.Count > 0) { NodeEditorUtility.PerformReflectComponent(needReflectedComponent.ToList(), compEvent, UNR); } foreach (KeyValuePair <uNodeComponent, uNodeComponent> keys in CopyedObjectMap) { if (keys.Value != null) { Undo.RegisterCreatedObjectUndo(keys.Value.gameObject, "Paste Node"); } } //allCopyedEvent.Clear(); Refresh(); }
protected override void InitializeView() { HLNode node = targetNode as HLNode; node.Refresh(); var instance = node.instance; if (instance == null) { return; } if (instance is IStateNode || instance is IStateCoroutineNode) { AddOutputFlowPort( new PortData() { portID = nameof(node.onSuccess), onValueChanged = (o) => { RegisterUndo(); node.onSuccess = o as MemberData; }, getPortName = () => "Success", getPortType = () => typeof(FlowInput), getPortValue = () => node.onSuccess, getPortTooltip = () => "Flow to execute on success", } ); AddOutputFlowPort( new PortData() { portID = nameof(node.onFailure), onValueChanged = (o) => { RegisterUndo(); node.onFailure = o as MemberData; }, getPortName = () => "Failure", getPortType = () => typeof(FlowInput), getPortValue = () => node.onFailure, getPortTooltip = () => "Flow to execute on failure", } ); } if (node.IsFlowNode()) //Flow input { nodeFlowPort = AddInputFlowPort( new PortData() { portID = UGraphView.SelfPortID, getPortName = () => "", getConnection = () => { return(new MemberData(node, MemberData.TargetType.FlowNode)); }, } ); AddOutputFlowPort( new PortData() { portID = "onFinished", onValueChanged = (o) => { RegisterUndo(); node.onFinished = o as MemberData; }, getPortName = () => instance is IStateNode || instance is IStateCoroutineNode ? "Finished" : "", getPortType = () => typeof(FlowInput), getPortValue = () => node.onFinished, } ); } if (node.CanGetValue() || node.CanSetValue()) //Value output { nodeValuePort = AddOutputValuePort( new PortData() { portID = UGraphView.SelfPortID, getPortName = () => "Out", getPortType = () => node.ReturnType(), getPortValue = () => node, getConnection = () => { return(new MemberData(node, MemberData.TargetType.ValueNode)); }, } ); } Type type = instance.GetType(); var fields = EditorReflectionUtility.GetFields(type); foreach (var field in fields) { if (field.IsDefined(typeof(NonSerializedAttribute)) || field.IsDefined(typeof(HideAttribute))) { continue; } var option = field.GetCustomAttribute(typeof(NodePortAttribute), true) as NodePortAttribute; if (option != null && option.hideInNode) { continue; } var val = node.initializers.FirstOrDefault(d => d.name == field.Name); if (val == null) { val = new FieldValueData() { name = field.Name, value = MemberData.CreateFromValue(field.GetValue(instance), field.FieldType), }; node.initializers.Add(val); } AddInputValuePort( new PortData() { portID = field.Name, onValueChanged = (o) => { RegisterUndo(); val.value = o as MemberData; }, getPortName = () => option != null ? option.name : field.Name, getPortType = () => field.FieldType, getPortValue = () => val.value, getPortTooltip = () => { var tooltip = field.GetCustomAttribute(typeof(TooltipAttribute), true) as TooltipAttribute; return(tooltip != null ? tooltip.tooltip : string.Empty); }, }); } }
void InitializeView() { if (data.block == null) { return; } if (data.block is Events.HLAction || data.block is Events.HLCondition) { InitializeHLBlock(data.block); return; } var fields = EditorReflectionUtility.GetFields(data.block.GetType()); if (fields != null && fields.Length > 0) { for (int idx = 0; idx < fields.Length; idx++) { FieldInfo field = fields[idx]; if (uNodeGUIUtility.IsHide(field, data.block)) { continue; } Type type = field.FieldType; if (type == typeof(MemberData)) { MemberData member = field.GetValueOptimized(data.block) as MemberData; object[] fieldAttribute = field.GetCustomAttributes(true); if (!ReflectionUtils.TryCorrectingAttribute(data.block, ref fieldAttribute)) { continue; } var filter = ReflectionUtils.GetAttribute <FilterAttribute>(fieldAttribute); if (member == null) { if (filter != null && !filter.SetMember && !filter.OnlyGetType && ReflectionUtils.CanCreateInstance(filter.GetActualType())) { member = new MemberData(ReflectionUtils.CreateInstance(filter.GetActualType())); } else if (filter != null && !filter.SetMember && !filter.OnlyGetType) { member = MemberData.none; } field.SetValueOptimized(data.block, member); Repaint(); } bool hasDependencies = EditorReflectionUtility.HasFieldDependencies(new string[] { field.Name }, fields); var port = AddPort(new PortData() { getPortName = () => ObjectNames.NicifyVariableName(field.Name), getPortType = () => filter?.GetActualType() ?? typeof(object), getPortValue = () => field.GetValueOptimized(data.block), filter = filter, onValueChanged = (obj) => { RegisterUndo(); member = obj as MemberData; field.SetValueOptimized(data.block, member); Repaint(); if (hasDependencies) { owner.nodeView.MarkRepaint(); } }, }); port.Add(new ControlView(port.portData.InstantiateControl(true), true)); } else if (type == typeof(MultipurposeMember)) { InitMultipurposeMember(field); } else { object fieldVal = field.GetValueOptimized(data.block); object[] fieldAttribute = field.GetCustomAttributes(true); if (!ReflectionUtils.TryCorrectingAttribute(data.block, ref fieldAttribute)) { continue; } bool hasDependencies = EditorReflectionUtility.HasFieldDependencies(new string[] { field.Name }, fields); var filter = ReflectionUtils.GetAttribute <FilterAttribute>(fieldAttribute); ControlConfig config = new ControlConfig() { owner = owner.nodeView, value = fieldVal, type = type, filter = filter, onValueChanged = (obj) => { RegisterUndo(); field.SetValueOptimized(data.block, obj); Repaint(); if (hasDependencies) { owner.nodeView.MarkRepaint(); } }, }; var valueControl = UIElementUtility.CreateControl(type, config, true); AddControl(field.Name, new ControlView(valueControl, true)); if (fieldVal is IList <MemberData> ) { IList <MemberData> members = fieldVal as IList <MemberData>; if (members != null) { for (int i = 0; i < members.Count; i++) { int index = i; var member = members[i]; if (member == null) { if (filter != null && !filter.SetMember && !filter.OnlyGetType && ReflectionUtils.CanCreateInstance(filter.GetActualType())) { member = new MemberData(ReflectionUtils.CreateInstance(filter.GetActualType())); } else { member = MemberData.none; } members[index] = member; field.SetValueOptimized(data.block, members); Repaint(); } var port = AddPort(new PortData() { getPortName = () => "Element " + index.ToString(), getPortType = () => filter?.GetActualType() ?? typeof(object), getPortValue = () => (field.GetValueOptimized(data.block) as IList <MemberData>)[index], filter = filter, onValueChanged = (obj) => { RegisterUndo(); member = obj as MemberData; members[index] = member; field.SetValueOptimized(data.block, members); Repaint(); }, }); port.Add(new ControlView(port.portData.InstantiateControl(true), true)); } } } else if (fieldVal is IList) { IList list = fieldVal as IList; if (list != null) { for (int i = 0; i < list.Count; i++) { int index = i; var element = list[i]; ControlConfig cfg = new ControlConfig() { owner = owner.nodeView, value = fieldVal, type = type, filter = filter, onValueChanged = (obj) => { RegisterUndo(); field.SetValueOptimized(data.block, obj); Repaint(); }, }; var elementControl = UIElementUtility.CreateControl(type, cfg, true); AddControl("Element " + index, new ControlView(elementControl, true)); } } } } } } }
public virtual void AddChildNodes(NodeComponent nodeComponent, TreeViewItem parentTree, IList <TreeViewItem> rows) { var fields = EditorReflectionUtility.GetFields(nodeComponent.GetType()); foreach (var field in fields) { var fieldType = field.FieldType; if (field.IsDefinedAttribute(typeof(FieldConnectionAttribute))) { var attributes = EditorReflectionUtility.GetAttributes(field); var FCA = ReflectionUtils.GetAttribute <FieldConnectionAttribute>(attributes); bool isFlow = nodeComponent is Node && (nodeComponent as Node).IsFlowNode(); if (FCA.hideOnFlowNode && isFlow) { continue; } if (FCA.hideOnNotFlowNode && !isFlow) { continue; } if (fieldType == typeof(MemberData)) { if (FCA.isFlowConnection) { MemberData member = field.GetValueOptimized(nodeComponent) as MemberData; if (member != null) { if (!FCA.isFinishedFlow && field.Name != nameof(MultipurposeNode.onFinished)) { if (FCA.displayFlowInHierarchy) { var flowItem = CreateFlowTree( nodeComponent, field.Name, member, FCA.label != null ? FCA.label.text : field.Name ); parentTree.AddChild(flowItem); rows.Add(flowItem); manager.AddNodeTree(member, flowItem, rows); } else { manager.AddNodeTree(member, parentTree, rows); } } else { manager.AddNodeTree(member, parentTree, rows, false); } } } } else if (fieldType == typeof(List <MemberData>) && FCA.isFlowConnection) { List <MemberData> members = field.GetValueOptimized(nodeComponent) as List <MemberData>; if (members != null && members.Count > 0) { foreach (var member in members) { if (member != null && member.isAssigned && member.IsTargetingPinOrNode) { Node n = member.GetTargetNode(); if (n != null) { manager.AddNodes(n, parentTree, rows, isChildren: !FCA.isFinishedFlow && field.Name != nameof(MultipurposeNode.onFinished)); } } } } } } else if (fieldType == typeof(EventData)) { var blocks = field.GetValueOptimized(nodeComponent) as EventData; AddBlocks(blocks, field.Name, parentTree, rows); } } }
void InitializeHLBlock(Block block) { if (block is Events.HLAction action) { Type type = action.type.startType; if (type != null) { var fields = EditorReflectionUtility.GetFields(type); foreach (var field in fields) { if (field.IsDefined(typeof(NonSerializedAttribute)) || field.IsDefined(typeof(HideAttribute))) { continue; } var option = field.GetCustomAttribute(typeof(NodePortAttribute), true) as NodePortAttribute; if (option != null && option.hideInNode) { continue; } var val = action.initializers.FirstOrDefault(d => d.name == field.Name); if (val == null) { val = new FieldValueData() { name = field.Name, value = MemberData.CreateFromValue(field.GetValueOptimized(ReflectionUtils.CreateInstance(type)), field.FieldType), }; action.initializers.Add(val); } var port = AddPort(new PortData() { portID = field.Name, getPortName = () => option != null ? option.name : field.Name, getPortType = () => field.FieldType, getPortValue = () => val.value, onValueChanged = (obj) => { RegisterUndo(); val.value = obj as MemberData; Repaint(); }, getPortTooltip = () => { var tooltip = field.GetCustomAttribute(typeof(TooltipAttribute), true) as TooltipAttribute; return(tooltip != null ? tooltip.tooltip : string.Empty); }, }); port.Add(new ControlView(port.portData.InstantiateControl(true), true)); } } } else if (block is Events.HLCondition condition) { Type type = condition.type.startType; if (type != null) { var fields = EditorReflectionUtility.GetFields(type); foreach (var field in fields) { if (field.IsDefined(typeof(NonSerializedAttribute)) || field.IsDefined(typeof(HideAttribute))) { continue; } var option = field.GetCustomAttribute(typeof(NodePortAttribute), true) as NodePortAttribute; if (option != null && option.hideInNode) { continue; } var val = condition.initializers.FirstOrDefault(d => d.name == field.Name); if (val == null) { val = new FieldValueData() { name = field.Name, value = MemberData.CreateFromValue(field.GetValueOptimized(ReflectionUtils.CreateInstance(type)), field.FieldType), }; condition.initializers.Add(val); } var port = AddPort(new PortData() { portID = field.Name, getPortName = () => option != null ? option.name : field.Name, getPortType = () => field.FieldType, getPortValue = () => val.value, onValueChanged = (obj) => { RegisterUndo(); val.value = obj as MemberData; Repaint(); }, getPortTooltip = () => { var tooltip = field.GetCustomAttribute(typeof(TooltipAttribute), true) as TooltipAttribute; return(tooltip != null ? tooltip.tooltip : string.Empty); }, }); port.Add(new ControlView(port.portData.InstantiateControl(true), true)); } } } }