private void LoadState(byte[][] state) { ISerializer serializer = IOC.Resolve <ISerializer>(); Type animType = GetSurrogateType(typeof(RuntimeAnimation)); Type clipType = GetSurrogateType(typeof(RuntimeAnimationClip)); if (serializer == null || animType == null || clipType == null) { return; } IPersistentSurrogate animationSurrogate = (IPersistentSurrogate)serializer.Deserialize(state[0], animType); animationSurrogate.WriteTo(Target); IList <RuntimeAnimationClip> clips = Target.Clips; for (int i = 0; i < clips.Count; ++i) { RuntimeAnimationClip clip = clips[i]; if (clip == null) { clips[i] = clip = ScriptableObject.CreateInstance <RuntimeAnimationClip>(); } IPersistentSurrogate clipSurrogate = (IPersistentSurrogate)serializer.Deserialize(state[1 + i], clipType); clipSurrogate.WriteTo(clip); } }
private static void SaveDependencies(GetDepsFromContext context, ITypeMap typeMap, IAssetDB assetDB, string path) { object[] dependencies = context.Dependencies.ToArray(); foreach (UnityObject dep in dependencies) { Type persistentType = typeMap.ToPersistentType(dep.GetType()); if (persistentType != null) { context.Dependencies.Clear(); IPersistentSurrogate persistentObject = (IPersistentSurrogate)Activator.CreateInstance(persistentType); persistentObject.GetDepsFrom(dep, context); SaveDependencies(context, typeMap, assetDB, path); } } foreach (UnityObject dep in dependencies) { if (dep is Component || dep is GameObject || !assetDB.IsDynamicResourceID(assetDB.ToID(dep))) { continue; } string name = dep.name; if (string.IsNullOrWhiteSpace(name)) { name = dep.GetType().Name; } string uniqueAssetPath = AssetDatabase.GenerateUniqueAssetPath(string.Format("{0}/{1}.asset", path, name)); AssetDatabase.CreateAsset(dep, uniqueAssetPath); } }
private byte[][] SaveState() { ISerializer serializer = IOC.Resolve <ISerializer>(); Type animType = GetSurrogateType(typeof(RuntimeAnimation)); Type clipType = GetSurrogateType(typeof(RuntimeAnimationClip)); if (serializer == null || animType == null || clipType == null) { return(new byte[0][]); } IList <RuntimeAnimationClip> clips = Target.Clips; byte[][] state = new byte[1 + clips.Count][]; IPersistentSurrogate animationSurrogate = (IPersistentSurrogate)Activator.CreateInstance(animType); IPersistentSurrogate clipSurrogate = (IPersistentSurrogate)Activator.CreateInstance(clipType); animationSurrogate.ReadFrom(Target); state[0] = serializer.Serialize(animationSurrogate); for (int i = 0; i < clips.Count; ++i) { RuntimeAnimationClip clip = clips[i]; clipSurrogate.ReadFrom(clip); state[1 + i] = serializer.Serialize(clipSurrogate); } return(state); }
protected void AddSurrogateDeps <T>(T obj, Func <T, IPersistentSurrogate> convert, GetDepsFromContext context) { if (obj != null) { IPersistentSurrogate surrogate = convert(obj); surrogate.GetDepsFrom(obj, context); } }
protected void AddSurrogateDeps <T>(T obj, Func <T, IPersistentSurrogate <TID> > convert, GetDepsContext <TID> context) { if (obj != null) { IPersistentSurrogate <TID> surrogate = convert(obj); surrogate.GetDeps(context); } }
protected void WriteSurrogateTo(IPersistentSurrogate from, object to) { if (from == null) { return; } from.WriteTo(to); }
protected void AddSurrogateDeps(IPersistentSurrogate <TID> surrogate, GetDepsContext <TID> context) { if (surrogate == null) { return; } surrogate.GetDeps(context); }
public bool CanCreateInstance(Type type, IPersistentSurrogate 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)); }
public UnityObject CreateInstance(Type type) { Type persistentType = m_typeMap.ToPersistentType(type); IPersistentSurrogate surrogate = null; if (persistentType != null) { surrogate = (IPersistentSurrogate)Activator.CreateInstance(persistentType); } return(CreateInstance(type, surrogate)); }
protected void AddSurrogateDeps <T>(List <T> surrogateList, GetDepsContext <TID> context) where T : IPersistentSurrogate <TID> { if (surrogateList == null) { return; } for (int i = 0; i < surrogateList.Count; ++i) { IPersistentSurrogate <TID> surrogate = surrogateList[i]; surrogate.GetDeps(context); } }
protected void AddSurrogateDeps <T>(T[] surrogateArray, GetDepsContext <TID> context) where T : IPersistentSurrogate <TID> { if (surrogateArray == null) { return; } for (int i = 0; i < surrogateArray.Length; ++i) { IPersistentSurrogate <TID> surrogate = surrogateArray[i]; surrogate.GetDeps(context); } }
public UnityObject CreateInstance(Type type, IPersistentSurrogate surrogate) { if (type == null) { Debug.LogError("type is null"); return(null); } if (type == typeof(Material)) { Material material = new Material(RenderPipelineInfo.DefaultMaterial); return(material); } else if (type == typeof(Texture2D)) { if (surrogate != null && surrogate.CanInstantiate(typeof(Texture2D))) { return((UnityObject)surrogate.Instantiate(type)); } 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))) { ScriptableObject obj = ScriptableObject.CreateInstance(type); obj.name = type.Name; return(obj); } 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>(HashSet <T> objHs, Func <T, IPersistentSurrogate> convert, GetDepsFromContext context) { if (objHs == null) { return; } foreach (T obj in objHs) { if (obj != null) { IPersistentSurrogate surrogate = convert(obj); surrogate.GetDepsFrom(obj, context); } } }
protected void AddSurrogateDeps <T>(T[] objArray, Func <T, IPersistentSurrogate> convert, GetDepsFromContext context) { if (objArray == null) { return; } for (int i = 0; i < objArray.Length; ++i) { T obj = objArray[i]; if (obj != null) { IPersistentSurrogate surrogate = convert(obj); surrogate.GetDepsFrom(obj, context); } } }
protected void AddSurrogateDeps <T>(List <T> objList, Func <T, IPersistentSurrogate> convert, GetDepsFromContext context) { if (objList == null) { return; } for (int i = 0; i < objList.Count; ++i) { T obj = objList[i]; if (obj != null) { IPersistentSurrogate surrogate = convert(obj); surrogate.GetDepsFrom(obj, context); } } }
protected void AddSurrogateDeps <T, V>(Dictionary <T, V> surrogateDict, GetDepsContext <TID> context) { if (surrogateDict == null) { return; } foreach (KeyValuePair <T, V> kvp in surrogateDict) { IPersistentSurrogate <TID> surrogate = kvp.Key as IPersistentSurrogate <TID>; if (surrogate != null) { surrogate.GetDeps(context); } surrogate = kvp.Value as IPersistentSurrogate <TID>; 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; IPersistentSurrogate surrogate = convertKey(obj) as IPersistentSurrogate; if (surrogate != null) { surrogate.GetDepsFrom(obj, context); } surrogate = convertValue(kvp.Value) as IPersistentSurrogate; if (surrogate != null) { surrogate.GetDepsFrom(obj, context); } } }
private static void CreateAssetLibraryForScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs) { TypeMap <long> typeMap = new TypeMap <long>(); AssetDB assetDB = new AssetDB(); RuntimeShaderUtil shaderUtil = new RuntimeShaderUtil(); IOC.Register <ITypeMap>(typeMap); IOC.Register <IAssetDB>(assetDB); IOC.Register <IAssetDB <long> >(assetDB); IOC.Register <IRuntimeShaderUtil>(shaderUtil); PersistentRuntimeScene <long> rtScene = new PersistentRuntimeScene <long>(); GetDepsFromContext ctx = new GetDepsFromContext(); rtScene.GetDepsFrom(scene, ctx); Queue <UnityObject> depsQueue = new Queue <UnityObject>(ctx.Dependencies.OfType <UnityObject>()); GetDepsFromContext getDepsCtx = new GetDepsFromContext(); while (depsQueue.Count > 0) { UnityObject uo = depsQueue.Dequeue(); if (!uo) { continue; } Type persistentType = typeMap.ToPersistentType(uo.GetType()); if (persistentType != null) { getDepsCtx.Clear(); try { IPersistentSurrogate persistentObject = (IPersistentSurrogate)Activator.CreateInstance(persistentType); persistentObject.GetDepsFrom(uo, getDepsCtx); } catch (Exception e) { Debug.LogError(e); } foreach (UnityObject dep in getDepsCtx.Dependencies) { if (!ctx.Dependencies.Contains(dep)) { ctx.Dependencies.Add(dep); depsQueue.Enqueue(dep); } } } } IOC.Unregister <IRuntimeShaderUtil>(shaderUtil); IOC.Unregister <ITypeMap>(typeMap); IOC.Unregister <IAssetDB>(assetDB); IOC.Unregister <IAssetDB <long> >(assetDB); CreateAssetLibrary(ctx.Dependencies.ToArray(), "Scenes/" + scene.name, "SceneAssetLibrary", index, asset, folder, hs); }