Adventure Creator Wikia
Advertisement

This custom Action allows you to change a UI Text component's label. Note that normally you'll want to link a UI Text component to the Menu Manager as a Label element, but this method allows you to control a UI Text without doing so.

The Text and Label fields can both be overridden with ActionList parameters.


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

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

   [System.Serializable]
   public class ActionTextLabel : Action
   {

       public Text text;
       public int textConstantID = 0;
       public int textParameterID = -1;

       public string newLabel;
       public int newLabelParameterID = -1;

       public override ActionCategory Category { get { ActionCategory.Menu; } }
       public override string Title { get { return "Update text label"; } }
       public override string Description { get { return "Changes the contents of a UI Text box."; } }

       public override void AssignValues (List<ActionParameter> parameters)
       {
           newLabel = AssignString (parameters, newLabelParameterID, newLabel);
           text = AssignFile <Text> (parameters, textParameterID, textConstantID, text);
       }

       override public float Run ()
       {
           if (text)
           {
               text.text = newLabel;
           }
           return 0f;
       }

       #if UNITY_EDITOR

       override public void ShowGUI (List<ActionParameter> parameters)
       {
           textParameterID = Action.ChooseParameterGUI ("Text component:", parameters, textParameterID, ParameterType.GameObject);
           if (textParameterID >= 0)
           {
               textConstantID = 0;
               text = null;
           }
           else
           {
               text = (Text) EditorGUILayout.ObjectField ("Text component:", text, typeof (Text), true);

               textConstantID = FieldToID (text, textConstantID);
               text = IDToField (text, textConstantID, false);
           }

           newLabelParameterID = Action.ChooseParameterGUI ("FSM to call (optional):", parameters, newLabelParameterID, ParameterType.String);
           if (newLabelParameterID < 0)
           {
               newLabel = EditorGUILayout.TextField ("New label:", newLabel);
           }
       }

       #endif

   }

}

Advertisement