using System; using System.IO; using System.Linq; using System.Reflection; using UnityEditor; using UnityEngine; namespace DiamondArrow.Editor { // Deterministic sampling of the actual UI/renderer. Never writes the player's save. public static class ArrowMotionPreview { public static string Status="Not started"; static DiamondArrowGame game;static ArrowBoard board;static PlayerSave original;static SaveStore originalStore;static int frame;static float clock;static double next; static readonly BindingFlags Private=BindingFlags.Instance|BindingFlags.NonPublic; static readonly string Folder=DiamondArrowSetup.Root+"Editor/Validation/Polish/Frames~"; public static void Begin() { if(!EditorApplication.isPlaying)throw new InvalidOperationException("Enter Play Mode first"); if(Status=="Recording")throw new InvalidOperationException("Capture is already running"); AssetDatabase.DisallowAutoRefresh();AssemblyReloadEvents.beforeAssemblyReload+=BeforeReload; game=UnityEngine.Object.FindFirstObjectByType();original=game.Save;originalStore=(SaveStore)typeof(DiamondArrowGame).GetField("store",Private).GetValue(game); var save=JsonUtility.FromJson(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; typeof(DiamondArrowGame).GetProperty("Save").SetValue(game,save);typeof(DiamondArrowGame).GetField("store",Private).SetValue(game,new SaveStore(Path.Combine(Application.temporaryCachePath,"DiamondArrowMotionPreview"))); game.ShowLobby();Capture("lobby_after.png");game.StartLevel(8);game.SendMessage("OnApplicationFocus",false);typeof(DiamondArrowGame).GetField("readyAt",Private).SetValue(game,0f); board=game.GetComponentsInChildren().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; } static void Tick() { if(EditorApplication.timeSinceStartupgame.Run.Blocker(i)>=0);game.TapArrow(index);} 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);} if(frame==12)Capture("game_after.png");board.SetVerticesDirty();Canvas.ForceUpdateCanvases();Capture("Frames~/frame_"+frame.ToString("D4")+".png");frame++; if(frame>=330){Status="Captured 330 frames at 30 fps";Finish();} } catch(Exception e){Status="Failed: "+e;Finish();} } static void Finish() { 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(); } static void BeforeReload(){Status="Interrupted by script reload";Finish();} static void Capture(string name) { 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; 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());} finally{camera.targetTexture=prev;RenderTexture.active=active;RenderTexture.ReleaseTemporary(rt);if(texture)UnityEngine.Object.DestroyImmediate(texture);} } } }