You've already forked UniversalViewer
							
							DanMachi:
-compatibility improvements Core: -simplified object and keyframe serialization -complicated assetbundle loading
This commit is contained in:
		| @@ -1,20 +1,16 @@ | ||||
| using System.Collections; | ||||
| using UnityEngine; | ||||
| using System.Linq; | ||||
|  | ||||
| public class CameraContainer: ObjectContainer | ||||
| { | ||||
|     protected override System.Type _serializeType => typeof(CameraContainerSerializable); | ||||
|     protected override System.Type _keyframeType => typeof(KeyframeDataCamera); | ||||
|  | ||||
|     public CameraOrbit Camera; | ||||
|     public KeyframeDataCamera LastPosition; | ||||
|     public LineRenderer LineRenderer; | ||||
|  | ||||
|     public CameraSettings Settings; | ||||
|  | ||||
|     protected override void Start() | ||||
|     { | ||||
|         //Set default keyframe in custom ModelViewerMain | ||||
|     } | ||||
|  | ||||
|     private void Update() | ||||
|     { | ||||
|         if (!LineRenderer.enabled) return; | ||||
| @@ -63,22 +59,6 @@ public class CameraContainer: ObjectContainer | ||||
|         return inverse.MultiplyPoint3x4(pos); | ||||
|     } | ||||
|  | ||||
|     public override ObjectContainerSerializable Serialize() | ||||
|     { | ||||
|         var serialized = new CameraContainerSerializable(this); | ||||
|         return serialized; | ||||
|     } | ||||
|  | ||||
|     public override KeyframeData SerializeFrame() | ||||
|     { | ||||
|         return new KeyframeDataCamera(this); | ||||
|     } | ||||
|  | ||||
|     public override void Deserialize(ObjectContainerSerializable serialized) | ||||
|     { | ||||
|         DeserializeFrames(serialized); | ||||
|     } | ||||
|  | ||||
|     public override void Lerp(KeyframeData frame1, KeyframeData frame2, float amount) | ||||
|     { | ||||
|         var tsf = frame1 as KeyframeDataCamera; | ||||
| @@ -103,14 +83,6 @@ public class CameraContainer: ObjectContainer | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public override void Select() | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     public override void Deselect() | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     public override void PastePose(KeyframeData frame, PoseLoadOptions pasteParams = null) | ||||
|     { | ||||
|         var keyframe = frame as KeyframeDataCamera; | ||||
| @@ -121,10 +93,12 @@ public class CameraContainer: ObjectContainer | ||||
|         //if (Camera.lookRotation.x < -90) Camera.lookRotation.x += 360; | ||||
|         //lookRotation.x = lookRotation.x - 360; | ||||
|  | ||||
|         Debug.Log(keyframe.CameraLock); | ||||
|  | ||||
|         using (new KeyframeToggleContext(this)) | ||||
|         { | ||||
|             Settings.DropdownCameraMode.value = keyframe.CameraMode; | ||||
|             //Settings.ToggleCameraLock.isOn = keyframe.CameraLock; | ||||
|             Settings.ToggleCameraLock.isOn = keyframe.CameraLock; | ||||
|             Settings.SliderFov.value = keyframe.Fov; | ||||
|             Settings.SliderCamDist.value = keyframe.CamDist; | ||||
|             Settings.SliderCamHeight.value = keyframe.CamHeight; | ||||
| @@ -132,12 +106,13 @@ public class CameraContainer: ObjectContainer | ||||
|             Settings.SliderCamAngle.value = keyframe.CamAngle; | ||||
|         } | ||||
|  | ||||
|         Camera.UpdateImmediate(); | ||||
|         Camera.UpdateImmediate(true); | ||||
|     } | ||||
|  | ||||
|     public override void Destroy() | ||||
|     { | ||||
|         //Do not destroy cameras | ||||
|         Debug.Log("Clearing camera"); | ||||
|         Frames.Clear(); | ||||
|         SetDefaultFrame(); | ||||
|     } | ||||
|   | ||||
| @@ -1,4 +1,3 @@ | ||||
| using System; | ||||
| using System.Linq; | ||||
| using UnityEngine; | ||||
| using Newtonsoft.Json; | ||||
| @@ -6,6 +5,9 @@ using System.Collections.Generic; | ||||
|  | ||||
| public class ObjectContainer : MonoBehaviour, IKeyframeSetter | ||||
| { | ||||
|     protected virtual System.Type _serializeType => typeof(ObjectContainerSerializable); | ||||
|     protected virtual System.Type _keyframeType => typeof(KeyframeData); | ||||
|  | ||||
|     [JsonIgnore] public bool DoNotSave; | ||||
|     public int ModelId; | ||||
|     public string GUID = ""; | ||||
| @@ -16,6 +18,7 @@ public class ObjectContainer : MonoBehaviour, IKeyframeSetter | ||||
|     public Dictionary<SkinnedMeshRenderer, List<MorphHelper>> Morphs = new Dictionary<SkinnedMeshRenderer, List<MorphHelper>>(); | ||||
|     public List<FrameContent> Frames = new List<FrameContent>(); | ||||
|     public List<GameObject> InstantiatedObjects = new List<GameObject>(); | ||||
|     public List<AssetBundleEntry> LoadedAssets = new List<AssetBundleEntry>(); | ||||
|     protected bool _setKeyframe = true; | ||||
|     protected bool _applicationQuitting = false; | ||||
|  | ||||
| @@ -36,9 +39,9 @@ public class ObjectContainer : MonoBehaviour, IKeyframeSetter | ||||
|         if (ModelViewerMain.GetInstance() == null) return; | ||||
|         if (DoNotSave) return; | ||||
|  | ||||
|         if (Frames.Count == 0) | ||||
|         if(Frames.Count == 0) | ||||
|         { | ||||
|             SetKeyframe(); | ||||
|             SetDefaultFrame(); | ||||
|         } | ||||
|  | ||||
|         ModelViewerMain.RegisterObject(this); | ||||
| @@ -63,17 +66,12 @@ public class ObjectContainer : MonoBehaviour, IKeyframeSetter | ||||
|  | ||||
|     public virtual ObjectContainerSerializable Serialize() | ||||
|     { | ||||
|         return new ObjectContainerSerializable(this); | ||||
|         return ObjectContainerSerializable.Serialize(_serializeType, this); | ||||
|     } | ||||
|  | ||||
|     public virtual KeyframeData SerializeFrame() | ||||
|     { | ||||
|         return new KeyframeData(this); | ||||
|     } | ||||
|  | ||||
|     public virtual void Deserialize(ObjectContainerSerializable serialized) | ||||
|     { | ||||
|         throw new NotImplementedException(GetType().FullName); | ||||
|         return KeyframeData.Serialize(_keyframeType, this); | ||||
|     } | ||||
|  | ||||
|     protected virtual void OnDestroy() | ||||
| @@ -104,8 +102,6 @@ public class ObjectContainer : MonoBehaviour, IKeyframeSetter | ||||
|     { | ||||
|         switch (this) | ||||
|         { | ||||
|             case SceneContainer _: | ||||
|                 return DataType.Scene; | ||||
|             case CameraContainer _: | ||||
|                 return DataType.Camera; | ||||
|             default: | ||||
| @@ -195,6 +191,7 @@ public class ObjectContainer : MonoBehaviour, IKeyframeSetter | ||||
|  | ||||
|     public void SetDefaultFrame() | ||||
|     { | ||||
|         Debug.Log("Setting default frames for " + name); | ||||
|         Frames = new List<FrameContent>() | ||||
|         { | ||||
|             new FrameContent(0).SetObjectData(SerializeFrame()) | ||||
| @@ -229,12 +226,11 @@ public class ObjectContainer : MonoBehaviour, IKeyframeSetter | ||||
|  | ||||
|     public virtual void Deselect() | ||||
|     { | ||||
|         throw new NotImplementedException(this.GetType().FullName); | ||||
|     } | ||||
|  | ||||
|     public virtual void Select() | ||||
|     { | ||||
|         throw new NotImplementedException(this.GetType().FullName); | ||||
|         AssetLibrary.GetInstance().SetAssetList(LoadedAssets.Select(a=> new string[] {a.Name, a.FilePath}).ToList()); | ||||
|     } | ||||
|  | ||||
|     public virtual void Destroy() | ||||
|   | ||||
| @@ -1,6 +1,12 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
|  | ||||
| public interface ISerializableContainer | ||||
| { | ||||
|     public abstract ObjectContainerSerializable Serialize(); | ||||
| } | ||||
|  | ||||
| [System.Serializable] | ||||
| public class ObjectContainerSerializable | ||||
| { | ||||
| @@ -23,4 +29,15 @@ public class ObjectContainerSerializable | ||||
|         this.GUID = source.GUID; | ||||
|         this.Frames = source.Frames.Select(frame => new FrameContent(frame)).ToList(); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     /// Call type's constructor with container as parameter | ||||
|     /// </summary> | ||||
|     /// <param name="type">Must derive from ObjectContainer</param> | ||||
|     /// <param name="container">Type must accept it as the only constructor parameter</param> | ||||
|     /// <returns></returns> | ||||
|     public static ObjectContainerSerializable Serialize(Type type, ObjectContainer container) | ||||
|     { | ||||
|         return Activator.CreateInstance(type, new object[] { container }) as ObjectContainerSerializable; | ||||
|     } | ||||
| } | ||||
| @@ -2,11 +2,10 @@ using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using UnityEngine; | ||||
|  | ||||
| public class SceneContainer : ObjectContainer | ||||
| [System.Serializable] | ||||
| public class SceneContainer | ||||
| { | ||||
|     public CameraContainer MainCameraOrbit; | ||||
|     public CameraContainer WorkCameraOrbit; | ||||
|  | ||||
|     public string GUID; | ||||
|     public List<ObjectContainer> AllObjects = new List<ObjectContainer>(); | ||||
|  | ||||
|     public List<T> Objects<T>() where T : ObjectContainer | ||||
| @@ -14,9 +13,11 @@ public class SceneContainer : ObjectContainer | ||||
|         return AllObjects.Cast<T>().ToList(); | ||||
|     } | ||||
|  | ||||
|     public static SceneContainer Create<T>(ModelViewerMain main) where T : SceneContainer | ||||
|     public SceneContainer() { } | ||||
|  | ||||
|     public static SceneContainer Create<T>(ModelViewerMain main) where T : SceneContainer, new() | ||||
|     { | ||||
|         var container = new GameObject("Scene").AddComponent<T>(); | ||||
|         var container = new T(); | ||||
|         container.Init(main); | ||||
|         return container; | ||||
|     } | ||||
| @@ -24,26 +25,22 @@ public class SceneContainer : ObjectContainer | ||||
|     public virtual void Init(ModelViewerMain main) | ||||
|     { | ||||
|         GUID = "scene"; | ||||
|         MainCameraOrbit = main.MainCameraOrbit; | ||||
|         WorkCameraOrbit = main.WorkCameraOrbit; | ||||
|         SetDefaultFrame(); | ||||
|         AllObjects.Add(MainCameraOrbit); | ||||
|         if (!AllObjects.Contains(main.MainCameraOrbit)) { AllObjects.Add(main.MainCameraOrbit); } | ||||
|     } | ||||
|  | ||||
|     public override ObjectContainerSerializable Serialize() | ||||
|     public virtual SceneSerializable Serialize() | ||||
|     { | ||||
|         return new SceneSerializable(this); | ||||
|     } | ||||
|  | ||||
|     protected override void OnDestroy() | ||||
|     public virtual void Destroy() | ||||
|     { | ||||
|         if (_applicationQuitting) return; | ||||
|  | ||||
|         for (int i = AllObjects.Count - 1; i >= 0; i--) | ||||
|         { | ||||
|             var obj = AllObjects[i]; | ||||
|             if (obj == this) continue; | ||||
|             obj.Destroy(); | ||||
|         } | ||||
|  | ||||
|         AllObjects.Clear(); | ||||
|     } | ||||
| } | ||||
| @@ -5,10 +5,12 @@ using System.Linq; | ||||
| using UnityEngine; | ||||
|  | ||||
| [Serializable] | ||||
| public class SceneSerializable : ObjectContainerSerializable | ||||
| public class SceneSerializable | ||||
| { | ||||
|     public string GUID; | ||||
|     public string Version; | ||||
|     public string Date; | ||||
|     public string Filename; | ||||
|     public TimelineControllerSerializable Timeline; | ||||
|     public List<ObjectContainerSerializable> Objects = new List<ObjectContainerSerializable>(); | ||||
|  | ||||
| @@ -19,11 +21,9 @@ public class SceneSerializable : ObjectContainerSerializable | ||||
|         this.Version = Application.version; | ||||
|         this.Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture); ; | ||||
|         this.Timeline = new TimelineControllerSerializable(TimelineController.Instance); | ||||
|         this.Frames = scene.Frames.Select(f => new FrameContent(f)).ToList(); | ||||
|         foreach (var obj in scene.AllObjects) | ||||
|         { | ||||
|             if (obj != scene) | ||||
|                 Objects.Add(obj.Serialize()); | ||||
|             Objects.Add(obj.Serialize()); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user