Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deebloo/unityflux
https://github.com/deebloo/unityflux
Last synced: 15 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/deebloo/unityflux
- Owner: deebloo
- Created: 2016-01-18T20:24:40.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T05:23:08.000Z (about 1 year ago)
- Last Synced: 2024-10-31T16:59:40.786Z (2 months ago)
- Language: ASP
- Size: 6.5 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UnityFlux
Use flux pattern with Unity from the RollABall tutorial level.
### Actions
```C#
public static class Actions {
// Create Score updated delegate and event
public delegate int ScoreUpdateAction();
public static event ScoreUpdateAction OnScoreUpdateAction;
// Update the score
public static void UpdateScore() {
if(OnScoreUpdateAction != null) {
// Fire the OnScoreUpdateAction event
OnScoreUpdateAction ();
}
}
}```
### Store
```C#
public static class ScoreStore {
// private model
private static int score;
// This stores delegate and event
public delegate void ScoreUpdated(int score);
public static event ScoreUpdated OnScoreUpdated;
// Set default score to 0 and add method to OnScoreUpDateAction event
static ScoreStore () {
score = 0;Actions.OnScoreUpdateAction += updateScore;
}// increment the score by one and fire the OnScoreUpdated event.
private static int updateScore() {
score++;
// Fire the OnScoreUpdated event
if(OnScoreUpdated != null) {
OnScoreUpdated (score);
}return score;
}// returns the score value;
public static int getScore() {
return score;
}
}
```### PlayerController
```C#
...
public class PlayerController : MonoBehaviour {
...
// update the score and desactivate collectables
void OnTriggerEnter(Collider other) {
if(other.gameObject.CompareTag("Pickup")) {
other.gameObject.SetActive (false);
Actions.UpdateScore();
}
}
}
```### ScoreBoardController
```C#
...
public class ScoreBoardController : MonoBehaviour {
private Text scoreBoard;// Use this for initialization
void Start () {
scoreBoard = GetComponent ();
setCountText (ScoreStore.getScore());
}void OnEnable () {
ScoreStore.OnScoreUpdated += setCountText;
}void OnDisable () {
ScoreStore.OnScoreUpdated -= setCountText;
}// Update text when score is updated
void setCountText (int score) {
scoreBoard.text = "Score: " + score.ToString();if(score >= 5) {
scoreBoard.text = "You Win!";
}
}
}
```