From 30a72433ee5f475df359bf08362e18c49264603e Mon Sep 17 00:00:00 2001 From: Hobbeslionheart Date: Sat, 2 Apr 2022 08:13:39 -0400 Subject: [PATCH] Added SetActiveState and Set Color Methods Added two public methods, SetActiveState and SetColor methods. SetActiveState should make it easier to toggle and untoggle buttons depending on the condition. SetColor detaches the color setting from the text making it more modular and flexible. --- unity/Assets/Scripts/UI/UIElement.cs | 56 ++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/unity/Assets/Scripts/UI/UIElement.cs b/unity/Assets/Scripts/UI/UIElement.cs index 5b13d0af8..f42f4e8a7 100644 --- a/unity/Assets/Scripts/UI/UIElement.cs +++ b/unity/Assets/Scripts/UI/UIElement.cs @@ -2,6 +2,8 @@ using Assets.Scripts.Content; using UnityEditor; using UnityEngine.UI; +using System; +using UnityEngine.Events; namespace Assets.Scripts.UI { @@ -52,7 +54,7 @@ public UIElement(Transform parent) public void Destroy() { if(bg!=null) - Object.Destroy(bg); + UnityEngine.Object.Destroy(bg); } /// @@ -136,7 +138,7 @@ public void SetImage(Texture2D texture, { if (!(fitter is null)) { - Object.Destroy(fitter); + UnityEngine.Object.Destroy(fitter); } return; } @@ -291,6 +293,11 @@ public void SetText(string content) SetText(content, Color.white); } + public void SetColor(Color textColor) + { + text.GetComponent().color = textColor; + } + /// /// Set element text. /// Text to display. @@ -517,7 +524,7 @@ public float GetStringWidth(string content, int fontSize, Font fontName) } textWidthObj.GetComponent().text = content; width = (textWidthObj.GetComponent().preferredWidth / UIScaler.GetPixelsPerUnit()) + (textPaddingDefault * 2); - Object.Destroy(textWidthObj); + UnityEngine.Object.Destroy(textWidthObj); textWidthObj = null; return width; } @@ -554,7 +561,7 @@ public float GetStringHeight(string content, float width) } textHeightObj.GetComponent().text = content; height = (textHeightObj.GetComponent().preferredHeight / UIScaler.GetPixelsPerUnit()) + (textPaddingDefault * 2); - Object.Destroy(textHeightObj); + UnityEngine.Object.Destroy(textHeightObj); textHeightObj = null; return height; } @@ -642,9 +649,50 @@ public virtual float HeightToTextPadding(float space = 0) return newHeight; } + /// + /// Sets the GameObject Name in Unity. Helps with debugging. + /// + /// The GameObject Name to name public void SetGameObjectName(string name) { bg.name = name; } + + /// + /// Sets the condition for the state toggle. UIElementBorder is created here as the color will change depending on the active state. + /// + /// Condition to assess + /// The button state if the condition parameter is true. True for active, false for inactive + /// Unity Action to assign to the button when active + public void SetActiveState(bool condition, bool activestateifconditiontrue, UnityAction action = null) + { + //if Condition is true or is met + if (condition) + { + if (activestateifconditiontrue) + { + SetButton(action); + new UIElementBorder(this); + } + else + { + SetColor(Color.gray); + new UIElementBorder(this, Color.gray); + } + } + else + { + if (activestateifconditiontrue) + { + SetColor(Color.gray); + new UIElementBorder(this, Color.gray); + } + else + { + SetButton(action); + new UIElementBorder(this); + } + } + } } }