示例#1
0
        static void AddTextMeshProInputField(MenuCommand menuCommand)

        {
            GameObject go = TMP_DefaultControls.CreateInputField(GetStandardResources());

            PlaceUIElementRoot(go, menuCommand);
        }
示例#2
0
        static public void AddDropdown(MenuCommand menuCommand)

        {
            GameObject go = TMP_DefaultControls.CreateDropdown(GetStandardResources());

            PlaceUIElementRoot(go, menuCommand);
        }
示例#3
0
        static void CreateTextMeshProGuiObjectPerform(MenuCommand menuCommand)
        {
            GameObject go = TMP_DefaultControls.CreateText(GetStandardResources());

            // Override text color and font size
            TextMeshProUGUI textComponent = go.GetComponent <TextMeshProUGUI>();

            if (textComponent.m_isWaitingOnResourceLoad == false)
            {
                // Apply TMP Settings Defaults if no Preset is defined
                if (Preset.GetDefaultForObject(textComponent) == null)
                {
                    textComponent.fontSize = TMP_Settings.defaultFontSize;
                    textComponent.color    = Color.white;
                    textComponent.text     = "New Text";
                }

                if (TMP_Settings.autoSizeTextContainer)
                {
                    Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
                    textComponent.rectTransform.sizeDelta = size;
                }
                else
                {
                    textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProUITextContainerSize;
                }
            }

            PlaceUIElementRoot(go, menuCommand);
        }
示例#4
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);
        }
示例#5
0
            public InputFieldTextMeshPro(TextComp comp, UnityAction <string> editEndCallback)
            {
                this.linkedTextComp = comp;

                TMP_DefaultControls.Resources resources = new TMP_DefaultControls.Resources();
                inputField = TMP_DefaultControls.CreateInputField(resources);
                inputField.transform.SetParent(comp.GetTransform(), false);
                RectTransform inputFieldRectTrans = inputField.transform.GetComponent <RectTransform>();

                inputFieldRectTrans.anchorMin = Vector2.zero;
                inputFieldRectTrans.anchorMax = Vector2.one;
                inputFieldRectTrans.offsetMin = Vector2.zero;
                inputFieldRectTrans.offsetMax = Vector2.zero;

                tmpInputFieldComp = inputField.GetComponent <TMP_InputField>();
                UnityAction <string> action = new UnityAction <string>(UpdateTextWhileTyping);

                tmpInputFieldComp.onValueChanged.AddListener(action);

                // Copy text into input field
                tmpInputFieldComp.text = comp.GetText();

                tmpInputFieldComp.lineType = TMP_InputField.LineType.MultiLineNewline;

                // Disable background image
                inputField.GetComponent <Image>().enabled = false;

                // Create preset of text component property values
                TextMeshProUGUI textComp               = inputField.transform.Find("Text Area").Find("Text").GetComponent <TextMeshProUGUI>();
                TextMeshProUGUI placeholderTextComp    = inputField.transform.Find("Text Area").Find("Placeholder").GetComponent <TextMeshProUGUI>();
                string          placeholderEmptyString = "Enter text...";
                Preset          textCompPreset         = (comp as TextCompTextMeshProUGUI).GetPresetOfInternalComp();
                bool            success = textCompPreset.ApplyTo(textComp);

                success = textCompPreset.ApplyTo(placeholderTextComp);
                placeholderTextComp.text    = placeholderEmptyString;
                placeholderTextComp.enabled = false;

                // Reset "Text Area" RectTransform component
                RectTransform textAreaRectTransform = inputField.transform.Find("Text Area").GetComponent <RectTransform>();
                Preset        rectTransformResetter = new Preset(inputField.transform.Find("Text Area").Find("Text").GetComponent <RectTransform>());

                success = rectTransformResetter.ApplyTo(textAreaRectTransform);

                TMP_InputField inputFieldComp = inputField.GetComponent <TMP_InputField>();

                inputFieldComp.onEndEdit.AddListener(editEndCallback);

                // @NOTE This fixes caret not spawned when creating TextMeshPro input field during runtime.
                tmpInputFieldComp.enabled = false;
                tmpInputFieldComp.enabled = true;
            }
示例#6
0
        static void CreateTextMeshProGuiObjectPerform(MenuCommand menuCommand)
        {
            GameObject go = TMP_DefaultControls.CreateText(GetStandardResources());

            // Override text color and font size
            TextMeshProUGUI textComponent = go.GetComponent <TextMeshProUGUI>();

            if (textComponent.m_isWaitingOnResourceLoad == false)
            {
                // Get reference to potential Presets for <TextMeshProUGUI> component
                #if UNITY_2019_3_OR_NEWER
                Preset[] presets = Preset.GetDefaultPresetsForObject(textComponent);

                if (presets == null || presets.Length == 0)
                {
                    textComponent.fontSize = TMP_Settings.defaultFontSize;
                    textComponent.color    = Color.white;
                    textComponent.text     = "New Text";
                }
                #else
                if (Preset.GetDefaultForObject(textComponent) == null)
                {
                    textComponent.fontSize = TMP_Settings.defaultFontSize;
                    textComponent.color    = Color.white;
                    textComponent.text     = "New Text";
                }
                #endif

                if (TMP_Settings.autoSizeTextContainer)
                {
                    Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
                    textComponent.rectTransform.sizeDelta = size;
                }
                else
                {
                    textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProUITextContainerSize;
                }

                //默认关闭设置
                textComponent.richText      = false;
                textComponent.raycastTarget = false;
            }
            else
            {
                textComponent.fontSize = -99;
                textComponent.color    = Color.white;
                textComponent.text     = "New Text";
            }

            PlaceUIElementRoot(go, menuCommand);
        }
示例#7
0
        static void CreateTextMeshProGuiObjectPerform(MenuCommand menuCommand)
        {
            GameObject go = TMP_DefaultControls.CreateText(GetStandardResources());

            // Override text color and font size
            TMP_Text textComponent = go.GetComponent <TMP_Text>();

            textComponent.color = Color.white;
            if (textComponent.m_isWaitingOnResourceLoad == false)
            {
                textComponent.fontSize = TMP_Settings.defaultFontSize;
            }

            PlaceUIElementRoot(go, menuCommand);
        }
示例#8
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);
    }