Example #1
0
        void BuildForm()
        {
#if UNITY_EDITOR
            name = string.Format("Form({0})", item.GetType().Name);
#endif

            formInfos = item.GetType().GetCustomAttribute <FormInfosAttribute>();

            if (title != null && !string.IsNullOrEmpty(titleFormat))
            {
                title.text = string.Format(titleFormat, formInfos != null ? formInfos.title : "", item.GetType().Name, mode);
            }

            if (description != null && !string.IsNullOrEmpty(descriptionFormat))
            {
                description.text = string.Format(descriptionFormat, formInfos != null ? formInfos.description : "", item.GetType().Name, mode);
            }

            foreach (FieldInfo fieldInfo in item.GetType().GetRuntimeFields())
            {
                FormFieldAttribute attribute = fieldInfo.GetCustomAttribute <FormFieldAttribute>();
                if (attribute != null)
                {
                    if (!attribute.onlyVisibleIn.HasFlag(mode))
                    {
                        continue;
                    }
                    FormField field = Instantiate(theme.GetFieldFromAttribute(attribute), fieldsContainer);
                    field.Configure(fieldInfo, attribute, fieldInfo.GetValue(item));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Returns the linked <see cref="FormField"/> from a given <see cref="FormFieldAttribute"/>
        /// </summary>
        /// <param name="attribute">The linked <see cref="FormFieldAttribute"/></param>
        public FormField GetFieldFromAttribute(FormFieldAttribute attribute)
        {
            if (attribute.formFieldType == typeof(IntField))
            {
                return(intField);
            }
            else if (attribute.formFieldType == typeof(StringField))
            {
                return(stringField);
            }
            else if (attribute.formFieldType == typeof(FloatField))
            {
                return(floatField);
            }
            else if (attribute.formFieldType == typeof(BoolField))
            {
                return(boolField);
            }

            else
            {
                foreach (FormField field in customFields)
                {
                    if (field.GetType() == attribute.formFieldType)
                    {
                        return(field);
                    }
                }
            }


            throw new Exception(string.Format("No {0} field (from attribute {1}) found in {2} Theme", attribute.formFieldType, attribute.GetType(), name));
        }
        /// <summary>
        /// This methods is used by the <see cref="Form"/> to configure the <see cref="FormField"/>
        /// </summary>
        /// <param name="fieldInfo">The <see cref="FieldInfo"/> of the field</param>
        /// <param name="attribute">The linked <see cref="FormFieldAttribute"/>, should have the same type thant the one defined in <see cref="FormField.attributeType"/></param>
        /// <param name="value">The field value of the item</param>
        public virtual void Configure(FieldInfo fieldInfo, FormFieldAttribute attribute, object value)
        {
#if UNITY_EDITOR
            name = string.Format("{0}_{1}_field", fieldInfo.Name, fieldInfo.FieldType.Name);
#endif
            this.fieldInfo = fieldInfo;
            this.attribute = attribute;

            if (label != null)
            {
                label.text = (attribute.label != null ? attribute.label : fieldInfo.Name) + (attribute.required ? " *" : "");
            }
        }