chenxin
2020-12-01 944950c86ab3be21c22b7c15039cf2e572efdc90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Core.Economy;
using System.Xml;
using TMPro;
using TowerDefense.Level;
using UnityEngine;
using UnityEngine.UI;
 
namespace TowerDefense.UI.HUD
{
    /// <summary>
    /// A class for controlling the displaying the currency
    /// </summary>
    public class CurrencyUI : MonoBehaviour
    {
        /// <summary>
        /// TextMeshPro对应的金币数据
        /// </summary>
        public TextMeshProUGUI displayText;
 
        // 对手的金币显示
        //public Text displayOpp;
 
        /// <summary>
        /// The currency prefix to display next to the amount
        /// </summary>
        public string currencySymbol = "$";
 
        protected Currency m_Currency;
        protected Currency m_CurrencyOpp;
 
        /// <summary>
        /// Assign the correct currency value
        /// </summary>
        protected virtual void Start()
        {
        }
 
        protected void Update()
        {
            if (m_Currency != null) return ;
 
            if ( (LevelManager.instance != null) )
            {
                m_Currency = LevelManager.instance.currency;
                //m_CurrencyOpp = OpponentMgr.instance.currency;
 
                UpdateDisplay();
                m_Currency.currencyChanged += UpdateDisplay;
                //m_CurrencyOpp.currencyChanged += UpdateDisplay;
            }
            else
            {
                Debug.LogError("[UI] No level manager to get currency from");
            }
 
        }
 
        /// <summary>
        /// Unsubscribe from events
        /// </summary>
        protected virtual void OnDestroy()
        {
            if (m_Currency != null)
            {
                m_Currency.currencyChanged -= UpdateDisplay;
                if( m_CurrencyOpp != null  )
                    m_CurrencyOpp.currencyChanged -= UpdateDisplay;
            }
        }
 
        /// <summary>
        /// A method for updating the display based on the current currency
        /// </summary>
        protected void UpdateDisplay()
        {
            int current = m_Currency.currentCurrency;
            displayText.text = AddNumberSemi(current.ToString());
 
            //m_CurrencyOpp = OpponentMgr.instance.currency;
            //current = m_CurrencyOpp.currentCurrency;
            //displayOpp.text = current.ToString();
        }
 
        /// <summary>
        /// 在数字中加入分号.
        /// </summary>
        /// <param name="str"></param>
        protected string AddNumberSemi( string str)
        {
            int len = str.Length;
            for( int ipos = len-3;ipos>0;ipos -= 3 )
            {
                str = str.Insert(ipos, ",");
            }
            return str;
        }
    }
}