Ejemplo n.º 1
0
        // Use this for initialization
        protected override void Init()
        {
            base.Init();

            // Init data containers dictionary
            if (m_PortsPerFieldInfo == null)
            {
                m_PortsPerFieldInfo = new NodePortFieldInfoDictionary();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates the ports fields displayed in the editor
        /// </summary>
        internal void UpdatePortFields(MonoBehaviour gameComponent, bool overrideScript = false)
        {
            if (gameComponent == null)
            {
                return;
            }

            if (overrideScript)
            {
                // Update the script present in the node (although this might return null if it is a clone and gets destroyed?)
                SetScript(gameComponent);
            }

            // Gets all fields information from the game component (using System.Reflection)
            FieldInfo[] serializedFields = m_ScriptInternal.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);

            // Go through all the fields
            for (int i = 0; i < serializedFields.Length; i++)
            {
                var fieldToUse = serializedFields[i];

                // Check if the field is marked with the "SendToIMLController" attribute
                SendToIMLController dataForIMLController = Attribute.GetCustomAttribute(fieldToUse, typeof(SendToIMLController)) as SendToIMLController;
                // We check now if the field is marked with the "PullFromIMLController" attribute
                PullFromIMLController dataFromIMLController = Attribute.GetCustomAttribute(fieldToUse, typeof(PullFromIMLController)) as PullFromIMLController;
                // Define flags to identify attribute behaviour
                bool isInputData = false, isOutputData = false;
                // Update flags
                if (dataForIMLController != null)
                {
                    isInputData = true;
                }
                if (dataFromIMLController != null)
                {
                    isOutputData = true;
                }

                // If the field is marked as either input or output...
                if (isInputData || isOutputData)
                {
                    // Debug type of that value in console
                    //Debug.Log(fieldToUse.Name + " Used in IMLComponent, With Type: " + fieldToUse.FieldType + ", With Value: " + fieldToUse.GetValue(gameComponent).ToString());

                    // Make sure that the dictionaries are initialised
                    if (m_PortsPerFieldInfo == null)
                    {
                        m_PortsPerFieldInfo = new NodePortFieldInfoDictionary();
                    }

                    // Check if the dictionary DOESN'T contain a fieldInfo for this reflected value, and then create nodes and dictionary values
                    if (!m_PortsPerFieldInfo.ContainsValue(fieldToUse))
                    {
                        // Secondly, we create a port (based on its type) for this fieldInfo and add it to the node
                        NodePort newPort = null;
                        // Add port to node
                        if (isInputData)
                        {
                            newPort = AddDynamicOutput(fieldToUse.FieldType, fieldName: fieldToUse.Name);
                        }
                        else if (isOutputData)
                        {
                            newPort = AddDynamicInput(fieldToUse.FieldType, fieldName: fieldToUse.Name);
                        }

                        // Add that to the dictionary
                        m_PortsPerFieldInfo.Add(newPort, fieldToUse);
                    }
                    // If the dictionary already contains a fieldInfo (and it is output), update it
                    else if (isOutputData)
                    {
                        // Get the port linked to that field info
                        var port = m_PortsPerFieldInfo.GetKey(fieldToUse);

                        FieldInfo field;
                        m_PortsPerFieldInfo.TryGetValue(port, out field);


                        // Set value by reflection
                        field.SetValue(m_ScriptInternal, port.GetInputValue());
                    }
                }
            }
        }