Created
October 30, 2019 09:16
-
-
Save verborghs/0b0305f0a8aa012bebd797c155250f90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product | |
{ | |
private int _available; | |
public event EventHandler AvailabilityChanged; | |
public Product(string name, int coins, int available) | |
{ | |
Name = name; | |
Coins = coins; | |
Available = available; | |
} | |
public string Name { get; } | |
public int Coins { get; } | |
public int Available | |
{ | |
get | |
{ | |
return _available; | |
} | |
set | |
{ | |
_available = value; | |
AvailabilityChanged?.Invoke(this, EventArgs.Empty); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductView : MonoBehaviour | |
{ | |
[SerializeField] | |
private Text _code; | |
[SerializeField] | |
private Text _name; | |
[SerializeField] | |
private Text _coins; | |
[SerializeField] | |
private Image _panel; | |
[SerializeField] | |
private Sprite _productAvailableTexture; | |
[SerializeField] | |
private Sprite _productUnavailableTexture; | |
private Product _product; | |
public Product Product | |
{ | |
set | |
{ | |
RemoveListeners(); | |
_product = value; | |
AddListener(); | |
_name.text = _product.Name; | |
_coins.text = "" + _product.Coins; | |
UpdateAvailabilityIndicator(); | |
} | |
} | |
public string Code | |
{ | |
set { _code.text = value; } | |
} | |
public ProductView Create(Transform parent) | |
{ | |
GameObject go = Instantiate(this.gameObject, parent); | |
return go.GetComponent<ProductView>(); | |
} | |
private void AddListener() | |
{ | |
_product.AvailabilityChanged += OnAvailabilityChanged; | |
} | |
private void RemoveListeners() | |
{ | |
if (_product != null) | |
_product.AvailabilityChanged -= OnAvailabilityChanged; | |
} | |
private void OnAvailabilityChanged(object sender, EventArgs evt) | |
{ | |
UpdateAvailabilityIndicator(); | |
} | |
private void UpdateAvailabilityIndicator() | |
{ | |
_panel.sprite = (_product.Available <= 0) ? _productUnavailableTexture : _productAvailableTexture; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment