Advertisement
Hermesis

Unity Ui Window Additive Scene

Feb 6th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1.     public class ResultLineEntity {
  2.         public int Place;
  3.         public readonly string Title;
  4.         public readonly TimeSpan Time;
  5.         public ResultLineEntity(string title, TimeSpan time, int? place = null) {
  6.             if (place != null) Place = place.Value;
  7.             Title = title;
  8.             Time = time;
  9.         }
  10.     }
  11.  
  12.     public class ResultsUi : MonoBehaviour {
  13.         public static ResultLineEntity[] Lines
  14.         {
  15.             get => _lines;
  16.             set
  17.             {
  18.                 _lines = value;
  19.                 OnLinesChange?.Invoke();
  20.             }
  21.         }
  22.         private static ResultLineEntity[] _lines;
  23.  
  24.         public static UnityEvent OnLinesChange { get; private set; } = new UnityEvent();
  25.  
  26.         [SerializeField] private Transform root;
  27.         [SerializeField] private ResultLineUi prefab;
  28.  
  29.         private void Awake() {
  30.             SetLines();
  31.             OnLinesChange?.AddListener(SetLines);
  32.         }
  33.  
  34.         public void SetLines() {
  35.             if (Lines==null || Lines.Length < 1) return;
  36.            
  37.             foreach (var child in root.GetComponentsInChildren<ResultLineUi>()) {
  38.                 Destroy(child.gameObject);
  39.             }
  40.  
  41.             var resultsOrdered = Lines.OrderBy(x => x.Time).ToArray();
  42.             for (var index = 0; index < resultsOrdered.Length; index++) {
  43.                 var line = resultsOrdered[index];
  44.                 line.Place = index + 1;
  45.                 var currentLine = Instantiate(prefab, root);
  46.                 currentLine.SetLine(line);
  47.             }
  48.         }
  49.     }
  50.  
  51. //EXAMPLE
  52. //ResultsUi.Lines = lines;
  53. //SceneManager.LoadScene(ResultsSceneName, LoadSceneMode.Additive);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement