public static void FixupStateRefs(TimelineStateMachine timeLineStateMachine, object obj)
            {
                if (obj != null)
                {
                    object[] nodeFieldObjects = SerializedFieldInfo.GetSerializedFieldInstances(obj);

                    foreach (object nodeFieldObject in nodeFieldObjects)
                    {
                        TimelineStateRef stateRefProperty = nodeFieldObject as TimelineStateRef;

                        if (stateRefProperty != null)
                        {
                            stateRefProperty.FixUpRef(timeLineStateMachine);
                        }
                        else
                        {
#if UNITY_EDITOR
                            LocalisedStringRef localisedstring = nodeFieldObject as LocalisedStringRef;

                            if (localisedstring != null)
                            {
                                localisedstring.SetEditorStateMachine(timeLineStateMachine);
                            }
#endif
                        }

                        FixupStateRefs(timeLineStateMachine, nodeFieldObject);
                    }
                }
            }
Exemplo n.º 2
0
            private Node[] GetNodesLinkingToNode(Node node)
            {
                //Find NodeInputFieldBase fields in node
                List <Node> nodes = new List <Node>();

                object[] nodeFieldObjects = SerializedFieldInfo.GetSerializedFieldInstances(node);

                foreach (object nodeFieldObject in nodeFieldObjects)
                {
                    if (SystemUtils.IsSubclassOfRawGeneric(typeof(NodeInputFieldBase <>), nodeFieldObject.GetType()))
                    {
                        //Check linked node
                        object nodeSourceId = SerializedFieldInfo.GetSerializedFieldInstance(nodeFieldObject, "sourceNodeId");

                        if (nodeSourceId != null)
                        {
                            Node linkedNode = GetNode((int)nodeSourceId);
                            if (linkedNode != null)
                            {
                                nodes.Add(linkedNode);
                            }
                        }
                    }
                }

                return(nodes.ToArray());
            }
 void SetTargetField(FieldInfo newField)
 {
     if (newField != null)
     {
         field = new SerializedFieldInfo(newField);
         setValue.SetType(newField.FieldType);
     }
 }
Exemplo n.º 4
0
 void SetTargetField(FieldInfo newField)
 {
     if (newField != null)
     {
         field = new SerializedFieldInfo(newField);
         checkValue.SetType(newField.FieldType);
         comparison = CompareMethod.EqualTo;
     }
 }
Exemplo n.º 5
0
            public static bool FindSerializedField(Type objType, string id, out SerializedFieldInfo field)
            {
                SerializedFieldInfo[] serializedFields = GetSerializedFields(objType);
                foreach (SerializedFieldInfo serializedField in serializedFields)
                {
                    if (serializedField.GetID() == id)
                    {
                        field = serializedField;
                        return(true);
                    }
                }

                field = new SerializedFieldInfo();
                return(false);
            }
Exemplo n.º 6
0
            public static void FixupNodeRefs(NodeGraph nodeGraph, object node)
            {
                if (node != null)
                {
                    object[] nodeFieldObjects = SerializedFieldInfo.GetSerializedFieldInstances(node);

                    foreach (object nodeFieldObject in nodeFieldObjects)
                    {
                        INodeInputField nodeField = nodeFieldObject as INodeInputField;

                        if (nodeField != null)
                        {
                            nodeField.SetParentNodeGraph(nodeGraph);
                        }

                        FixupNodeRefs(nodeGraph, nodeFieldObject);
                    }
                }
            }
Exemplo n.º 7
0
            public static void FixupGameObjectRefs(GameObject sourceObject, object node)
            {
                if (node != null)
                {
                    object[] nodeFieldObjects = SerializedFieldInfo.GetSerializedFieldInstances(node);

                    foreach (object nodeFieldObject in nodeFieldObjects)
                    {
                        GameObjectRef gameObjectRefProperty = nodeFieldObject as GameObjectRef;

                        if (gameObjectRefProperty != null)
                        {
                            gameObjectRefProperty._sourceObject = sourceObject;
                        }

                        FixupGameObjectRefs(sourceObject, nodeFieldObject);
                    }
                }
            }
Exemplo n.º 8
0
                private static bool ShouldWriteObject(Type objType, ObjectConverter converter, object obj, object defualtObj)
                {
                    //If its an array always write
                    if (objType.IsArray)
                    {
                        return(true);
                    }
                    //Never write the object if it's null (??what about if the default is non null?? write a null node??)
                    else if (obj == null)
                    {
                        return(false);
                    }
                    //If object is null by default OR is of a different then always write
                    else if (defualtObj == null || defualtObj.GetType() != obj.GetType())
                    {
                        return(true);
                    }
                    //If the object has a converter ask it if should write
                    else if (converter != null)
                    {
                        return(converter._shouldWrite(obj, defualtObj));
                    }
                    //otherwise check objects fields to see if they need to write
                    else
                    {
                        SerializedFieldInfo[] xmlFields = SerializedFieldInfo.GetSerializedFields(obj.GetType());
                        foreach (SerializedFieldInfo xmlField in xmlFields)
                        {
                            ObjectConverter fieldConverter  = GetConverter(xmlField.GetFieldType());
                            object          fieldObj        = xmlField.GetValue(obj);
                            object          defualtFieldObj = xmlField.GetValue(defualtObj);

                            if (ShouldWriteObject(xmlField.GetFieldType(), fieldConverter, fieldObj, defualtFieldObj))
                            {
                                return(true);
                            }
                        }
                    }

                    return(false);
                }
Exemplo n.º 9
0
        public void SetField(FieldInfo newField, AccessMode mode, object instance = null)
        {
            if (newField == null)
            {
                return;
            }

            newField   = newField.GetBaseDefinition();
            _field     = new SerializedFieldInfo(newField);
            accessMode = mode;
            GatherPorts();

            if (instance != null && !newField.IsStatic)
            {
                var port = (ValueInput)GetFirstInputOfType(instance.GetType());
                if (port != null)
                {
                    port.serializedValue = instance;
                }
            }
        }
Exemplo n.º 10
0
            public static                 SerializedFieldInfo[] GetSerializedFields(Type objType)
            {
                //Get all public variables that ARENT marked with NonSerialized
                //AND all non public variables that are marked with SerializeField

                //Find all fields in type
                List <SerializedFieldInfo> serializedFields = new List <SerializedFieldInfo>();

                BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy;

                //First find fields
                FieldInfo[] fields = objType.GetFields(bindingAttr);
                foreach (FieldInfo field in fields)
                {
                    if ((field.IsPublic && !field.IsNotSerialized) || (!field.IsPublic && SystemUtils.GetAttribute <SerializeField>(field) != null))
                    {
                        bool hideInEditor = SystemUtils.GetAttribute <HideInInspector>(field) != null;
                        SerializedFieldInfo serializedField = new SerializedFieldInfo(field, hideInEditor);
                        serializedFields.Add(serializedField);
                    }
                }

                //Then find all properties marked with SerializeField attribute?
                PropertyInfo[] properties = objType.GetProperties(bindingAttr);
                foreach (PropertyInfo property in properties)
                {
                    if (SystemUtils.GetAttribute <SerializeField>(property) != null)
                    {
                        bool hideInEditor = SystemUtils.GetAttribute <HideInInspector>(property) != null;
                        SerializedFieldInfo serializedField = new SerializedFieldInfo(property, hideInEditor);
                        serializedFields.Add(serializedField);
                    }
                }

                return(serializedFields.ToArray());
            }
Exemplo n.º 11
0
                private void RenderStaticValueBox(NodeEditorField nodeField, Vector2 position, Color color, float scale)
                {
                    //Get _value field from the input field and then
                    object          inputValueInstance = nodeField._fieldInfo.GetValue(nodeField._nodeEditorGUI.GetEditableObject());
                    INodeInputField inputField         = inputValueInstance as INodeInputField;

                    if (inputField.IsStaticValue())
                    {
                        object nodeValue = SerializedFieldInfo.GetSerializedFieldInstance(inputValueInstance, "value");

                        if (nodeValue != null)
                        {
                            string     labelText    = nodeValue + "  ";
                            GUIContent labelContent = new GUIContent(labelText);
                            Vector2    size         = _nodeBoldTextStyle.CalcSize(labelContent);
                            size.y  = kLinkIconWidth * scale;
                            size.x += kLinkIconWidth;

                            Rect staticFieldPos = new Rect(position.x - size.x, position.y - size.y * 0.5f, size.x, size.y);

                            Handles.BeginGUI();
                            Handles.color = color;
                            Handles.DrawSolidDisc(new Vector3(staticFieldPos.x + kLinkIconWidth * 0.25f, staticFieldPos.y + kLinkIconWidth * 0.5f * scale, 0.0f), -Vector3.forward, kLinkIconWidth * 0.5f * scale);
                            Handles.EndGUI();

                            Color origBackgroundColor = GUI.backgroundColor;
                            GUI.backgroundColor = color;

                            GUI.BeginGroup(staticFieldPos, EditorUtils.ColoredRoundedBoxStyle);
                            {
                                GUI.Label(new Rect(0, 0, staticFieldPos.width, staticFieldPos.height), labelContent, _nodeBoldTextStyle);
                            }
                            GUI.EndGroup();
                        }
                    }
                }
 public void SetEvent(FieldInfo field, object instance = null)
 {
     _field = new SerializedFieldInfo(field);
     GatherPorts();
 }
Exemplo n.º 13
0
            //The equivalent of a SerializedPropertyField but for objects serialized using Xml.
            public static T ObjectField <T>(T obj, GUIContent label, out bool dataChanged)
            {
                //If object is an array show an editable array field
                if (typeof(T).IsArray)
                {
                    bool  arrayChanged = false;
                    Array arrayObj     = obj as Array;
                    arrayObj = ArrayField(label, arrayObj, typeof(T).GetElementType(), ref arrayChanged);

                    if (arrayChanged)
                    {
                        dataChanged = true;
                        return((T)(arrayObj as object));
                    }

                    dataChanged = false;
                    return(obj);
                }

                //If the object is a ICustomEditable then just need to call its render properties function.
                if (obj != null && obj is ICustomEditorInspector)
                {
                    dataChanged = ((ICustomEditorInspector)obj).RenderObjectProperties(label);
                    return(obj);
                }

                Type objType = obj == null ? typeof(T) : obj.GetType();

                //Otherwise check the class has a object editor class associated with it.
                SerializedObjectEditorAttribute.RenderPropertiesDelegate renderPropertiesDelegate = GetEditorDelegateForObject(objType);
                if (renderPropertiesDelegate != null)
                {
                    //If it has one then just need to call its render properties function.
                    return((T)renderPropertiesDelegate(obj, label, out dataChanged));
                }

                //Otherwise loop through each xml field in object and render each as a property field
                {
                    dataChanged = false;
                    SerializedFieldInfo[] serializedFields = SerializedFieldInfo.GetSerializedFields(objType);
                    foreach (SerializedFieldInfo serializedField in serializedFields)
                    {
                        if (!serializedField.HideInEditor())
                        {
                            //Create GUIContent for label and optional tooltip
                            string           fieldName       = StringUtils.FromCamelCase(serializedField.GetID());
                            TooltipAttribute fieldToolTipAtt = SystemUtils.GetAttribute <TooltipAttribute>(serializedField);
                            GUIContent       labelContent    = fieldToolTipAtt != null ? new GUIContent(fieldName, fieldToolTipAtt.tooltip) : new GUIContent(fieldName);

                            bool   fieldChanged;
                            object nodeFieldObject = serializedField.GetValue(obj);

                            if (serializedField.GetFieldType().IsArray)
                            {
                                fieldChanged    = false;
                                nodeFieldObject = ArrayField(labelContent, nodeFieldObject as Array, serializedField.GetFieldType().GetElementType(), ref fieldChanged);
                            }
                            else
                            {
                                nodeFieldObject = ObjectField(nodeFieldObject, labelContent, out fieldChanged);
                            }

                            if (fieldChanged)
                            {
                                dataChanged = true;
                                serializedField.SetValue(obj, nodeFieldObject);
                            }
                        }
                    }

                    return(obj);
                }
            }
