Forum rules - please read before posting.

Possible to save current text in Text Mesh Pro Text Input field

edited August 2021 in Technical Q&A

Hello!

First off, using AC 1.73.8, and Unity 2021.1.7f1, and all menus are made in Unity UI.

In my game, I made a custom dropdown menu (Unity UI prefab) that a player can click to expand it, then choose a selection by clicking one of the dropdown's fields / buttons. This menu is part of an interactive sequence / puzzle on a computer screen in the game -- it isn't for anything involving changing the game's settings, inventory, or anything like that.

Essentially it consists of a set of Unity UI buttons, and initially the buttons underneath the top-most button have an alpha of 0 -- and when the top button is clicked it just changes the other buttons' alpha to be visible (1).
The full menu looks like this when expanded:
https://imgur.com/a/x4kU4xo

The menu uses a custom script (shown below) to transfer what's in the "Text Input" field (of the "Text Mesh Pro" component) on the button that's clicked -- and then show that same text in the top-most button which also uses a "Text Mesh Pro" component (where the down-facing arrow symbol is shown in the screenshot). Then the bottom buttons disappear again, which creates a visual appearance like a dropdown menu would.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class ClickToChangeTextLeft : MonoBehaviour
{
    public TextMeshProUGUI textshowed = null;
    public void changeWord (string wordLeft)
    {
        textshowed.text = wordLeft;
    }   
}

The script is loosely similar in a very basic way to the Text Mesh pro Integration script on the Wiki, though of course this custom script is quite different.
The custom script is run via the clicked button's On Click () event, shown here:
https://imgur.com/a/orsXWQj

It all works good for the visual effect of a dropdown menu, and it functions nicely for the game story's progression using AC Actionlists and variables.

Though an issue came up when it comes to saving / loading. After one of the sub-menu buttons are clicked, that transferred text from the sub-buttons' Text Mesh Pro "Text Input" field (which then shows in the top-most button) isn't saved.
Instead when a game is loaded it just shows what text was there last in the game that was running before the save was loaded.

I mainly understand why this happens, though haven't been able to create a solution to it yet, and haven't come across information on this as far as the AC save/load system goes.

I was wondering if there were any thoughts on any more straightforward ways for doing this in AC that I haven't discovered yet.
Also, is saving what's in a Text Mesh Pro "Input Field" possible via AC?

Feeling this may fall under Saving custom global data. Though after reading the tutorial, I'm not clear on how a Text Mesh Pro "Test Input" field might be targeted and saved.

One other approach I thought of as a workaround for this was to have it just "reset" (clear) the text from the top-most button (and variables associated with it) in the "OnLoad" Actionlist. Not the most ideal, though I believe that could work fine -- if there's any further suggestions on how to go about resetting the text in AC.

Thank you for any assistance that can be provided! It's appreciated!

Comments

  • The approach covered in the tutorial would work, but things are complicated by the fact that a hidden menu is disabled in the Hierarchy, meaning you'd probably need a custom script to sit inside the scene on a separate GameObject.

    Perhaps a better approach would be to instead have your drop-down buttons update a Global String variable, and configure the variable to sync itself with the TMPro Text component.

    See the Manual's "Variable linking / Linking with custom scripts" chapter for details. With this approach, you'd first need to create a new Global String variable, and then update your above script to something like:

    using UnityEngine;
    using AC;
    
    public class ClickToChangeTextLeft : MonoBehaviour
    {
        public string variableToLinkTo = "MyVariable";
        public void changeWord (string wordLeft)
        {
            GlobalVariables.GetVariable (variableToLinkTo).TextValue = wordLeft;
        }
    }
    

    You can then use this separate script to sync the variable to the TMPro Text component:

    using UnityEngine;
    using AC;
    using TMPro;
    
    public class LinkTextToVariable : MonoBehaviour
    {
    
        public TextMeshProUGUI textshowed;
        public string variableToLinkTo = "MyVariable";
    
        private void OnEnable ()
        {
            textshowed.text = variable.TextValue;
            EventManager.OnDownloadVariable += OnDownload;
            EventManager.OnUploadVariable += OnUpload;
        }
    
        private void OnDisable ()
        {
            EventManager.OnDownloadVariable -= OnDownload;
            EventManager.OnUploadVariable -= OnUpload;
        }
    
        private void OnDownload (GVar variable, Variables variables)
        {
            if (variable.label == variableToLinkTo)
            {
                variable.TextValue = textshowed.text;
            }
        }
    
        private void OnUpload (GVar variable, Variables variables)
        {
            if (variable.label == variableToLinkTo)
            {
                textshowed.text = variable.TextValue;
            }
        }
    
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.