Пример #1
0
    // Use this for initialization
    void Start()
    {
        // ----------------------------------
        // Activate mainMenu
        // ----------------------------------
        mainMenu.SetActive(true);
        endMenu.SetActive(false);
        gameMenu.SetActive(false);


        // ----------------------------------
        // Setup scoreDelayed + scoreDelta
        // ----------------------------------

        // Score Delayed
        scoreDelayed = score.Throttle(TimeSpan.FromSeconds(1)).ToReactiveProperty();

        // Score Delta ( no need to have it as a private value since we're not gonna need to use it again in another function )
        var scoreDelta = score.Select(x => x - scoreDelayed.Value);

        // Change deltaScoreText and format it so that if the number is positive it has a "+" in front of it
        scoreDelta.SubscribeToText(deltaScoreText, x => (x > 0 ? ("+" + x) : x.ToString()));

        // Add animation everytime score delta changes
        scoreDelta.Subscribe(_ => AnimateObj(deltaScoreText.gameObject));

        scoreDelayed.Subscribe(delayedScore => {
            // Change text
            scoreText.text = delayedScore.ToString(); // You can also set it using scoreDelayed.SubscribeToText(scoreText);

            // Start animation
            AnimateObj(scoreText.gameObject);

            // Clear deltaScore ocne we've updated the score
            deltaScoreText.text = "";
        });

        // ----------------------------------
        // Setup highscore ( and save values )
        // ----------------------------------
        int lastHighScore = PlayerPrefs.GetInt("highscore");

        highscore = score.StartWith(lastHighScore).DistinctUntilChanged().Scan(int.MinValue, Math.Max).Do(x => PlayerPrefs.SetInt("highscore", x)).ToReactiveProperty();

        // ----------------------------------
        // Setup levels
        // ----------------------------------
        level.SubscribeToText(levelText, x => "Level " + x.ToString());

        // ----------------------------------
        // Button actions
        // ----------------------------------
        startButton.onClick.AddListener(StartGame);
        restartButton.onClick.AddListener(StartGame);
    }