Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

c# Speed up score calculation on Click/Touch

Discussion in 'Scripting' started by amichael, Dec 3, 2015.

  1. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    I was wondering what i could add to my code to have the score calculate really fast on click and touch, if a player does not want to sit and wait for it to finish calculating. Here is my current code

    Code (CSharp):
    1. private mainRoot pMainRoot = null;
    2.     private int pScore = 0;
    3.     private int pFinalScore = 0;
    4.     private UILabel pPointsLable = null;
    5.     private UILabel pScoreLable = null;
    6.     private UILabel pBonusLable = null;
    7.     private UILabel pMultiplierLable = null;
    8.     private int pPoints = 0;
    9.     private int pCoins = 0;
    10.     private int pCombos = 0;
    11.     private int vCombos = 20;
    12.     private bool pTallyActive = false;
    13.     private int pTallyStage=0;
    14.     private int pTallyCycle = 0;
    15.     private int pTallyDelayCycle = 0;
    16.  
    17.     //
    18.     private void Start () {
    19.         findObjects();
    20.     }
    21.     //
    22.     private void findObjects(){
    23.         pMainRoot = GameObject.Find ("mainRoot").GetComponent<mainRoot> ();
    24.         pPointsLable = GameObject.Find ("Label_points").GetComponent<UILabel> ();
    25.         pScoreLable = GameObject.Find ("Label_score").GetComponent<UILabel> ();
    26.         pBonusLable = GameObject.Find ("Label_bonus").GetComponent<UILabel> ();
    27.         pMultiplierLable = GameObject.Find ("Label_multiplier").GetComponent<UILabel> ();
    28.     }
    29.     //
    30.     public void takeScore(int vPoints, int vCoins, int vCombos){
    31.         traceOut ("takeScore");
    32.         pPoints = vPoints;
    33.         pCoins = vCoins;
    34.         pCombos = vCombos;
    35.         //for testing
    36.         //pPoints = 100;
    37.         //pCoins = 15;
    38.         //pCombos = 30;
    39.     }
    40.     //sent by intro animation event
    41.     public void introAnimationOver(){
    42.         pTallyActive = true;
    43.         nextTallyStage();
    44.         traceOut ("introAnimationOver");
    45.     }
    46.     //
    47.     private void Update () {
    48.         if (pTallyActive){
    49.             tallyScore();
    50.         }
    51.     }
    52.     //
    53.     private void tallyScore(){
    54.         pTallyDelayCycle++;
    55.         if (pTallyDelayCycle<2){
    56.             return;
    57.         }
    58.         pTallyDelayCycle = 0;
    59.         switch(pTallyStage){
    60.         case 1:
    61.             tallyPoints();
    62.  
    63.        
    64.  
    65.             break;
    66.        
    67.        
    68.        
    69.         case 2:
    70.             tallyCombos();
    71.        
    72.  
    73.  
    74.             break;
    75.        
    76.        
    77.        
    78.         case 3:
    79.             tallyCoins();
    80.  
    81.        
    82.  
    83.             break;
    84.  
    85.        
    86.         case 4:
    87.             tallyFinalScore();
    88.             break;
    89.         }
    90.     }
    91.     //
    92.     private void playCoinSound(){
    93.         pMainRoot.pSoundManager.playBtnSound(2);
    94.     }
    95.     //
    96.     private void nextTallyStage(){
    97.         pTallyStage++;
    98.         pTallyCycle=0;;
    99.     }
    100.     //
    101.     private void tallyPoints(){
    102.         if (pTallyCycle<pPoints){
    103.             pTallyCycle++;
    104.             pPointsLable.text = "POINTS " + pTallyCycle;
    105.             //playCoinSound();
    106.         }else{
    107.             nextTallyStage();
    108.         }
    109.     }
    110.  
    111.  
    112.     //
    113.     //
    114.     private void tallyCombos(){
    115.         if (pTallyCycle<pCombos){
    116.             pTallyCycle++;
    117.             pMultiplierLable.text = "MULTIPLIER X" + pTallyCycle;
    118.             //playCoinSound();
    119.         }else{
    120.             nextTallyStage();
    121.         }
    122.     }
    123.     //
    124.     private void tallyCoins(){
    125.         if (pTallyCycle<pCoins){
    126.             pTallyCycle++;
    127.             pBonusLable.text = "BONUS ITEM X " + pTallyCycle;
    128.             //playCoinSound();
    129.         }else{
    130.             calculateFinalScore();
    131.             nextTallyStage();
    132.         }
    133.     }
    134.     //
    135.     private void calculateFinalScore(){
    136.         pFinalScore = pPoints + pCombos * 20 + pCoins * 50;
    137.     }
    138.     //
    139.     private void tallyFinalScore(){
    140.         if (pTallyCycle<pFinalScore){
    141.             pTallyCycle++;
    142.         }else{
    143.             pTallyActive = false;
    144.         }
    145.         string score = addCommasToScore(pTallyCycle);
    146.         //playCoinSound();
    147.         pScoreLable.text = "SCORE " + score;
    148.     }
    149.     //
    150.     private string addCommasToScore(int vScore){
    151.         string score = vScore.ToString( "n0" );
    152.         return (score);
    153.     }
    154.     //
    155.     private void traceOut (String txt) {
    156.         print ("scoreScreen: " + txt);
    157.     }
    158. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    line 55, add a boolean check
    Code (csharp):
    1.  
    2. if(pTallyDelayCycle<2 && !skipWait){
    3.  
    have the boolean controlled by the input. Basically, if the user taps/clicks it skips the return lines regardless.
     
  3. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    @LeftyRighty thx buddy, i have another question for you though. I have a skip button in place that they can click already and just cut to next screen , so they can always just skip the entire thing if they want. You know of a way to have it just tally up very fast? so cuts the tally time in half if they do stay on the screen?