示例#1
0
        public static void AddButton(MenuCommand menuCommand)
        {
            GameObject go = TMP_DefaultControls.CreateButton(GetStandardResources());

            // Override font size
            TMP_Text textComponent = go.GetComponentInChildren <TMP_Text>();

            textComponent.fontSize = 24;

            PlaceUIElementRoot(go, menuCommand);
        }
示例#2
0
    static void CreateMenuScene()
    {
        EditorSceneManager.SaveOpenScenes();

        Scene menuScene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);

        menuScene.name = "Menu";

        GameObject canvasObj = new GameObject("Canvas");
        Canvas     canvas    = canvasObj.AddComponent <Canvas>();

        canvas.renderMode = RenderMode.ScreenSpaceOverlay;

        CreateEventSystem(false, null);

        CanvasScaler canvasScaler = canvasObj.AddComponent <CanvasScaler>();

        canvasScaler.uiScaleMode         = CanvasScaler.ScaleMode.ScaleWithScreenSize;
        canvasScaler.referenceResolution = new Vector2Int(951, 535);
        canvasScaler.matchWidthOrHeight  = 1f;

        canvasObj.AddComponent <GraphicRaycaster>();

        GameObject qualityDropdown = TMP_DefaultControls.CreateDropdown(GetStandardResources());

        qualityDropdown.transform.SetParent(canvasObj.transform);
        RectTransform qualityRectTransform = qualityDropdown.GetComponent <RectTransform>();

        qualityRectTransform.anchoredPosition = Vector2.up * 15f;
        qualityDropdown.name = "QualityDropdown";

        GameObject resolutionDropdown = TMP_DefaultControls.CreateDropdown(GetStandardResources());

        resolutionDropdown.transform.SetParent(canvasObj.transform);
        RectTransform resolutionRectTransform = resolutionDropdown.GetComponent <RectTransform>();

        resolutionRectTransform.anchoredPosition = Vector2.down * 15f;
        resolutionDropdown.name = "ResolutionDropdown";

        GameObject musicSlider;

        using (new FactorySwapToEditor())
            musicSlider = DefaultControls.CreateSlider(GetStandardUIResources());
        musicSlider.transform.SetParent(canvasObj.transform);
        RectTransform musicRectTransform = musicSlider.GetComponent <RectTransform>();

        musicRectTransform.anchoredPosition = Vector2.down * 40f;
        musicSlider.name = "MusicSlider";

        GameObject sfxSlider;

        using (new FactorySwapToEditor())
            sfxSlider = DefaultControls.CreateSlider(GetStandardUIResources());
        sfxSlider.transform.SetParent(canvasObj.transform);
        RectTransform sfxRectTransform = sfxSlider.GetComponent <RectTransform>();

        sfxRectTransform.anchoredPosition = Vector2.down * 60;
        sfxSlider.name = "MusicSlider";

        GameObject playButton = TMP_DefaultControls.CreateButton(GetStandardResources());

        playButton.transform.SetParent(canvasObj.transform);
        TMP_Text playTextComponent = playButton.GetComponentInChildren <TMP_Text>();

        playTextComponent.fontSize = 24;
        playTextComponent.text     = "PLAY";
        RectTransform playRectTransform = playButton.GetComponent <RectTransform>();

        playRectTransform.anchoredPosition = Vector2.up * 45f;
        playButton.name = "PlayButton";

        GameObject quitButton = TMP_DefaultControls.CreateButton(GetStandardResources());

        quitButton.transform.SetParent(canvasObj.transform);
        TMP_Text quitTextComponent = quitButton.GetComponentInChildren <TMP_Text>();

        quitTextComponent.fontSize = 24;
        quitTextComponent.text     = "QUIT";
        RectTransform quitRectTransform = quitButton.GetComponent <RectTransform>();

        quitRectTransform.anchoredPosition = Vector2.down * 85f;
        quitButton.name = "QuitButton";

        MenuController menuController     = canvasObj.AddComponent <MenuController>();
        Slider         sliderMusic        = menuController.musicSlider = musicSlider.GetComponent <Slider>();
        Slider         sliderSfx          = menuController.sfxSlider = sfxSlider.GetComponent <Slider>();
        TMP_Dropdown   dropdownQuality    = menuController.qualityDropdown = qualityDropdown.GetComponent <TMP_Dropdown>();
        TMP_Dropdown   dropdownResolution = menuController.resolutionDropdown = resolutionDropdown.GetComponent <TMP_Dropdown>();

        sliderMusic.onValueChanged.AddListener(menuController.SetMusicVolume);
        sliderSfx.onValueChanged.AddListener(menuController.SetSfxVolume);
        dropdownQuality.onValueChanged.AddListener(menuController.SetQuality);
        dropdownResolution.onValueChanged.AddListener(menuController.SetResolution);

        playButton.GetComponent <Button>().onClick.AddListener(menuController.Play);
        quitButton.GetComponent <Button>().onClick.AddListener(menuController.Quit);
    }