using System.Reflection; using HutongGames.PlayMaker; using HutongGames.PlayMaker.Actions; using MSCLoader; using UnityEngine; namespace MSCO { /// Hilfsfunktionen rund um Hauptmenü und das Laden der Spielszene. public static class MenuHelper { public const string MenuScene = "MainMenu"; public const string GameScene = "GAME"; public static bool InMenu { get { return Application.loadedLevelName == MenuScene; } } public static bool InGame { get { return Application.loadedLevelName == GameScene; } } static readonly MethodInfo switchState = typeof(Fsm).GetMethod("SwitchState", BindingFlags.NonPublic | BindingFlags.Instance); /// /// Sucht im Hauptmenü die PlayMaker-FSM des CONTINUE-Buttons und den Zustand, /// der per LoadLevel die Szene "GAME" lädt. /// public static bool FindContinueLoadState(out PlayMakerFSM fsm, out FsmState state) { fsm = null; state = null; foreach (Object o in Object.FindObjectsOfType(typeof(PlayMakerFSM))) { var candidate = (PlayMakerFSM)o; if (!HierarchyContains(candidate.transform, "continue")) continue; foreach (FsmState s in candidate.FsmStates) { if (!s.ActionsLoaded) s.LoadActions(); if (s.Actions == null) continue; foreach (FsmStateAction action in s.Actions) { var load = action as LoadLevel; if (load != null && load.levelName != null && load.levelName.Value == GameScene) { fsm = candidate; state = s; return true; } } } } return false; } /// Startet das Spiel wie ein Klick auf CONTINUE. // Knöpfe des Spiel-Hauptmenüs, die MSCO ersetzt (der MSCO-Launcher ist das Hauptmenü) static readonly string[] HiddenMenuButtons = { "ButtonContinue", "ButtonNewgame", "ButtonCredits", "ButtonQuit", "ButtonChangelog" }; /// /// Continue, New Game, Credits, Changelog und Quit des Spiels unsichtbar und unklickbar machen. Nicht abschalten: /// die Logik hinter CONTINUE lädt beim Beitreten den übertragenen Spielstand (StartGame). Gibt die Anzahl zurück. /// public static int HideGameMenuButtons() { int hidden = 0; foreach (string name in HiddenMenuButtons) { GameObject button = GameObject.Find(name); if (button == null) continue; foreach (Renderer r in button.GetComponentsInChildren(true)) r.enabled = false; foreach (Collider c in button.GetComponentsInChildren(true)) c.enabled = false; hidden++; } return hidden; } public static void StartGame() { PlayMakerFSM fsm; FsmState state; if (switchState != null && FindContinueLoadState(out fsm, out state)) { MscoLog.Info("[MSCO] Loading via CONTINUE (" + fsm.name + " / " + state.Name + ")"); switchState.Invoke(fsm.Fsm, new object[] { state }); } else { MscoLog.Warning("[MSCO] CONTINUE button not found, loading GAME directly"); Application.LoadLevel(GameScene); } } static bool HierarchyContains(Transform t, string text) { for (; t != null; t = t.parent) if (t.name.ToLower().Contains(text)) return true; return false; } } }