| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- namespace HexaCrazy.Editor
- {
- public static class HexMotionReviewCapture
- {
- static string Folder=>Path.GetFullPath("output/qa/motion-v3");
- [MenuItem("Hexa Crazy/Capture Merge Feedback (Play Mode)")]
- public static void Capture()
- {
- if(!EditorApplication.isPlaying)throw new System.InvalidOperationException("Enter Play Mode first.");
- var app=Object.FindAnyObjectByType<HexGameApp>();app.StartCoroutine(Run(app));
- }
- static IEnumerator Run(HexGameApp app)
- {
- while(string.IsNullOrEmpty(app.ScreenName)||app.ScreenName=="Loading")yield return null;
- Directory.CreateDirectory(Folder);Directory.CreateDirectory(Path.Combine(Folder,"frames"));
- HexProjectBuilder.SetGameView(750,1334);app.UseReviewProfile(Path.Combine(Folder,"review-profile.json"));app.Profile.Reset();
- app.Profile.Data.tutorial=1;app.Profile.Data.reduceMotion=false;app.Profile.Data.music=false;app.Profile.Data.sound=false;
- app.GetComponent<HexAudio>().Music=false;app.GetComponent<HexAudio>().Sound=false;
- app.StartLevel(0,1,true);foreach(var c in app.Game.Cells)c.tiles.Clear();
- 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);
- 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);
- app.Game.Cells[source].tiles.AddRange(Enumerable.Repeat(1,7));app.Game.Cells[source].tiles.AddRange(Enumerable.Repeat(0,3));
- app.Game.Cells[target].tiles.AddRange(Enumerable.Repeat(0,5));app.Game.Cells[lemon].tiles.Add(1);
- app.Game.Hand=new[]{new HandGroup(0,1),new HandGroup(3,3),new HandGroup(2,4)};app.SelectHand(0);
- yield return new WaitForSecondsRealtime(.4f);yield return new WaitForEndOfFrame();ScreenCapture.CaptureScreenshot(Path.Combine(Folder,"mixed-stack.png"));
- 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;
- var frames=new List<Texture2D>();var shots=new Dictionary<string,Texture2D>();
- var capture=new RenderTexture(750,1334,0,RenderTextureFormat.ARGB32);capture.Create();
- var small=new RenderTexture(375,667,0,RenderTextureFormat.ARGB32);small.Create();
- var views=app.Root.GetComponentsInChildren<HexStackView>();var bar=app.Root.GetComponentInChildren<HexScoreBar>();
- while(Time.realtimeSinceStartup-start<7.2f)
- {
- if(!moved&&Time.realtimeSinceStartup-start>.8f){app.TapCell(drop);moved=true;}
- yield return new WaitForEndOfFrame();
- float now=Time.realtimeSinceStartup;times.Add(now.ToString("F5",CultureInfo.InvariantCulture));
- var previous=RenderTexture.active;ScreenCapture.CaptureScreenshotIntoRenderTexture(capture);
- if(SystemInfo.graphicsUVStartsAtTop)Graphics.Blit(capture,small,new Vector2(1,-1),new Vector2(0,1));else Graphics.Blit(capture,small);
- RenderTexture.active=small;
- 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);
- 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)));
- if(!mergeShot&&app.AnimationStep==StepKind.Merge&&app.FlyingTileCount>=2){shots.Add("merge-in-flight",ScreenCapture.CaptureScreenshotAsTexture());mergeShot=true;}
- if(!floatShot&&bar.TargetScore==74&&bar.DisplayedScore>25&&bar.DisplayedScore<70){shots.Add("score-feedback",ScreenCapture.CaptureScreenshotAsTexture());floatShot=true;}
- if(!chainShot&&bar.TargetScore==194&&bar.DisplayedScore>120&&bar.DisplayedScore<190){shots.Add("chain-feedback",ScreenCapture.CaptureScreenshotAsTexture());chainShot=true;}
- frame++;yield return null;
- }
- capture.Release();small.Release();Object.Destroy(capture);Object.Destroy(small);
- // Encode after playback so PNG/JPEG compression cannot stall the motion being reviewed.
- 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;}
- foreach(var shot in shots){File.WriteAllBytes(Path.Combine(Folder,shot.Key+".png"),shot.Value.EncodeToPNG());Object.Destroy(shot.Value);}
- File.WriteAllLines(Path.Combine(Folder,"frame-times.txt"),times);File.WriteAllLines(Path.Combine(Folder,"frame-states.csv"),states);
- yield return new WaitForEndOfFrame();ScreenCapture.CaptureScreenshot(Path.Combine(Folder,"settled.png"));
- File.WriteAllText(Path.Combine(Folder,"capture-complete.txt"),System.DateTimeOffset.UtcNow.ToString("O"));Debug.Log("HEXA_MOTION_CAPTURE_COMPLETE");
- }
- }
- }
|