public ProjectAsyncOperation SetValue <T>(string key, T obj, ProjectEventHandler callback = null)
        {
            ProjectAsyncOperation ao = new ProjectAsyncOperation();
            ITypeMap typeMap         = IOC.Resolve <ITypeMap>();
            Type     persistentType  = typeMap.ToPersistentType(typeof(T));

            if (persistentType == null)
            {
                ao.Error = new Error(Error.E_NotFound);
                if (callback != null)
                {
                    callback(ao.Error);
                }
                ao.IsCompleted = true;
            }
            else
            {
                PersistentSurrogate surrogate = (PersistentSurrogate)Activator.CreateInstance(persistentType);
                surrogate.ReadFrom(obj);
                ISerializer serializer = IOC.Resolve <ISerializer>();
                byte[]      bytes      = serializer.Serialize(surrogate);
                string      data       = Convert.ToBase64String(bytes);
                PlayerPrefs.SetString(key, data);
                ao.Error = Error.NoError;
                if (callback != null)
                {
                    callback(ao.Error);
                }
                ao.IsCompleted = true;
            }

            return(ao);
        }
 protected void AddSurrogateDeps <T>(T obj, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (obj != null)
     {
         PersistentSurrogate surrogate = convert(obj);
         surrogate.GetDepsFrom(obj, context);
     }
 }
        protected void AddSurrogateDeps(PersistentSurrogate surrogate, GetDepsContext context)
        {
            if (surrogate == null)
            {
                return;
            }

            surrogate.GetDeps(context);
        }
 public bool CanCreateInstance(Type type, PersistentSurrogate surrogate)
 {
     return(type == typeof(Material) ||
            type == typeof(Texture2D) ||
            type == typeof(Mesh) ||
            type == typeof(PhysicMaterial) ||
            type.IsSubclassOf(typeof(ScriptableObject)) ||
            type == typeof(GameObject) ||
            surrogate != null && surrogate.CanInstantiate(type));
 }
