Example #1
0
    protected string GetSerializedValue(object value)
    {
        if (value == null)
        {
            return("-1");
        }
        Type type = value.GetType();

        if (typeof(Type).IsAssignableFrom(type))
        {
            return(TypeSymbol + ((Type)value).AssemblyQualifiedName);
        }
        else if (type.IsPrimitive)
        {
            if (value is float)
            {
                return(ValueSymbol + ((float)value).ToString("R"));
            }
            else if (type == typeof(double) || type == typeof(decimal))
            {
                return(ValueSymbol + Convert.ToDouble(value).ToString("R"));
            }
            else
            {
                return(ValueSymbol + value.ToString());
            }
        }
        else if (type.IsEnum)
        {
            return(ValueSymbol + ((int)value).ToString());
        }
        else if (type == typeof(string))
        {
            return(StringSymbol + SerializerUtil.EscapeString(value as string));
        }
        else if (type.IsArray)
        {
            IList list = value as IList;
            if (list == null || list.Count == 0)
            {
                return(EmptyListSymbol);
            }
            else
            {
                return(ListSymbol + GetWriteTargetId(value).ToString());
            }
        }
        //consider not allowing structs since they are problematic, override a class serializer if you want them
        else if (type.IsValueType)
        {
            return(StructSymbol + GetWriteTargetId(value));
        }
        else if (type.IsClass)
        {
            UnityEngine.Object uObj = value as UnityEngine.Object;
            if (uObj == null)
            {
                return(ObjectSymbol + GetWriteTargetId(value).ToString());
            }
            else
            {
#if UNITY_EDITOR
                string assetPath = AssetDatabase.GetAssetPath(uObj);
                if (string.IsNullOrEmpty(assetPath))
                {
                    if (PrefabUtility.GetPrefabType(uObj) == PrefabType.None)
                    {
                        //todo write out game object heirarchy
                        //traverse gameobject to root
                        //ensure each gameobject + transform is serialized
                        //return a ref to parent component
                        return(" { SOMEKINDAGAMEOBJECT");
                    }
                    return(" { Not Asset");
                }
                else
                {
                    return(AssetSymbol + AssetDatabase.AssetPathToGUID(assetPath));
                }
#else
                return("??");
#endif
            }
            //if is unityobject && !subclass of component || gameobject -> serialize
            //todo check if is asset (prefab | texture | scripatable | mesh | etc)
        }
        else
        {
            return(ErrorSymbol + SerializerUtil.GetTypeName(type));
        }
    }