| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System;
- using System.IO;
- using DevilLink2.Presentation;
- using UnityEditor;
- using UnityEditor.SceneManagement;
- using UnityEditor.TestTools.TestRunner.Api;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- namespace DevilLink2.Editor
- {
- [InitializeOnLoad]
- public static class DevilLink2BoardSetup
- {
- public const string ScenePath = "Assets/DevilLink2/Scenes/Main.unity";
- public const string PrefabPath = "Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab";
- public const string RootName = "DevilLink2BoardDemo";
- private const string SessionKey = "DevilLink2.Task04B.ReadyV1";
- private static TestRunnerApi runner;
- static DevilLink2BoardSetup() { EditorApplication.delayCall += AutoSetup; }
- private static void AutoSetup()
- {
- if (EditorApplication.isPlayingOrWillChangePlaymode || SessionState.GetBool(SessionKey, false) ||
- File.Exists("Temp/DevilLink2BoardValidation/Task04BResults.xml")) return;
- if (EditorApplication.isCompiling || EditorApplication.isUpdating)
- { EditorApplication.delayCall += AutoSetup; return; }
- SessionState.SetBool(SessionKey, true);
- try
- {
- SetupInteraction();
- SetupInteraction();
- EditorApplication.delayCall += RunValidation;
- }
- catch (Exception e) { Debug.LogError("[DevilLink2.Task04B] SETUP BLOCKED: " + e); }
- }
- [MenuItem("Tools/Devil Link 2/Set Up Board Interaction")]
- public static void SetupInteraction()
- {
- Setup();
- var scene = SceneManager.GetActiveScene(); var root = FindRoot(scene);
- if (!root.GetComponent<BoardInteractionController>())
- {
- root.AddComponent<BoardInteractionController>();
- EditorSceneManager.MarkSceneDirty(scene); EditorSceneManager.SaveScene(scene);
- }
- Debug.Log("[DevilLink2.Task04B] Interaction configured; existing layout retained.");
- }
- [MenuItem("Tools/Devil Link 2/Set Up Board Demo")]
- public static void Setup()
- {
- if (EditorApplication.isPlayingOrWillChangePlaymode) throw new InvalidOperationException("Exit Play Mode before setup.");
- var scene = SceneManager.GetActiveScene();
- if (scene.path != ScenePath || scene.isDirty)
- throw new InvalidOperationException("Setup requires the saved Main scene active, with no pending scene edits. No other scene will be opened or saved.");
- var existing = FindRoot(scene);
- if (existing)
- {
- if (!AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath) || !existing.GetComponentInChildren<BoardView>(true))
- throw new InvalidOperationException("Existing demo is incomplete; inspect it before running setup again.");
- Debug.Log("[DevilLink2.Task04B] SETUP idempotence: existing root and prefab retained.");
- return;
- }
- EnsureFolder("Assets/DevilLink2/Prefabs", "Gameplay");
- var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
- if (!prefab)
- {
- var tile = Rect("TileView", null);
- try
- {
- var outline = tile.gameObject.AddComponent<Image>(); outline.color = new Color32(255, 211, 77, 255); outline.raycastTarget = false;
- tile.sizeDelta = new Vector2(42, 42);
- var face = Rect("Face", tile); Stretch(face, 1, 1, 1, 1);
- var image = face.gameObject.AddComponent<Image>(); image.color = new Color32(26, 35, 56, 255); image.raycastTarget = false;
- var label = MakeText("Symbol", face, "IMP", 11); Stretch((RectTransform)label.transform, 1, 1, 1, 1);
- tile.gameObject.AddComponent<TileView>().Configure(image, outline, label);
- prefab = PrefabUtility.SaveAsPrefabAsset(tile.gameObject, PrefabPath);
- if (!prefab) throw new InvalidOperationException("Tile prefab could not be saved.");
- }
- finally { UnityEngine.Object.DestroyImmediate(tile.gameObject); }
- }
- var root = Rect(RootName, null);
- SceneManager.MoveGameObjectToScene(root.gameObject, scene);
- var canvas = root.gameObject.AddComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay;
- var scaler = root.gameObject.AddComponent<CanvasScaler>(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
- scaler.referenceResolution = new Vector2(430, 932); scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
- root.gameObject.AddComponent<GraphicRaycaster>();
- var background = Rect("Background", root); Stretch(background);
- var bg = background.gameObject.AddComponent<Image>(); bg.color = new Color32(5, 8, 15, 255); bg.raycastTarget = false;
- var safe = Rect("SafeArea", root); Stretch(safe); safe.gameObject.AddComponent<SafeAreaAdapter>();
- var frame = Rect("PortraitFrame", safe); frame.anchorMin = frame.anchorMax = frame.pivot = new Vector2(.5f, .5f);
- var aspect = frame.gameObject.AddComponent<AspectRatioFitter>(); aspect.aspectMode = AspectRatioFitter.AspectMode.FitInParent; aspect.aspectRatio = 430f / 932;
- var frameImage = frame.gameObject.AddComponent<Image>(); frameImage.color = new Color32(13, 18, 32, 255); frameImage.raycastTarget = false;
- var title = MakeText("Title", frame, "DEVIL LINK 2 / CLASSIC 01", 15);
- var titleRect = (RectTransform)title.transform; titleRect.anchorMin = new Vector2(0, 1); titleRect.anchorMax = Vector2.one;
- titleRect.pivot = new Vector2(.5f, 1); titleRect.sizeDelta = new Vector2(0, 48); titleRect.anchoredPosition = Vector2.zero;
- var viewport = Rect("BoardViewport", frame); Stretch(viewport, 6, 12, 6, 60);
- var hit = viewport.gameObject.AddComponent<Image>(); hit.color = new Color(0, 0, 0, 0); hit.raycastTarget = true;
- viewport.gameObject.AddComponent<RectMask2D>();
- var scroll = viewport.gameObject.AddComponent<ScrollRect>(); scroll.viewport = viewport;
- scroll.horizontal = false; scroll.vertical = true; scroll.movementType = ScrollRect.MovementType.Clamped;
- var content = Rect("Content", viewport); content.anchorMin = new Vector2(0, 1); content.anchorMax = Vector2.one;
- content.pivot = new Vector2(.5f, 1); content.anchoredPosition = Vector2.zero; content.sizeDelta = Vector2.zero;
- scroll.content = content;
- var board = Rect("Board", content);
- var panel = board.gameObject.AddComponent<Image>(); panel.color = new Color32(5, 8, 15, 255); panel.raycastTarget = false;
- var view = viewport.gameObject.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(), viewport, content, board);
- root.gameObject.AddComponent<BoardDemoBootstrap>().Configure(view);
- if (!UnityEngine.Object.FindFirstObjectByType<EventSystem>())
- {
- var events = new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule));
- events.transform.SetParent(root, false);
- }
- Canvas.ForceUpdateCanvases();
- EditorSceneManager.MarkSceneDirty(scene);
- if (!EditorSceneManager.SaveScene(scene)) throw new InvalidOperationException("Main could not be saved.");
- Debug.Log("[DevilLink2.Task04B] SETUP COMPLETE: Main + TileView.prefab. Runtime board: 4 columns x 8 rows, seed 2026.");
- }
- private static GameObject FindRoot(Scene scene)
- {
- GameObject found = null;
- foreach (var root in scene.GetRootGameObjects()) if (root.name == RootName)
- { if (found) throw new InvalidOperationException("Duplicate managed demo roots."); found = root; }
- return found;
- }
- private static void EnsureFolder(string parent, string name)
- { if (!AssetDatabase.IsValidFolder(parent + "/" + name)) AssetDatabase.CreateFolder(parent, name); }
- private static RectTransform Rect(string name, Transform parent)
- {
- var rect = (RectTransform)new GameObject(name, typeof(RectTransform)).transform;
- if (parent) rect.SetParent(parent, false);
- return rect;
- }
- private static void Stretch(RectTransform rect, float left = 0, float bottom = 0, float right = 0, float top = 0)
- { rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.offsetMin = new Vector2(left, bottom); rect.offsetMax = new Vector2(-right, -top); }
- private static Text MakeText(string name, Transform parent, string value, int size)
- {
- var text = Rect(name, parent).gameObject.AddComponent<Text>();
- text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf"); text.text = value; text.fontSize = size;
- text.alignment = TextAnchor.MiddleCenter; text.color = new Color32(220, 230, 247, 255); text.raycastTarget = false;
- return text;
- }
- [MenuItem("Tools/Devil Link 2/Validate Board Demo")]
- public static void RunValidation()
- {
- runner = ScriptableObject.CreateInstance<TestRunnerApi>(); runner.RegisterCallbacks(new Results());
- runner.Execute(new ExecutionSettings(new Filter { testMode = TestMode.EditMode,
- assemblyNames = new[] { "DevilLink2.Core.Tests", "DevilLink2.Presentation.Tests", "DevilLink2.Interaction.Tests" } }));
- }
- private sealed class Results : ICallbacks
- {
- public void RunStarted(ITestAdaptor testsToRun) { Debug.Log("[DevilLink2.Task04B] TESTS STARTED"); }
- public void RunFinished(ITestResultAdaptor result)
- {
- Directory.CreateDirectory("Temp/DevilLink2BoardValidation");
- TestRunnerApi.SaveResultToFile(result, "Temp/DevilLink2BoardValidation/Task04BResults.xml");
- Debug.Log($"[DevilLink2.Task04B] TESTS FINISHED passed={result.PassCount} failed={result.FailCount} skipped={result.SkipCount}");
- runner.UnregisterCallbacks(this); UnityEngine.Object.DestroyImmediate(runner);
- }
- public void TestStarted(ITestAdaptor test) { }
- public void TestFinished(ITestResultAdaptor result)
- { if (result.TestStatus == TestStatus.Failed) Debug.LogError("[DevilLink2.Task04B] TEST FAILED " + result.FullName + " " + result.Message + " " + result.StackTrace); }
- }
- }
- }
|