Exemplo n.º 1
0
        private static void GlobalAssert()
        {
            StackTraceLogType stackTraceLogType = Application.GetStackTraceLogType(LogType.Error);

            Application.SetStackTraceLogType(LogType.Error, StackTraceLogType.None);

            MonoBehaviour[] allMonoOnActiveScene = Resources.FindObjectsOfTypeAll <MonoBehaviour>();
            foreach (MonoBehaviour monoBehaviour in allMonoOnActiveScene)
            {
                if (Attribute.GetCustomAttribute(monoBehaviour.GetType(), typeof(Assert)) as Assert != null)
                {
                    SerializedObject serializedObject = new SerializedObject(monoBehaviour);
                    foreach (FieldInfo field in monoBehaviour.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                    {
                        if (field.FieldType.IsClass)
                        {
                            SerializeField atr = Attribute.GetCustomAttribute(field, typeof(SerializeField)) as SerializeField;
                            if (atr != null)
                            {
                                if (serializedObject.FindProperty(field.Name).propertyType == SerializedPropertyType.ObjectReference &&
                                    serializedObject.FindProperty(field.Name).objectReferenceValue == null)
                                {
                                    Debug.LogErrorFormat(monoBehaviour.gameObject, monoBehaviour.gameObject.name + "." + field.Name + ", typeof " + field.FieldType.Name + " is null");
                                }
                            }
                        }
                    }
                }
            }
            Application.SetStackTraceLogType(LogType.Error, stackTraceLogType);
        }
Exemplo n.º 2
0
        protected override void ConstructFieldTemplates(NodeProvider nodeProvider, Dictionary <Type, NodeFieldTemplate> templates)
        {
            NodeLibrary library = nodeProvider.GetNodeLibrary();

            foreach (var nodeTemplate in library.nodeTemplates)
            {
                Type type     = nodeTemplate.RuntimeNodeType;
                var  fields   = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                var  template = new NodeFieldTemplate();

                var supressInput = type.GetCustomAttribute <SupressInputAttribute>();

                // All tasks (nodes in this case) need to have an input, that isnt in the code of the behaviours
                // so we just add it in the first thing we do.
                if (supressInput == null)
                {
                    template.InputPorts.Add(new PortDescription("Input", typeof(Task), PortDirection.Input, false, false));
                }

                foreach (var field in fields)
                {
                    OutputAttribute output   = field.GetCustomAttribute <OutputAttribute>();
                    InputAttribute  input    = field.GetCustomAttribute <InputAttribute>();
                    SerializeField  property = field.GetCustomAttribute <SerializeField>();

                    bool autoAdd  = false;
                    bool isInput  = input == null ? false : true;
                    bool isOutput = output == null ? false : true;

                    if (output != null)
                    {
                        autoAdd = output.AutoAddPortOnConnect;
                    }
                    if (input != null)
                    {
                        autoAdd = input.AutoAddPortOnConnect;
                    }

                    bool isList = IsListType(field);

                    if (output != null || input != null)
                    {
                        if (IsListType(field))
                        {
                            AddGenericPort(template, field, isInput, isOutput, autoAdd);
                        }
                        else
                        {
                            AddNonGenericPort(template, field.FieldType, field.Name, field.Name, isInput, isOutput, autoAdd);
                        }
                    }
                    else if (property != null)
                    {
                        AddNonGenericProperty(template, field, field.Name, field.Name);
                    }
                }
                templates.Add(type, template);
            }
        }
Exemplo n.º 3
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            //Cast target to WeatherManager and get all fields
            WeatherManager weatherSystem = (WeatherManager)target;

            FieldInfo[] objectFields = typeof(WeatherManager).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            ///Organise fields by attriubte
            List <FieldInfo> proceduralFields = new List <FieldInfo>();
            List <FieldInfo> manualFields     = new List <FieldInfo>();
            List <FieldInfo> serializedFields = new List <FieldInfo>();

            objectFields.ToList().ForEach((field) =>
            {
                ProceduralAttribute proceduralAttribute = (ProceduralAttribute)Attribute.GetCustomAttribute(field, typeof(ProceduralAttribute));
                if (proceduralAttribute != null)
                {
                    proceduralFields.Add(field);
                    return;
                }

                ManualAttribute manualAttribute = (ManualAttribute)Attribute.GetCustomAttribute(field, typeof(ManualAttribute));
                if (manualAttribute != null)
                {
                    manualFields.Add(field);
                    return;
                }

                SerializeField serializeAttribute = (SerializeField)Attribute.GetCustomAttribute(field, typeof(SerializeField));
                if (serializeAttribute != null)
                {
                    serializedFields.Add(field);
                    return;
                }
            });
            objectFields = null;

            //First, draw all the non-specific fields
            //Do we need this? The returns in the .ForEach loop above must mean we don't?
            List <string> nonDefaultFields = new List <string>();

            nonDefaultFields.AddRange(manualFields.Select(field =>
            {
                return(field.Name);
            }));
            nonDefaultFields.AddRange(proceduralFields.Select(field =>
            {
                return(field.Name);
            }));
            DrawPropertiesExcluding(serializedObject, nonDefaultFields.ToArray());

            //Now draw the elements relevent to weatherSystem.procedural value
            //The procedural variables if selected and manual ones otherwise
            weatherSystem.procedural = (WeatherMode)SwitchElements(weatherSystem.procedural, "Procedural Mode?", new List <List <FieldInfo> > {
                proceduralFields, manualFields
            });
            serializedObject.ApplyModifiedProperties();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates inspector field for most basic c# type and returns TRUE if inspector value was changed.
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        /// Accepted types:
        /// byte
        /// sbyte
        /// short
        /// ushort
        /// int
        /// uint
        /// long
        /// float
        /// double
        /// bool
        /// string
        protected bool MakeInspectorField(string label, ref object value,
                                          System.Type containingObjectType = null, bool privateMember = false)
        {
            // Returns true if target property or field has the [SerializeField] attribute.
            bool HasSerializeField()
            {
                BindingFlags accessFlag = privateMember ? BindingFlags.NonPublic : BindingFlags.Public;

                if (containingObjectType != null)
                {
                    MemberInfo[] allMembers  = containingObjectType.GetMembers();
                    MemberInfo[] memberInfos = containingObjectType.GetMember(label, BindingFlags.Instance | accessFlag);
                    foreach (MemberInfo memberInfo in memberInfos)
                    {
                        SerializeField serializeFieldAttribute =
                            (memberInfo.GetCustomAttribute <SerializeField>(false));
                        if (serializeFieldAttribute != null)
                        {
                            // Debug.Log("Serialize field attribute found.");
                            return(true);
                        }
                    }
                    return(false);
                }
                return(true);
            }

            System.Type type = value?.GetType();
            if (!HasSerializeField())
            {
                return(false);
            }

            if (type == typeof(byte))
            {
                byte oldValue = (byte)value;
                byte newValue = ByteField(label, (byte)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(sbyte))
            {
                sbyte oldValue = (sbyte)value;
                sbyte newValue = SByteField(label, (sbyte)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(short))
            {
                short oldValue = (short)value;
                short newValue = ShortField(label, (short)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(ushort))
            {
                ushort oldValue = (ushort)value;
                ushort newValue = UShortField(label, (ushort)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(int))
            {
                int oldValue = (int)value;
                int newValue = EditorGUILayout.IntField(label, (int)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(uint))
            {
                uint oldValue = (ushort)value;
                uint newValue = UIntField(label, (uint)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(long))
            {
                long oldValue = (long)value;
                long newValue = EditorGUILayout.LongField(label, (long)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(float))
            {
                float oldValue = (float)value;
                float newValue = EditorGUILayout.FloatField(label, (float)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(double))
            {
                double oldValue = (double)value;
                double newValue = EditorGUILayout.DoubleField(label, (double)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(bool))
            {
                bool oldValue = (bool)value;
                bool newValue = EditorGUILayout.Toggle(label, (bool)value);
                value = newValue;
                return(oldValue != newValue);
            }
            if (type == typeof(string))
            {
                string oldValue = (string)value;
                string newValue = EditorGUILayout.TextField(label, (string)value);
                value = newValue;
                return(oldValue != newValue);
            }
            return(false);
        }