Advertisement
Guest User

TotalScoreView

a guest
Jul 19th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5.  
  6. public class TotalScoreView : MonoBehaviour
  7. {
  8.     //этот класс должен быть на сцене только в одном экземпляре, иначе чревато
  9.     private List<ScoreView> m_ScoreViewObjects;
  10.  
  11.     private Text m_TotalScore;
  12.     private int m_TotalScoreCounter = 0;
  13.  
  14.     private void Awake()
  15.     {      
  16.         m_ScoreViewObjects = new List<ScoreView> ();
  17.         m_TotalScore = GetComponent<Text> ();
  18.     }
  19.  
  20.     public void RegisterObserver(ScoreView observer)
  21.     {
  22.         observer.onDamageRecieve += OnDamageRecieve;
  23.         m_ScoreViewObjects.Add (observer);
  24.     }
  25.  
  26.     private void ShowScore(string value)
  27.     {
  28.         m_TotalScore.text = value;
  29.     }
  30.  
  31.     private string ScoresToString(int value)
  32.     {
  33.         return string.Format ("Score: {0}", value);
  34.     }
  35.  
  36.     private void OnDamageRecieve(int value)
  37.     {
  38.         m_TotalScoreCounter += value;
  39.         ShowScore (ScoresToString (m_TotalScoreCounter));
  40.     }
  41.  
  42.     private static TotalScoreView m_Instance;
  43.     public static TotalScoreView Instance
  44.     {
  45.         get
  46.         {
  47.             if (m_Instance == null)
  48.             {
  49.                 m_Instance = this;
  50.                 return m_Instance;
  51.             }
  52.             return m_Instance;
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement