Search Unity

Best Time in Table ??

Discussion in 'Scripting' started by kosprer, Feb 5, 2019.

  1. kosprer

    kosprer

    Joined:
    Jan 8, 2019
    Posts:
    14
    hi, would anyone help me modify the code so that instead of typing the time that is displayed on the scoreboard it was captured at the end of the race? I do not know at what point I should throw it in and how to implement it. Help :(

    code dashboard in down

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerTime
    8. {
    9.     public string playerName;
    10.     public string playerTime;
    11.  
    12.     public PlayerTime(string playerName, string playerTime)
    13.     {
    14.         this.playerName = playerName;
    15.         this.playerTime = playerTime;
    16.     }
    17.  
    18.     public string GetFormat()
    19.     {
    20.         return playerName + "~5~" + playerTime;
    21.     }
    22. }
    23.  
    24.  
    25.  
    26. public class TimeBoard : MonoBehaviour
    27. {
    28.     public int timeCount = 10;
    29.  
    30.     [Header("SAVE PANEL")]
    31.     public InputField inputName;
    32.     public InputField inputTime;
    33.  
    34.     [Header("TIME DISPLAY")]
    35.     public GameObject timeObject;
    36.     public GameObject timeRoot;
    37.     public Text textName, textTime;
    38.  
    39.     static TimeBoard timeBoard;
    40.     static string separator = "~5~";
    41.  
    42.     void Start()
    43.     {
    44.         timeBoard = this;
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         if (Input.GetKeyDown(KeyCode.S))
    50.         {
    51.  
    52.         }
    53.  
    54.         if (Input.GetKeyDown(KeyCode.P))
    55.         {
    56.             List<PlayerTime> playerTime = GetTime();
    57.             foreach (PlayerTime p in playerTime)
    58.             {
    59.                 print(p.playerName + "      " + p.playerTime);
    60.             }
    61.         }
    62.     }
    63.  
    64.     public void SaveTimeNow()
    65.     {
    66.         SaveTime(inputName.text, inputTime.text);
    67.     }
    68.  
    69.     public void ShowTime()
    70.     {
    71.         StartCoroutine(CoShowTime());
    72.     }
    73.  
    74.     IEnumerator CoShowTime()
    75.     {
    76.         while (timeRoot.transform.childCount > 0)
    77.         {
    78.             Destroy(timeRoot.transform.GetChild(0).gameObject);
    79.             yield return null;
    80.         }
    81.  
    82.         List<PlayerTime> playerTime = GetTime();
    83.  
    84.         foreach (PlayerTime time in playerTime)
    85.         {
    86.             textName.text = time.playerName;
    87.             textTime.text = time.playerTime.ToString();
    88.  
    89.             GameObject instantiatedTime = Instantiate(timeObject);
    90.             instantiatedTime.SetActive(true);
    91.  
    92.             instantiatedTime.transform.SetParent(timeRoot.transform);
    93.         }
    94.     }
    95.  
    96.     public static void SaveTime(string name, string time)
    97.     {
    98.         List<PlayerTime> playerTime = new List<PlayerTime>();
    99.         for (int i = 0; i < timeBoard.timeCount; i++)
    100.         {
    101.             if (PlayerPrefs.HasKey("Time" + i))
    102.             {
    103.                 string[] timeFormat = PlayerPrefs.GetString("Time" + i).Split(new string[] { separator }, System.StringSplitOptions.RemoveEmptyEntries);
    104.                 playerTime.Add(new PlayerTime(timeFormat[0], timeFormat[1]));
    105.             }
    106.             else
    107.             {
    108.                 break;
    109.             }
    110.         }
    111.         if (playerTime.Count < 1)
    112.         {
    113.             PlayerPrefs.SetString("Time0", name + separator + time);
    114.             print("terpanggil");
    115.             return;
    116.         }
    117.         playerTime.Add(new PlayerTime(name, time));
    118.         playerTime = playerTime.OrderBy(o => o.playerTime).ToList();
    119.  
    120.         for (int i = 0; i < timeBoard.timeCount; i++)
    121.         {
    122.             if (i >= playerTime.Count) { break; }
    123.             PlayerPrefs.SetString("Time" + i, playerTime[i].GetFormat());
    124.         }
    125.     }
    126.  
    127.     public List<PlayerTime> GetTime()
    128.     {
    129.         List<PlayerTime> playerTime = new List<PlayerTime>();
    130.         for (int i = 0; i < timeBoard.timeCount; i++)
    131.         {
    132.             if (PlayerPrefs.HasKey("Time" + i))
    133.             {
    134.                 string[] timeFormat = PlayerPrefs.GetString("Time" + i).Split(new string[] { separator }, System.StringSplitOptions.RemoveEmptyEntries);
    135.                 playerTime.Add(new PlayerTime(timeFormat[0], timeFormat[1]));
    136.             }
    137.             else
    138.             {
    139.                 break;
    140.             }
    141.         }
    142.         return playerTime;
    143.     }
    144. }
    145.  


    code from which I wants to capture time



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LapComplete : MonoBehaviour
    7. {
    8.  
    9.     public GameObject LapCompleteTrig;
    10.     public GameObject HalfLapTrig;
    11.  
    12.     public GameObject MinuteDisplay;
    13.     public GameObject SecondDisplay;
    14.     public GameObject MilliDisplay;
    15.  
    16.     public GameObject LapTimeBox;
    17.  
    18.     public GameObject LapCounter;
    19.     public int LapsDone;
    20.  
    21.     public float RawTime;
    22.  
    23.     public GameObject RaceFinish;
    24.  
    25.     void Update()
    26.     {
    27.         if (LapsDone == 3)
    28.         {
    29.             RaceFinish.SetActive(true);
    30.         }
    31.     }
    32.  
    33.     void OnTriggerEnter()
    34.     {
    35.  
    36.         LapsDone += 1;
    37.         RawTime = PlayerPrefs.GetFloat("RawTime");
    38.         if (LapTimeManager.RawTime <= RawTime)
    39.         {
    40.             if (LapTimeManager.SecondCount <= 9)
    41.             {
    42.                 SecondDisplay.GetComponent<Text>().text = "0" + LapTimeManager.SecondCount + ".";
    43.             }
    44.             else
    45.             {
    46.                 SecondDisplay.GetComponent<Text>().text = "" + LapTimeManager.SecondCount + ".";
    47.             }
    48.  
    49.             if (LapTimeManager.MinuteCount <= 9)
    50.             {
    51.                 MinuteDisplay.GetComponent<Text>().text = "0" + LapTimeManager.MinuteCount + ".";
    52.             }
    53.             else
    54.             {
    55.                 MinuteDisplay.GetComponent<Text>().text = "" + LapTimeManager.MinuteCount + ".";
    56.             }
    57.  
    58.             MilliDisplay.GetComponent<Text>().text = "" + LapTimeManager.MilliCount;
    59.  
    60.             PlayerPrefs.SetFloat("RawTime", LapTimeManager.RawTime);
    61.         }
    62.         else
    63.         {
    64.             PlayerPrefs.SetFloat("RawTime", RawTime);
    65.         }
    66.  
    67.         PlayerPrefs.SetInt("MinSave", LapTimeManager.MinuteCount);
    68.         PlayerPrefs.SetInt("SecSave", LapTimeManager.SecondCount);
    69.         PlayerPrefs.SetFloat("MilliSave", LapTimeManager.MilliCount);
    70.         //PlayerPrefs.SetFloat("RawTime", LapTimeManager.RawTime);
    71.      
    72.         LapTimeManager.MinuteCount = 0;
    73.         LapTimeManager.SecondCount = 0;
    74.         LapTimeManager.MilliCount = 0;
    75.         LapTimeManager.RawTime = 0;
    76.         LapCounter.GetComponent<Text>().text = "" + LapsDone;
    77.  
    78.         HalfLapTrig.SetActive(true);
    79.         LapCompleteTrig.SetActive(false);
    80.  
    81.  
    82.     }
    83. }

    in case of anything I can send some photos from the application