private bool TrySetupNode(UdonNodeDefinition udonNodeDefinition, Vector2?position, out UdonNode node,
                                  ref UdonNodeData nodeData)
        {
            DoPropHack();
            if (!TryCreateNodeInstance(out node))
            {
                return(false);
            }
            if (nodeData == null)
            {
                nodeData = data.AddNode(udonNodeDefinition.fullName);
            }
            node.uid = nodeData.uid;
            return(true);

            void DoPropHack()
            {
                //Awful hack to fix regression in unity graph property type conversion
                {
                    FieldInfo prop = typeof(TypeConverter).GetField(
                        "useCompatibleTypeConversion",
                        BindingFlags.NonPublic | BindingFlags.Static
                        );
                    if (prop != null)
                    {
                        prop.SetValue(this, true);
                    }
                }
            }

            bool TryCreateNodeInstance(out UdonNode outNode)
            {
                outNode = CreateInstance <UdonNode>();

                outNode.name     = udonNodeDefinition.fullName;
                outNode.title    = PrettyString(udonNodeDefinition.name).FriendlyNameify();
                outNode.position = position == null ? new Rect(Vector2.zero, Vector2.zero) : new Rect(position.Value, Vector2.zero);
                string nodeName = outNode.name;

                if (nodeName.StartsWith("Event_") &&
                    (nodeName != "Event_Custom" || graphProgramAsset.GetType() == typeof(UdonSubGraphAsset)))
                {
                    if (nodes.Any(n => n.name == nodeName))
                    {
                        Debug.LogWarning(
                            $"Can't create more than one {nodeName} node, try managing your flow with a Block node instead!");
                        return(false);
                    }
                }

                if (nodeName.StartsWith("Event_") &&
                    (nodeName != "Event_Custom" && graphProgramAsset.GetType() == typeof(UdonSubGraphAsset)))
                {
                    Debug.LogWarning($"SubGraphs can't use built-in events, pipe in your event from the parent graph instead!");
                    return(false);
                }

                if (outNode.title == "Const_VRCUdonCommonInterfacesIUdonEventReceiver")
                {
                    outNode.title = "UdonBehaviour";
                }

                return(true);
            }
        }
Beispiel #2
0
        public void CreateNode(UdonNodeDefinition udonNodeDefinition, Vector2?position = null)
        {
            //Awful hack to fix regression in unity graph property type conversion
            {
                var prop = typeof(TypeConverter).GetField(
                    "useCompatibleTypeConversion",
                    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static
                    );
                prop.SetValue(this, true);
            }

            UdonNode node = CreateInstance <UdonNode>();

            node.name     = udonNodeDefinition.fullName;
            node.title    = PrettyString(udonNodeDefinition.name).FriendlyNameify();
            node.position = position == null ? new Rect(Vector2.zero, Vector2.zero) : new Rect(position.Value, Vector2.zero);

            if (node.name.StartsWith("Event_") && node.name != "Event_Custom")
            {
                if (nodes.Any(n => n.name == node.name))
                {
                    Debug.LogWarning($"Can't create more than one {node.name} node, try managing your flow with a Block node instead!");
                    return;
                }
            }

            if (node.title == "Const_VRCUdonCommonInterfacesIUdonEventReceiver")
            {
                node.title = "UdonBehaviour";
            }

            UdonNodeData nodeData = data.AddNode(udonNodeDefinition.fullName);

            if (udonNodeDefinition.flow)
            {
                if (!udonNodeDefinition.fullName.StartsWith("Event_"))
                {
                    node.AddInputSlot("");
                }

                node.AddOutputSlot("");
                if (_specialFlows.Contains(udonNodeDefinition.fullName))
                {
                    node.AddOutputSlot("");
                }
            }

            foreach (Type input in udonNodeDefinition.inputs)
            {
                node.AddInputSlot(FriendlyTypeName(input), SlotTypeConverter(input, udonNodeDefinition.fullName));
            }

            foreach (Type output in udonNodeDefinition.outputs)
            {
                node.AddOutputSlot(FriendlyTypeName(output), SlotTypeConverter(output, udonNodeDefinition.fullName));
            }


            if (udonNodeDefinition.defaultValues != null)
            {
                nodeData.nodeValues = new SerializableObjectContainer[udonNodeDefinition.defaultValues.Length];
                nodeData.nodeUIDs   = new string[udonNodeDefinition.defaultValues.Length];
                for (int i = 0; i < udonNodeDefinition.defaultValues.Length; i++)
                {
                    object defaultValue = udonNodeDefinition.defaultValues[i];
                    if (defaultValue != null)
                    {
                        nodeData.nodeValues[i] = SerializableObjectContainer.Serialize(defaultValue);
                    }
                }
            }

            node.uid = nodeData.uid;

            ReSerializeData();

            AddNode(node);
        }