Exemplo n.º 14
0
                public static object FromXmlNode(Type objType, XmlNode node, object defualtObject = null)
                {
                    object obj = null;

                    //If object is an array convert each element one by one
                    if (objType.IsArray)
                    {
                        int numChildren = node != null ? node.ChildNodes.Count : 0;

                        //Create a new array from this nodes children
                        Array array = Array.CreateInstance(objType.GetElementType(), numChildren);

                        for (int i = 0; i < array.Length; i++)
                        {
                            //Convert child node
                            object elementObj = FromXmlNode(objType.GetElementType(), node.ChildNodes[i]);
                            //Then set it on the array
                            array.SetValue(elementObj, i);
                        }

                        //Then set value on member
                        obj = array;
                    }
                    else
                    {
                        //First find the actual object type from the xmlNode (could be different due to inheritance)
                        Type realObjType = GetRuntimeType(node);

                        //If the xml node type and passed in type are both generic then read type from runtime type node
                        if (NeedsRuntimeTypeInfo(objType, realObjType))
                        {
                            realObjType = ReadTypeFromRuntimeTypeInfo(node);

                            //If its still can't be found then use passed in type
                            if (realObjType == null)
                            {
                                realObjType = objType;
                            }
                        }
                        //If we don't have an xmlNode or the object type is invalid then use passed in type
                        else if (node == null || realObjType == null || realObjType.IsAbstract || realObjType.IsGenericType)
                        {
                            realObjType = objType;
                        }

                        //Convert objects fields
                        if (defualtObject != null)
                        {
                            obj = defualtObject;
                        }
                        //Create an object instance if default not passed in
                        else if (!realObjType.IsAbstract)
                        {
                            obj = CreateInstance(realObjType);
                        }

                        //If the object has an associated converter class, convert the object using it
                        ObjectConverter converter = GetConverter(realObjType);
                        if (converter != null)
                        {
                            obj = converter._onConvertFromXmlNode(obj, node);
                        }
                        //Otherwise convert fields
                        else if (node != null && obj != null)
                        {
                            SerializedFieldInfo[] serializedFields = SerializedFieldInfo.GetSerializedFields(realObjType);
                            foreach (SerializedFieldInfo serializedField in serializedFields)
                            {
                                //First try and find xml node with an id attribute matching our attribute id
                                XmlNode fieldNode = XmlUtils.FindChildWithAttributeValue(node, kXmlFieldIdAttributeTag, serializedField.GetID());

                                object fieldObj     = serializedField.GetValue(obj);
                                Type   fieldObjType = serializedField.GetFieldType();

                                //Convert the object from xml node
                                fieldObj = FromXmlNode(fieldObjType, fieldNode, fieldObj);

                                //Then set value on parent object
                                try
                                {
                                    serializedField.SetValue(obj, fieldObj);
                                }
                                catch (Exception e)
                                {
                                    throw e;
                                }
                            }
                        }
                    }

                    //IXmlConversionCallbackReceiver callback
                    if (obj is ISerializationCallbackReceiver)
                    {
                        ((ISerializationCallbackReceiver)obj).OnAfterDeserialize();
                    }

                    return(obj);
                }
