From 784d75a8831a4af3b3e83da93981b89f6095af18 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 14 Aug 2015 11:44:46 +0100 Subject: [PATCH] Get Text and Set Text now work on any text object including UI text, UI input fields & 3D text mesh --- .../Flowchart/Scripts/Commands/GetText.cs | 57 ------------ .../Flowchart/Scripts/Commands/SetText.cs | 47 ---------- Assets/Fungus/UI/Scripts/Commands/GetText.cs | 93 +++++++++++++++++++ .../Scripts/Commands/GetText.cs.meta | 0 Assets/Fungus/UI/Scripts/Commands/SetText.cs | 87 +++++++++++++++++ .../Scripts/Commands/SetText.cs.meta | 0 6 files changed, 180 insertions(+), 104 deletions(-) delete mode 100644 Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs delete mode 100644 Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs create mode 100644 Assets/Fungus/UI/Scripts/Commands/GetText.cs rename Assets/Fungus/{Flowchart => UI}/Scripts/Commands/GetText.cs.meta (100%) create mode 100644 Assets/Fungus/UI/Scripts/Commands/SetText.cs rename Assets/Fungus/{Flowchart => UI}/Scripts/Commands/SetText.cs.meta (100%) diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs b/Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs deleted file mode 100644 index 749d0874..00000000 --- a/Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs +++ /dev/null @@ -1,57 +0,0 @@ -using UnityEngine; -using UnityEngine.UI; -using System; -using System.Collections; - -namespace Fungus -{ - [CommandInfo("Scripting", - "Get Text", - "Gets the text property from a UI Text object and stores it in a string variable.")] - - [AddComponentMenu("")] - public class GetText : Command - { - [Tooltip("Text object to get text value from")] - public Text textObject; - - [Tooltip("String variable to store the text value in")] - [VariableProperty(typeof(StringVariable))] - public Variable variable; - - public override void OnEnter() - { - if (textObject != null) - { - StringVariable stringVariable = variable as StringVariable; - if (stringVariable != null) - { - stringVariable.value = textObject.text; - } - } - - Continue(); - } - - public override string GetSummary() - { - if (textObject == null) - { - return "Error: No text object selected"; - } - - if (variable == null) - { - return "Error: No variable selected"; - } - - return textObject.name + " : " + variable.name; - } - - public override Color GetButtonColor() - { - return new Color32(235, 191, 217, 255); - } - } - -} \ No newline at end of file diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs b/Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs deleted file mode 100644 index ba996584..00000000 --- a/Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs +++ /dev/null @@ -1,47 +0,0 @@ -using UnityEngine; -using UnityEngine.UI; -using System; -using System.Collections; - -namespace Fungus -{ - [CommandInfo("Scripting", - "Set Text", - "Sets the text property on a UI Text object.")] - - [AddComponentMenu("")] - public class SetText : Command - { - [Tooltip("Text object to set text on")] - public Text textObject; - - [Tooltip("String value to assign to the text object")] - public StringData stringData; - - public override void OnEnter() - { - if (textObject != null) - { - textObject.text = stringData.Value; - } - - Continue(); - } - - public override string GetSummary() - { - if (textObject == null) - { - return "Error: No text object selected"; - } - - return textObject.name + " : " + stringData.Value; - } - - public override Color GetButtonColor() - { - return new Color32(235, 191, 217, 255); - } - } - -} \ No newline at end of file diff --git a/Assets/Fungus/UI/Scripts/Commands/GetText.cs b/Assets/Fungus/UI/Scripts/Commands/GetText.cs new file mode 100644 index 00000000..d4c19e79 --- /dev/null +++ b/Assets/Fungus/UI/Scripts/Commands/GetText.cs @@ -0,0 +1,93 @@ +using UnityEngine; +using UnityEngine.UI; +using System; +using System.Collections; +using UnityEngine.Serialization; + +namespace Fungus +{ + [CommandInfo("UI", + "Get Text", + "Gets the text property from a UI Text object and stores it in a string variable.")] + + [AddComponentMenu("")] + public class GetText : Command + { + [Tooltip("Text object to get text value from")] + public GameObject targetTextObject; + + [Tooltip("String variable to store the text value in")] + [VariableProperty(typeof(StringVariable))] + public StringVariable stringVariable; + + public override void OnEnter() + { + if (stringVariable == null) + { + Continue(); + return; + } + + if (targetTextObject != null) + { + // Use first component found of Text, Input Field or Text Mesh type + Text uiText = targetTextObject.GetComponent(); + if (uiText != null) + { + stringVariable.value = uiText.text; + } + else + { + InputField inputField = targetTextObject.GetComponent(); + if (inputField != null) + { + stringVariable.value = inputField.text; + } + else + { + TextMesh textMesh = targetTextObject.GetComponent(); + if (textMesh != null) + { + stringVariable.value = textMesh.text; + } + } + } + } + + Continue(); + } + + public override string GetSummary() + { + if (targetTextObject == null) + { + return "Error: No text object selected"; + } + + if (stringVariable == null) + { + return "Error: No variable selected"; + } + + return targetTextObject.name + " : " + stringVariable.name; + } + + public override Color GetButtonColor() + { + return new Color32(235, 191, 217, 255); + } + + // Backwards compatibility with Fungus v2.1.2 + [HideInInspector] + [FormerlySerializedAs("textObject")] + public Text _textObjectObsolete; + protected virtual void OnEnable() + { + if (_textObjectObsolete != null) + { + targetTextObject = _textObjectObsolete.gameObject; + } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs.meta b/Assets/Fungus/UI/Scripts/Commands/GetText.cs.meta similarity index 100% rename from Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs.meta rename to Assets/Fungus/UI/Scripts/Commands/GetText.cs.meta diff --git a/Assets/Fungus/UI/Scripts/Commands/SetText.cs b/Assets/Fungus/UI/Scripts/Commands/SetText.cs new file mode 100644 index 00000000..084995e4 --- /dev/null +++ b/Assets/Fungus/UI/Scripts/Commands/SetText.cs @@ -0,0 +1,87 @@ +using UnityEngine; +using UnityEngine.UI; +using System; +using System.Collections; +using UnityEngine.Serialization; + +namespace Fungus +{ + [CommandInfo("UI", + "Set Text", + "Sets the text property on a UI Text object and/or an Input Field object.")] + + [AddComponentMenu("")] + public class SetText : Command + { + [Tooltip("Text object to set text on. Can be a UI Text, Text Field or Text Mesh object.")] + public GameObject targetTextObject; + + [Tooltip("String value to assign to the text object")] + public StringData stringData; + + public override void OnEnter() + { + Flowchart flowchart = GetFlowchart(); + string newText = flowchart.SubstituteVariables(stringData.Value); + + if (targetTextObject == null) + { + Continue(); + return; + } + + // Use first component found of Text, Input Field or Text Mesh type + Text uiText = targetTextObject.GetComponent(); + if (uiText != null) + { + uiText.text = newText; + } + else + { + InputField inputField = targetTextObject.GetComponent(); + if (inputField != null) + { + inputField.text = newText; + } + else + { + TextMesh textMesh = targetTextObject.GetComponent(); + if (textMesh != null) + { + textMesh.text = newText; + } + } + } + + Continue(); + } + + public override string GetSummary() + { + if (targetTextObject != null) + { + return targetTextObject.name + " : " + stringData.Value; + } + + return "Error: No text object selected"; + } + + public override Color GetButtonColor() + { + return new Color32(235, 191, 217, 255); + } + + // Backwards compatibility with Fungus v2.1.2 + [HideInInspector] + [FormerlySerializedAs("textObject")] + public Text _textObjectObsolete; + protected virtual void OnEnable() + { + if (_textObjectObsolete != null) + { + targetTextObject = _textObjectObsolete.gameObject; + } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs.meta b/Assets/Fungus/UI/Scripts/Commands/SetText.cs.meta similarity index 100% rename from Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs.meta rename to Assets/Fungus/UI/Scripts/Commands/SetText.cs.meta