ArrowMotionPreview.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace DiamondArrow.Editor
  8. {
  9. // Deterministic sampling of the actual UI/renderer. Never writes the player's save.
  10. public static class ArrowMotionPreview
  11. {
  12. public static string Status="Not started";
  13. static DiamondArrowGame game;static ArrowBoard board;static PlayerSave original;static SaveStore originalStore;static int frame;static float clock;static double next;
  14. static readonly BindingFlags Private=BindingFlags.Instance|BindingFlags.NonPublic;
  15. static readonly string Folder=DiamondArrowSetup.Root+"Editor/Validation/Polish/Frames~";
  16. public static void Begin()
  17. {
  18. if(!EditorApplication.isPlaying)throw new InvalidOperationException("Enter Play Mode first");
  19. if(Status=="Recording")throw new InvalidOperationException("Capture is already running");
  20. AssetDatabase.DisallowAutoRefresh();AssemblyReloadEvents.beforeAssemblyReload+=BeforeReload;
  21. game=UnityEngine.Object.FindFirstObjectByType<DiamondArrowGame>();original=game.Save;originalStore=(SaveStore)typeof(DiamondArrowGame).GetField("store",Private).GetValue(game);
  22. var save=JsonUtility.FromJson<PlayerSave>(JsonUtility.ToJson(original));save.tutorial=true;save.name="Lucky Vega";save.avatar=0;save.gems=150;save.chestDate="";for(int i=0;i<30;i++)save.stars[i]=i<7?(i<5?3:2):0;
  23. typeof(DiamondArrowGame).GetProperty("Save").SetValue(game,save);typeof(DiamondArrowGame).GetField("store",Private).SetValue(game,new SaveStore(Path.Combine(Application.temporaryCachePath,"DiamondArrowMotionPreview")));
  24. game.ShowLobby();Capture("lobby_after.png");game.StartLevel(8);game.SendMessage("OnApplicationFocus",false);typeof(DiamondArrowGame).GetField("readyAt",Private).SetValue(game,0f);
  25. board=game.GetComponentsInChildren<ArrowBoard>().First(b=>b.interactive);clock=Time.unscaledTime+1;board.PreviewTime=clock;board.Configure(game.Run.Level,game.Run);frame=0;Directory.CreateDirectory(Folder);Status="Recording";next=EditorApplication.timeSinceStartup+.5;EditorApplication.update-=Tick;EditorApplication.update+=Tick;
  26. }
  27. static void Tick()
  28. {
  29. if(EditorApplication.timeSinceStartup<next)return;next=EditorApplication.timeSinceStartup+.015;
  30. try
  31. {
  32. if(!EditorApplication.isPlaying)throw new InvalidOperationException("Play Mode stopped");
  33. board.PreviewTime=clock+frame/30f;
  34. if(frame==18){int index=Enumerable.Range(0,game.Run.Alive.Length).First(i=>game.Run.Blocker(i)>=0);game.TapArrow(index);}
  35. if(frame>=60&&frame<=252&&frame%12==0){var free=game.Run.FreeArrows();int index=free.OrderByDescending(i=>game.Run.Level.arrows[i].cells.Length).First();game.TapArrow(index);}
  36. if(frame==12)Capture("game_after.png");board.SetVerticesDirty();Canvas.ForceUpdateCanvases();Capture("Frames~/frame_"+frame.ToString("D4")+".png");frame++;
  37. if(frame>=330){Status="Captured 330 frames at 30 fps";Finish();}
  38. }
  39. catch(Exception e){Status="Failed: "+e;Finish();}
  40. }
  41. static void Finish()
  42. {
  43. EditorApplication.update-=Tick;if(game){typeof(DiamondArrowGame).GetProperty("Save").SetValue(game,original);typeof(DiamondArrowGame).GetField("store",Private).SetValue(game,originalStore);game.SendMessage("OnApplicationFocus",true);game.ShowLobby();}Debug.Log(Status);AssemblyReloadEvents.beforeAssemblyReload-=BeforeReload;AssetDatabase.AllowAutoRefresh();
  44. }
  45. static void BeforeReload(){Status="Interrupted by script reload";Finish();}
  46. static void Capture(string name)
  47. {
  48. Canvas.ForceUpdateCanvases();var camera=Camera.main;var prev=camera.targetTexture;var active=RenderTexture.active;var rt=RenderTexture.GetTemporary(Screen.width,Screen.height,24);Texture2D texture=null;
  49. try{camera.targetTexture=rt;camera.Render();RenderTexture.active=rt;texture=new Texture2D(rt.width,rt.height,TextureFormat.RGB24,false);texture.ReadPixels(new Rect(0,0,rt.width,rt.height),0,0);texture.Apply();File.WriteAllBytes(DiamondArrowSetup.Root+"Editor/Validation/Polish/"+name,texture.EncodeToPNG());}
  50. finally{camera.targetTexture=prev;RenderTexture.active=active;RenderTexture.ReleaseTemporary(rt);if(texture)UnityEngine.Object.DestroyImmediate(texture);}
  51. }
  52. }
  53. }