예제 #1
0
        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);
            }
        }
        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);
        }