Beispiel #5
0
        public UnityObject CreateInstance(Type type)
        {
            Type persistentType           = m_typeMap.ToPersistentType(type);
            PersistentSurrogate surrogate = null;

            if (persistentType != null)
            {
                surrogate = (PersistentSurrogate)Activator.CreateInstance(persistentType);
            }
            return(CreateInstance(type, surrogate));
        }
 protected void AddSurrogateDeps <T>(List <T> surrogateList, GetDepsContext context) where T : PersistentSurrogate
 {
     if (surrogateList == null)
     {
         return;
     }
     for (int i = 0; i < surrogateList.Count; ++i)
     {
         PersistentSurrogate surrogate = surrogateList[i];
         surrogate.GetDeps(context);
     }
 }
 protected void AddSurrogateDeps <T>(T[] surrogateArray, GetDepsContext context) where T : PersistentSurrogate
 {
     if (surrogateArray == null)
     {
         return;
     }
     for (int i = 0; i < surrogateArray.Length; ++i)
     {
         PersistentSurrogate surrogate = surrogateArray[i];
         surrogate.GetDeps(context);
     }
 }
 protected void AddSurrogateDeps <T>(HashSet <T> objHs, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (objHs == null)
     {
         return;
     }
     foreach (T obj in objHs)
     {
         if (obj != null)
         {
             PersistentSurrogate surrogate = convert(obj);
             surrogate.GetDepsFrom(obj, context);
         }
     }
 }
 protected void AddSurrogateDeps <T>(List <T> objList, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (objList == null)
     {
         return;
     }
     for (int i = 0; i < objList.Count; ++i)
     {
         T obj = objList[i];
         if (obj != null)
         {
             PersistentSurrogate surrogate = convert(obj);
             surrogate.GetDepsFrom(obj, context);
         }
     }
 }
 protected void AddSurrogateDeps <T>(T[] objArray, Func <T, PersistentSurrogate> convert, GetDepsFromContext context)
 {
     if (objArray == null)
     {
         return;
     }
     for (int i = 0; i < objArray.Length; ++i)
     {
         T obj = objArray[i];
         if (obj != null)
         {
             PersistentSurrogate surrogate = convert(obj);
             surrogate.GetDepsFrom(obj, context);
         }
     }
 }
        public UnityObject CreateInstance(Type type, PersistentSurrogate surrogate)
        {
            if (type == null)
            {
                Debug.LogError("type is null");
                return(null);
            }

            if (type == typeof(Material))
            {
                Material material = new Material(m_standardShader);
                return(material);
            }
            else if (type == typeof(Texture2D))
            {
                Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, true);
                return(texture);
            }
            else if (type == typeof(Shader))
            {
                Debug.LogWarning("Unable to instantiate Shader");
                return(null);
            }
            else if (type.IsSubclassOf(typeof(ScriptableObject)))
            {
                return(ScriptableObject.CreateInstance(type));
            }

            try
            {
                if (surrogate != null)
                {
                    return((UnityObject)surrogate.Instantiate(type));
                }

                return((UnityObject)Activator.CreateInstance(type));
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                Debug.LogWarning("Collecting scene dependencies could fix this exeption. Tools->Runtime Save Load->Collect Scene Dependencies");
                return(null);
            }
        }
        protected void AddSurrogateDeps <T, V>(Dictionary <T, V> surrogateDict, GetDepsContext context)
        {
            if (surrogateDict == null)
            {
                return;
            }

            foreach (KeyValuePair <T, V> kvp in surrogateDict)
            {
                PersistentSurrogate surrogate = kvp.Key as PersistentSurrogate;
                if (surrogate != null)
                {
                    surrogate.GetDeps(context);
                }

                surrogate = kvp.Value as PersistentSurrogate;
                if (surrogate != null)
                {
                    surrogate.GetDeps(context);
                }
            }
        }
        protected void AddSurrogateDeps <T, V, T1, V1>(Dictionary <T, V> dict, Func <T, T1> convertKey, Func <V, V1> convertValue, GetDepsFromContext context)
        {
            if (dict == null)
            {
                return;
            }
            foreach (KeyValuePair <T, V> kvp in dict)
            {
                T obj = kvp.Key;

                PersistentSurrogate surrogate = convertKey(obj) as PersistentSurrogate;
                if (surrogate != null)
                {
                    surrogate.GetDepsFrom(obj, context);
                }

                surrogate = convertValue(kvp.Value) as PersistentSurrogate;
                if (surrogate != null)
                {
                    surrogate.GetDepsFrom(obj, context);
                }
            }
        }
        public ProjectAsyncOperation <T> GetValue <T>(string key, ProjectEventHandler <T> callback = null)
        {
            ProjectAsyncOperation <T> ao = new ProjectAsyncOperation <T>();
            ITypeMap typeMap             = IOC.Resolve <ITypeMap>();
            Type     persistentType      = typeMap.ToPersistentType(typeof(T));

            if (persistentType == null || !PlayerPrefs.HasKey(key))
            {
                ao.Error = new Error(Error.E_NotFound);
                if (callback != null)
                {
                    callback(ao.Error, default(T));
                }
                ao.IsCompleted = true;
            }
            else
            {
                string data  = PlayerPrefs.GetString(key);
                byte[] bytes = Convert.FromBase64String(data);

                ISerializer         serializer = IOC.Resolve <ISerializer>();
                PersistentSurrogate surrogate  = (PersistentSurrogate)serializer.Deserialize(bytes, persistentType);

                T obj = (T)surrogate.Instantiate(typeof(T));
                surrogate.WriteTo(obj);

                ao.Result = obj;
                ao.Error  = Error.NoError;
                if (callback != null)
                {
                    callback(ao.Error, ao.Result);
                }
                ao.IsCompleted = true;
            }

            return(ao);
        }