Exemplo n.º 1
0
        public static string DefaultToEditorContent(this System.Object textEditable)
        {
            var objectType         = textEditable.GetType();
            var valueMap           = new Dictionary <string, object>();
            var defaultStringField = textEditable.DefaultStringField();

            foreach (var field in objectType.GetFields(flags))
            {
                if (FieldValid(field) == false)
                {
                    continue;
                }
                valueMap[field.Name] = field.GetValue(textEditable);
            }

            var    results        = "";
            string default_string = null;

            foreach (var field in objectType.GetFields(flags))
            {
                var name = field.Name;
                //do something with this huh
                if (valueMap.ContainsKey(name) == false)
                {
                    continue;
                }
                var value = valueMap[name];
                var type  = field.FieldType;

                if (type == typeof(System.Boolean) && (bool)value == false)
                {
                    continue;
                }
                if (value == null)
                {
                    continue;
                }

                if (type == typeof(System.Boolean) && (bool)value == true)
                {
                    results += "\n" + field.Name;
                    continue;
                }

                bool fieldHideDefault = field.GetCustomAttribute <HideDefault>() != null;
                if (type.IsEnum && fieldHideDefault && (int)value <= 0)
                {
                    continue;
                }
                if (type == typeof(string) && fieldHideDefault && (value == null || ((string)value).Length <= 0))
                {
                    continue;
                }

                string valueStr = value.ToString();

                // Check for default string field
                if (field.Name == defaultStringField)
                {
                    default_string = value == null ? null : valueStr.Trim('\n', ' ');
                    continue;
                }

                //PREFAB COMPONENT
                if (typeof(Component).IsAssignableFrom(type))
                {
                    valueStr = ObjectToAssetPath((UnityEngine.Object)value);
                }
                //PREFAB
                else if (typeof(GameObject).IsAssignableFrom(type))
                {
                    valueStr = ObjectToAssetPath((UnityEngine.Object)value);
                }
                //OBJECT OR SCRIPTABLE OBJECT
                else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
                {
                    valueStr = ObjectToAssetPath((UnityEngine.Object)value);
                }

                if (valueStr == null)
                {
                    continue;
                }

                if (results.Length > 0)
                {
                    results += "\n";
                }
                results += $"{field.Name}: {valueStr}";
            }

            if (default_string != null)
            {
                results += "\n\n" + default_string;
            }
            return(results.Trim('\n', ' '));
        }
Exemplo n.º 2
0
        public static void DefaultFromEditorContent(this System.Object textEditable, string editorContent)
        {
            var    object_type        = textEditable.GetType();
            var    fieldNames         = new HashSet <string>();
            var    valueMap           = new Dictionary <string, object>();
            var    typeMap            = new Dictionary <string, Type>();
            var    fieldMap           = new Dictionary <string, FieldInfo>();
            var    defaultStringField = textEditable.DefaultStringField();
            string defaultString      = "";

            foreach (var field in object_type.GetFields(flags))
            {
                if (FieldValid(field) == false)
                {
                    continue;
                }
                fieldNames.Add(field.Name);
                var fieldtype = field.FieldType;
                typeMap[field.Name] = fieldtype;

                valueMap[field.Name] = fieldtype.IsValueType ? Activator.CreateInstance(fieldtype) : null;
                if (field.FieldType == typeof(string))
                {
                    valueMap[field.Name] = "";
                }

                fieldMap[field.Name] = field;
            }

            var lines = editorContent.Split('\n');

            foreach (var line in lines)
            {
                if (line.Trim().Length <= 0)
                {
                    continue;
                }

                string key   = null;
                string value = null;

                var colon_index = line.IndexOf(':');
                var single_key  = line.IndexOf(':') < 0 && line.IndexOf(' ') < 0;
                if (colon_index < 0)
                {
                    key = line;
                }
                else
                {
                    key   = line.Substring(0, colon_index).Trim();
                    value = line.Substring(colon_index + 1).Trim();

                    if (key.IndexOf(' ') > 0)
                    {
                        key = null;
                    }
                    if (key?.Length <= 0)
                    {
                        key = null;
                    }
                }

                if (key == null || fieldMap.ContainsKey(key) == false)
                {
                    key   = null;
                    value = line.Trim();
                }
                if (value?.Length <= 0)
                {
                    value = null;
                }

                if (key == null && value != null && defaultStringField != null)
                {
                    defaultString += "\n" + value;
                }

                if (key == null || typeMap.ContainsKey(key) == false)
                {
                    continue;
                }

                var type = typeMap[key];

                try
                {
                    if (type == typeof(bool) && single_key)
                    {
                        valueMap[key] = true;
                    }
                    else if (type == typeof(bool))
                    {
                        valueMap[key] = bool.Parse(value);
                    }
                    else if (type == typeof(int))
                    {
                        valueMap[key] = int.Parse(value);
                    }
                    else if (type == typeof(float))
                    {
                        valueMap[key] = float.Parse(value);
                    }
                    else if (type == typeof(long))
                    {
                        valueMap[key] = long.Parse(value);
                    }
                    else if (type == typeof(double))
                    {
                        valueMap[key] = double.Parse(value);
                    }
                    else if (type.IsEnum)
                    {
                        valueMap[key] = System.Enum.Parse(type, value);
                    }
                    else if (typeof(UnityEngine.GameObject).IsAssignableFrom(type))
                    {
                        valueMap[key] = AssetPathToPrefabObject(value);
                    }
                    else if (typeof(UnityEngine.Component).IsAssignableFrom(type))
                    {
                        valueMap[key] = AssetPathToPrefabObject(value, type);
                    }
                    else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
                    {
                        valueMap[key] = AssetPathToObject(value, type);
                    }
                    else if (type == typeof(string))
                    {
                        valueMap[key] = value == null ? "" : value;
                    }
                }
                catch { }
            }

            if (defaultStringField != null)
            {
                valueMap[defaultStringField] = defaultString.Trim('\n', ' ');
            }

            foreach (var fieldname in typeMap.Keys)
            {
                if (valueMap.ContainsKey(fieldname) == false)
                {
                    continue;
                }
                var field = object_type.GetField(fieldname);
                if (field == null)
                {
                    continue;
                }
                try
                {
                    field.SetValue(textEditable, valueMap[fieldname]);
                }
                catch { }
            }
        }