Exemplo n.º 15
0
                public static XmlNode ToXmlNode <T>(T obj, XmlDocument xmlDoc, object defualtObject = null)
                {
                    XmlNode node = null;

                    if (obj != null)
                    {
                        Type            objType   = obj.GetType();
                        ObjectConverter converter = GetConverter(objType);

                        if (ShouldWriteObject(objType, converter, obj, defualtObject))
                        {
                            //IXmlConversionCallbackReceiver callback
                            if (obj is ISerializationCallbackReceiver)
                            {
                                ((ISerializationCallbackReceiver)obj).OnBeforeSerialize();
                            }

                            //If the object is an array create a node for the array and convert each element individually
                            if (objType.IsArray)
                            {
                                object[] arrayField = obj as object[];

                                if (arrayField != null && arrayField.Length > 0)
                                {
                                    XmlNode arrayXmlNode = XmlUtils.CreateXmlNode(xmlDoc, kXmlArrayTag);

                                    //Append all child nodes
                                    foreach (object arrayItem in arrayField)
                                    {
                                        XmlNode arrayElementXmlNode = ToXmlNode(arrayItem, xmlDoc);
                                        XmlUtils.SafeAppendChild(arrayXmlNode, arrayElementXmlNode);
                                    }

                                    node = arrayXmlNode;
                                }
                            }
                            else
                            {
                                string tag = GetXmlTag(objType);

                                if (!string.IsNullOrEmpty(tag))
                                {
                                    node = XmlUtils.CreateXmlNode(xmlDoc, tag);

                                    //If the object has an associated converter class, convert the object using it
                                    if (converter != null)
                                    {
                                        converter._onConvertToXmlNode(obj, node);
                                    }
                                    //Otherwise convert each field
                                    else
                                    {
                                        SerializedFieldInfo[] xmlFields = SerializedFieldInfo.GetSerializedFields(objType);

                                        //Create a default version of this to compare with?
                                        if (defualtObject == null)
                                        {
                                            defualtObject = CreateInstance(objType);
                                        }

                                        foreach (SerializedFieldInfo xmlField in xmlFields)
                                        {
                                            object fieldObj        = xmlField.GetValue(obj);
                                            object defualtFieldObj = xmlField.GetValue(defualtObject);

                                            XmlNode fieldXmlNode = ToXmlNode(fieldObj, xmlDoc, defualtFieldObj);

                                            if (fieldXmlNode != null)
                                            {
                                                AddRuntimeTypeInfoIfNecessary(xmlField.GetFieldType(), fieldObj.GetType(), fieldXmlNode, xmlDoc);
                                                XmlUtils.AddAttribute(xmlDoc, fieldXmlNode, kXmlFieldIdAttributeTag, xmlField.GetID());
                                                XmlUtils.SafeAppendChild(node, fieldXmlNode);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(node);
                }
Exemplo n.º 16
0
                public override bool RenderObjectProperties(GUIContent label)
                {
                    EditorGUI.BeginChangeCheck();
                    GetEditableObject()._editorDescription = EditorGUILayout.TextField("Editor Description", GetEditableObject()._editorDescription);
                    bool dataChanged = EditorGUI.EndChangeCheck();

                    //Render Inputs
                    bool renderedFirstInput = false;
                    {
                        foreach (NodeEditorField input in _inputNodes)
                        {
                            if (!renderedFirstInput)
                            {
                                EditorGUILayout.Separator();
                                EditorGUILayout.LabelField("Inputs", EditorStyles.boldLabel);
                                EditorGUILayout.Separator();
                                renderedFirstInput = true;
                            }

                            string           fieldName       = StringUtils.FromCamelCase(input._name);
                            TooltipAttribute fieldToolTipAtt = SystemUtils.GetAttribute <TooltipAttribute>(input._fieldInfo);
                            GUIContent       labelContent    = fieldToolTipAtt != null ? new GUIContent(fieldName, fieldToolTipAtt.tooltip) : new GUIContent(fieldName);

                            bool   fieldChanged;
                            object nodeFieldObject = input._fieldInfo.GetValue(GetEditableObject());
                            nodeFieldObject = SerializedObjectEditorGUILayout.ObjectField(nodeFieldObject, labelContent, out fieldChanged);
                            if (fieldChanged)
                            {
                                dataChanged = true;
                                input._fieldInfo.SetValue(GetEditableObject(), nodeFieldObject);
                            }
                        }
                    }

                    //Render other properties
                    bool renderedFirstProperty = false;

                    {
                        SerializedFieldInfo[] serializedFields = SerializedFieldInfo.GetSerializedFields(GetEditableObject().GetType());
                        foreach (SerializedFieldInfo serializedField in serializedFields)
                        {
                            if (!serializedField.HideInEditor() && !SystemUtils.IsSubclassOfRawGeneric(typeof(NodeInputFieldBase <>), serializedField.GetFieldType()))
                            {
                                if (!renderedFirstProperty)
                                {
                                    EditorGUILayout.Separator();
                                    EditorGUILayout.LabelField("Properties", EditorStyles.boldLabel);
                                    EditorGUILayout.Separator();
                                    renderedFirstProperty = true;
                                }

                                string           fieldName       = StringUtils.FromCamelCase(serializedField.GetID());
                                TooltipAttribute fieldToolTipAtt = SystemUtils.GetAttribute <TooltipAttribute>(serializedField);
                                GUIContent       labelContent    = fieldToolTipAtt != null ? new GUIContent(fieldName, fieldToolTipAtt.tooltip) : new GUIContent(fieldName);

                                bool   fieldChanged;
                                object nodeFieldObject = serializedField.GetValue(GetEditableObject());
                                nodeFieldObject = SerializedObjectEditorGUILayout.ObjectField(nodeFieldObject, labelContent, out fieldChanged);
                                if (fieldChanged)
                                {
                                    dataChanged = true;
                                    serializedField.SetValue(GetEditableObject(), nodeFieldObject);
                                }
                            }
                        }
                    }

                    return(dataChanged);
                }