superluig164

Untitled

Feb 15th, 2022
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. public class TextFieldController : MonoBehaviour
  2. {
  3.     public GameObject GameControllerReference; //Reference to the GameController.  Must be defined in the Inspector.
  4.  
  5.     public List<string> FieldsToDisplay; //A list of the fields to display in this text element.  They will be separated by new lines.
  6.  
  7.     // Start is called before the first frame update
  8.     void Start()
  9.     {
  10.         if (GameControllerReference == null)
  11.             Debug.LogError("Game Controller reference not set on this TextController.");
  12.     }
  13.  
  14.     // Update is called once per frame
  15.     void Update()
  16.     {
  17.         string textToDisplay = ""; //The text we will eventually display will be stored in this.
  18.  
  19.         if (FieldsToDisplay[0] == "score") //If the first field is "score"...
  20.             textToDisplay = "Score: " + GameControllerReference.GetComponent<GameController>().GetScore().ToString(); //Ignore everything else and use this text box as a score display.
  21.         else //Otherwise...
  22.         {
  23.             foreach (string field in FieldsToDisplay) //For each field that we want to display...
  24.             {
  25.                 foreach (string type in GameControllerReference.GetComponent<GameController>().GetProfileFields()) //For each type of field...
  26.                 {
  27.                     if (field.Equals(type)) //If this is the field we need...
  28.                     {
  29.                         int index = GameControllerReference.GetComponent<GameController>().GetProfileFields().IndexOf(field); //Get the index of this type of field.
  30.  
  31.                         textToDisplay = textToDisplay + GameControllerReference.GetComponent<GameController>().GetCurrentProfile()[index] + "\n"; //Use that index to get the value from the current profile, and append it to the text to display, plus a new line.
  32.                     }
  33.                 }
  34.  
  35.                 if (field.Equals("all")) //If any field is "all"...
  36.                 {
  37.                     textToDisplay = textToDisplay + GameControllerReference.GetComponent<GameController>().GetCurrentProfileString() + "\n"; //Dump all the fields at this position.
  38.                 }
  39.             }
  40.         }
  41.  
  42.         gameObject.GetComponent<TextMesh>().text = textToDisplay; //Change the text of the TextMesh to match what we want to display.
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment