HexMotionReviewCapture.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace HexaCrazy.Editor
  9. {
  10. public static class HexMotionReviewCapture
  11. {
  12. static string Folder=>Path.GetFullPath("output/qa/motion-v3");
  13. [MenuItem("Hexa Crazy/Capture Merge Feedback (Play Mode)")]
  14. public static void Capture()
  15. {
  16. if(!EditorApplication.isPlaying)throw new System.InvalidOperationException("Enter Play Mode first.");
  17. var app=Object.FindAnyObjectByType<HexGameApp>();app.StartCoroutine(Run(app));
  18. }
  19. static IEnumerator Run(HexGameApp app)
  20. {
  21. while(string.IsNullOrEmpty(app.ScreenName)||app.ScreenName=="Loading")yield return null;
  22. Directory.CreateDirectory(Folder);Directory.CreateDirectory(Path.Combine(Folder,"frames"));
  23. HexProjectBuilder.SetGameView(750,1334);app.UseReviewProfile(Path.Combine(Folder,"review-profile.json"));app.Profile.Reset();
  24. app.Profile.Data.tutorial=1;app.Profile.Data.reduceMotion=false;app.Profile.Data.music=false;app.Profile.Data.sound=false;
  25. app.GetComponent<HexAudio>().Music=false;app.GetComponent<HexAudio>().Sound=false;
  26. app.StartLevel(0,1,true);foreach(var c in app.Game.Cells)c.tiles.Clear();
  27. int source=app.Game.Cells.FindIndex(c=>c.q==-1&&c.r==0),target=app.Game.Cells.FindIndex(c=>c.q==0&&c.r==0);
  28. int drop=app.Game.Cells.FindIndex(c=>c.q==1&&c.r==0),lemon=app.Game.Cells.FindIndex(c=>c.q==-2&&c.r==0);
  29. app.Game.Cells[source].tiles.AddRange(Enumerable.Repeat(1,7));app.Game.Cells[source].tiles.AddRange(Enumerable.Repeat(0,3));
  30. app.Game.Cells[target].tiles.AddRange(Enumerable.Repeat(0,5));app.Game.Cells[lemon].tiles.Add(1);
  31. app.Game.Hand=new[]{new HandGroup(0,1),new HandGroup(3,3),new HandGroup(2,4)};app.SelectHand(0);
  32. yield return new WaitForSecondsRealtime(.4f);yield return new WaitForEndOfFrame();ScreenCapture.CaptureScreenshot(Path.Combine(Folder,"mixed-stack.png"));
  33. var times=new List<string>();var states=new List<string>{"frame,time,step,flying,source,target,score,displayedScore"};int frame=0;float start=Time.realtimeSinceStartup;bool moved=false,mergeShot=false,floatShot=false,chainShot=false;
  34. var frames=new List<Texture2D>();var shots=new Dictionary<string,Texture2D>();
  35. var capture=new RenderTexture(750,1334,0,RenderTextureFormat.ARGB32);capture.Create();
  36. var small=new RenderTexture(375,667,0,RenderTextureFormat.ARGB32);small.Create();
  37. var views=app.Root.GetComponentsInChildren<HexStackView>();var bar=app.Root.GetComponentInChildren<HexScoreBar>();
  38. while(Time.realtimeSinceStartup-start<7.2f)
  39. {
  40. if(!moved&&Time.realtimeSinceStartup-start>.8f){app.TapCell(drop);moved=true;}
  41. yield return new WaitForEndOfFrame();
  42. float now=Time.realtimeSinceStartup;times.Add(now.ToString("F5",CultureInfo.InvariantCulture));
  43. var previous=RenderTexture.active;ScreenCapture.CaptureScreenshotIntoRenderTexture(capture);
  44. if(SystemInfo.graphicsUVStartsAtTop)Graphics.Blit(capture,small,new Vector2(1,-1),new Vector2(0,1));else Graphics.Blit(capture,small);
  45. RenderTexture.active=small;
  46. var texture=new Texture2D(375,667,TextureFormat.RGB24,false);texture.ReadPixels(new Rect(0,0,375,667),0,0,false);RenderTexture.active=previous;frames.Add(texture);
  47. states.Add(string.Join(",",frame,(now-start).ToString("F4",CultureInfo.InvariantCulture),app.AnimationStep,app.FlyingTileCount,views.First(v=>v.CellIndex==source).TileCount,views.First(v=>v.CellIndex==target).TileCount,app.Game.Score,bar.DisplayedScore.ToString("F3",CultureInfo.InvariantCulture)));
  48. if(!mergeShot&&app.AnimationStep==StepKind.Merge&&app.FlyingTileCount>=2){shots.Add("merge-in-flight",ScreenCapture.CaptureScreenshotAsTexture());mergeShot=true;}
  49. if(!floatShot&&bar.TargetScore==74&&bar.DisplayedScore>25&&bar.DisplayedScore<70){shots.Add("score-feedback",ScreenCapture.CaptureScreenshotAsTexture());floatShot=true;}
  50. if(!chainShot&&bar.TargetScore==194&&bar.DisplayedScore>120&&bar.DisplayedScore<190){shots.Add("chain-feedback",ScreenCapture.CaptureScreenshotAsTexture());chainShot=true;}
  51. frame++;yield return null;
  52. }
  53. capture.Release();small.Release();Object.Destroy(capture);Object.Destroy(small);
  54. // Encode after playback so PNG/JPEG compression cannot stall the motion being reviewed.
  55. for(int i=0;i<frames.Count;i++){File.WriteAllBytes(Path.Combine(Folder,"frames",i.ToString("D4")+".jpg"),frames[i].EncodeToJPG(92));Object.Destroy(frames[i]);if(i%8==0)yield return null;}
  56. foreach(var shot in shots){File.WriteAllBytes(Path.Combine(Folder,shot.Key+".png"),shot.Value.EncodeToPNG());Object.Destroy(shot.Value);}
  57. File.WriteAllLines(Path.Combine(Folder,"frame-times.txt"),times);File.WriteAllLines(Path.Combine(Folder,"frame-states.csv"),states);
  58. yield return new WaitForEndOfFrame();ScreenCapture.CaptureScreenshot(Path.Combine(Folder,"settled.png"));
  59. File.WriteAllText(Path.Combine(Folder,"capture-complete.txt"),System.DateTimeOffset.UtcNow.ToString("O"));Debug.Log("HEXA_MOTION_CAPTURE_COMPLETE");
  60. }
  61. }
  62. }