Advertisement
Guest User

UpdateDisplay

a guest
Apr 6th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using System.Diagnostics;
  6.  
  7. public class UpdateDisplay : MonoBehaviour
  8. {
  9.     // Start is called before the first frame update
  10.     TMP_Text thisText;
  11.     [SerializeField] InventoryObject inventoryToRead;
  12.     void Start()
  13.     {
  14.         PlayerInventory.OnItemPickedUp += UpdateDisplayText;
  15.         thisText = GetComponent<TMP_Text>();
  16.      
  17.     }
  18.    
  19.     // Update is called once per frame
  20.     void Update()
  21.     {
  22.        
  23.     }
  24.     void UpdateDisplayText(InventoryObject _inventory)
  25.     {
  26.         UnityEngine.Debug.Log(GetClassAndMethod());
  27.         UnityEngine.Debug.Log("UpdateDisplayText() called!");
  28.  
  29.         thisText.text = ProcessInventoryToSingleString(_inventory);
  30.     }
  31.     string[] GenerateArray(InventoryObject _inventory)
  32.     {
  33.         string[] arrayToReturn = new string[_inventory.Container.Count];
  34.         for (int i = 0; i < _inventory.Container.Count; i++)
  35.         {
  36.             arrayToReturn[i] = $"{_inventory.Container[i].amount}x {_inventory.Container[i].itemContained.itemName} \n";
  37.         }
  38.         return arrayToReturn;
  39.     }
  40.     string MakeArrayOneString(string[] stringArray)
  41.     {
  42.         string wholeText = "";
  43.         foreach(string s in stringArray)
  44.         {
  45.             wholeText += s;
  46.         }
  47.         return wholeText;
  48.     }
  49.     string ProcessInventoryToSingleString(InventoryObject _inventory)
  50.     {
  51.         string output = MakeArrayOneString(GenerateArray(_inventory));
  52.         return output;
  53.     }
  54.     private void OnApplicationQuit()
  55.     {
  56.         PlayerInventory.OnItemPickedUp -= UpdateDisplayText;
  57.     }
  58.     private static string GetClassAndMethod()
  59.     {
  60.         StackTrace stackTrace = new StackTrace();
  61.         StackFrame frame = stackTrace.GetFrame(2); // get the frame of the most recent caller
  62.         string s = frame.GetMethod().DeclaringType.Name + "::" + frame.GetMethod().Name + "(";
  63.         foreach (System.Reflection.ParameterInfo pi in frame.GetMethod().GetParameters())
  64.         {
  65.             s += pi.ParameterType.ToString() + " " + pi.Name + ",";
  66.         }
  67.         s += ")";
  68.         return s;
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement