UILeaderBoard.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Collections;
  5. using UnityEngine.SocialPlatforms.GameCenter;
  6. public class UILeaderBoard : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private UIButton Home;
  10. [SerializeField]
  11. private UIAtlas _atlas;
  12. [SerializeField]
  13. private string _spriteName;
  14. [SerializeField]
  15. private GameObject _scrollRoot;
  16. [SerializeField]
  17. private float Offset;
  18. [SerializeField]
  19. private UIFont _font;
  20. private int _maxScroll;
  21. private List<GameObject> _items = new List<GameObject>();
  22. //private List<GPGScore> _scores = new List<GPGScore>();
  23. public List<UserTop> scores = new List<UserTop>();
  24. private WaitForSeconds wait = new WaitForSeconds(0.5f);
  25. //public bool _isScoresCreated = false;
  26. public UIButton Up;
  27. public UIButton Down;
  28. private UIGrid _grid;
  29. public static UILeaderBoard Instance;
  30. #if UNITY_ANDROID
  31. private const string _leaderBoardID = "CgkIl5Kk5v8bEAIQBw";
  32. #endif
  33. // Use this for initialization
  34. void Start ()
  35. {
  36. Instance = this;
  37. //Home.onClick.Add(new EventDelegate(OnHome));
  38. #if UNITY_ANDROID
  39. Init();
  40. #endif
  41. #if UNITY_IPHONE
  42. //gameObject.AddComponent<GCLeaderBoard>();
  43. Debug.Log("UILeaderBoard::Started");
  44. #endif
  45. _grid = this.gameObject.GetComponentInChildren<UIGrid>();
  46. }
  47. void Update()
  48. {
  49. }
  50. //
  51. public void Reset()
  52. {
  53. //return;
  54. for (int i = 0; i < _items.Count; i++)
  55. {
  56. Destroy(_items[i]);
  57. }
  58. _items.Clear();
  59. StartLoadScores();
  60. }
  61. public void StartLoadScores()
  62. {
  63. StartCoroutine(LoadScores());
  64. }
  65. void OnEnable()
  66. {
  67. //ServerGiftManager.Instance.RequestWeeksUpdate(week =>
  68. //{
  69. // if (string.IsNullOrEmpty(week))
  70. // {
  71. // Debug.Log("Error");
  72. // //todo: server error, or wrong number filled
  73. // }
  74. // else
  75. // {
  76. // Label.text = string.Format(week.ToString());
  77. // }
  78. //});
  79. }
  80. public IEnumerator LoadScores()
  81. {
  82. if (scores == null)
  83. {
  84. yield break;
  85. }
  86. for (int i = 0; i < scores.Count; i++)
  87. {
  88. if (i % 10 == 0)
  89. {
  90. yield return wait;
  91. }
  92. // ---- CREATE SPRITE
  93. var panel = NGUITools.AddChild<UIPanel>(_scrollRoot);//.AddSprite(_scrollRoot, _atlas, _spriteName);
  94. var go = panel.transform;
  95. Destroy(panel);
  96. //go.MakePixelPerfect();
  97. //go.transform.localPosition = new Vector3(0, -i * (go.height + 5) + Offset, 0);
  98. go.gameObject.AddComponent<UIDragPanelContents>();
  99. //go.gameObject.AddComponent<BoxCollider>();
  100. //go.gameObject.GetComponent<BoxCollider>().size = new Vector3(400, 110, 0);
  101. //go.type = UISprite.Type.Simple;
  102. //go.width = 450;
  103. //go.height = 110;
  104. // ---- ADD USER_PLACE TO SPRITE
  105. var place = NGUITools.AddChild<UILabel>(go.gameObject);
  106. place.name = "Place";
  107. place.pivot = UIWidget.Pivot.Left;
  108. place.font = _font;
  109. place.color = new Color(249f / 255f, 205f / 255f, 82f / 255f, 1);
  110. //place.effectStyle = UILabel.Effect.Outline;
  111. place.effectColor = Color.white;
  112. place.text = (i + 1).ToString() + ".";
  113. place.depth = 0;
  114. place.transform.localPosition = new Vector3(-180, 10, -1);
  115. place.transform.localScale = new Vector3(1f, 1f, 1);
  116. // ---- ADD USER_NAME TO SPRITE
  117. var pos = NGUITools.AddChild<UILabel>(go.gameObject);
  118. var pos2 = NGUITools.AddChild<UILabel>(go.gameObject);
  119. pos.name = "FacebookUserName";
  120. pos2.name = "FacebookSecondName";
  121. pos2.pivot =
  122. pos.pivot = UIWidget.Pivot.Left;
  123. pos.font = _font;
  124. pos.color = new Color(249f/255f, 205f / 255f, 82f / 255f, 1);
  125. //pos.effectStyle = UILabel.Effect.Outline;
  126. pos.effectColor = Color.white;
  127. pos.text = scores[i].User.FirstName;// +" " + _scores[i].User.LastName;
  128. pos.depth = 0;
  129. pos.transform.localPosition = new Vector3(-140, 10, -1);
  130. pos.transform.localScale = new Vector3(0.9f, 0.9f, 1);
  131. // ---- ADD USER_SCORE TO SPRITE
  132. var lname = NGUITools.AddChild<UILabel>(go.gameObject);
  133. lname.name = "Distance";
  134. lname.font = _font;
  135. lname.color = Color.white;
  136. // lname.effectStyle = UILabel.Effect.Outline;
  137. lname.effectColor = new Color(249f / 255f, 205f / 255f, 82f / 255f, 1); ;
  138. lname.text = scores[i].User.Score.ToString() + " Stig";
  139. lname.depth = 0;
  140. lname.transform.localPosition = new Vector3(35, 10, 0);
  141. lname.transform.localScale = new Vector3(0.9f, 0.9f, 1);
  142. // ---- ADD USER_gifts TO SPRITE
  143. //var gift = NGUITools.AddChild<UILabel>(go.gameObject);
  144. //gift.name = "Gifts";
  145. //gift.font = _font;
  146. //gift.text = _scores[i].User.Gifts.ToString();
  147. //gift.depth = 0;
  148. //gift.transform.localPosition = new Vector3(160, -22, 0);
  149. //gift.transform.localScale = new Vector3(0.6f, 0.6f, 1);
  150. if (i < 3)
  151. {
  152. // ---- ADD SANTA_ICONS_TOP_3 TO SPRITE
  153. string spriteName = "";
  154. //int idBadge = _scores[i].User.SantaBadge;
  155. switch (i)
  156. {
  157. case 0: spriteName = "medal_gold"; break;
  158. case 1: spriteName = "medal_silver"; break;
  159. case 2: spriteName = "medal_bronze"; break;
  160. }
  161. var santa = NGUITools.AddSprite(go.gameObject, _atlas, spriteName);
  162. santa.name = "santaBadge";
  163. santa.transform.localPosition = new Vector3(160, 10, -1);
  164. santa.MakePixelPerfect();
  165. santa.width = 63;
  166. santa.height = 63;
  167. santa.depth = 0;
  168. }
  169. _items.Add(go.gameObject);
  170. _grid.Reposition();
  171. }
  172. //_isScoresCreated = true;
  173. yield break;
  174. }
  175. //
  176. private bool _initDone;
  177. public UILabel Label;
  178. #if UNITY_ANDROID
  179. private void Init()
  180. {
  181. if (_initDone)
  182. {
  183. return;
  184. }
  185. _initDone = true;
  186. /* GPGManager.authenticationSucceededEvent += OnAuthDone;
  187. GPGManager.authenticationFailedEvent += OnAuthFail;
  188. GPGManager.submitScoreFailedEvent += OnSubmitScoreFail;
  189. GPGManager.submitScoreSucceededEvent += OnSubmitScoreDone;
  190. GPGManager.loadScoresFailedEvent += OnGetLeadeboardFail;
  191. GPGManager.loadScoresSucceededEvent += OnGetLeadeboardDone;*/
  192. //FileLog.Init(Application.persistentDataPath,"leaderboard",true,true,true,FileLog.LogLevels.Log);
  193. LoginProcess();
  194. Debug.Log("Init");
  195. }
  196. #endif
  197. //private List<Action> _postAuthActions = new List<Action>();
  198. //private List<Action> _postRequestActions = new List<Action>();
  199. #if UNITY_ANDROID
  200. private bool LoginProcess()
  201. {
  202. // if (!PlayGameServices.isSignedIn())
  203. {
  204. //#if UNITY_IPHONE
  205. // if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_id", "")))
  206. // {
  207. // PlayGameServices.init(PlayerPrefs.GetString("user_id"), true);
  208. // }
  209. // else
  210. // {
  211. // PlayGameServices.authenticate();
  212. // }
  213. //#else
  214. if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_id", "")))
  215. {
  216. //PlayGameServices.attemptSilentAuthentication();
  217. }
  218. else
  219. {
  220. // PlayGameServices.authenticate();
  221. Debug.Log("LoginProcess:authenticate");
  222. }
  223. //#endif
  224. return false;
  225. }
  226. return true;
  227. }
  228. private void OnAuthDone(string userId)
  229. {
  230. Debug.Log("OnAuthDone " + userId);
  231. PlayerPrefs.SetString("user_id",userId);
  232. if (NGUIMenuScript.Instance.getCurrentMenu() == NGUIMenuScript.NGUIMenus.LeaderBoard)
  233. {
  234. RequestLeaderBoard();
  235. }
  236. }
  237. private void OnAuthFail(string fail)
  238. {
  239. Debug.LogError("OnAuthFail " + fail);
  240. //PlayGameServices.authenticate();
  241. }
  242. private void OnSubmitScoreDone(string s, Dictionary<string, object> dictionary)
  243. {
  244. /* Debug.Log("OnSubmitScoreDone " + s);
  245. if (NGUIMenuScript.Instance.getCurrentMenu() == NGUIMenuScript.NGUIMenus.LeaderBoard)
  246. {
  247. RequestLeaderBoard();
  248. }*/
  249. }
  250. private void OnSubmitScoreFail(string a, string b)
  251. {
  252. //Debug.Log("OnSubmitScoreFail " + a + " : " + b);
  253. }
  254. private void OnGetLeadeboardDone(/*List<GPGScore> gpgScores*/)
  255. {
  256. /*Debug.Log("OnGetLeadeboardDone " + gpgScores.Count);
  257. _scores = gpgScores;
  258. Reset();*/
  259. }
  260. private void OnGetLeadeboardFail(string a, string b)
  261. {
  262. //Debug.Log("OnGetLeadeboardFail " + a + " : " + b);
  263. }
  264. #endif
  265. private void OnHome()
  266. {
  267. if (NGUIMenuScript.Instance.CurStatePopup == NGUIMenuScript.StatesMenuForPopup.Popup ||
  268. NGUIMenuScript.Instance.CurStatePopup == NGUIMenuScript.StatesMenuForPopup.GameOver)
  269. {
  270. //NGUIMenuScript.Instance.CloseMenu(NGUIMenuScript.NGUIMenus.LeaderBoardNew);
  271. //NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.Banner);
  272. InGameScriptCS.Instance.LoadStartSanta(6);
  273. return;
  274. }
  275. NGUIMenuScript.Instance.CloseMenu(NGUIMenuScript.NGUIMenus.LeaderBoardNew);
  276. NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.Menu);
  277. }
  278. /*
  279. private void OnDrag(Vector2 drag)
  280. {
  281. _scrollRoot.transform.localPosition+= Vector3.up * drag.y;
  282. if (_scrollRoot.transform.localPosition.y < 0)
  283. {
  284. _scrollRoot.transform.localPosition = Vector3.zero;
  285. }
  286. if (_scrollRoot.transform.localPosition.y > _maxScroll)
  287. {
  288. _scrollRoot.transform.localPosition = new Vector3(0, _maxScroll,0);
  289. }
  290. }*/
  291. #if UNITY_ANDROID
  292. public void RequestLeaderBoard()
  293. {
  294. //PlayGameServices.showLeaderboard(_leaderBoardID, GPGLeaderboardTimeScope.AllTime);
  295. }
  296. #endif
  297. public void OnShow()
  298. {
  299. #if UNITY_IPHONE
  300. //GCLeaderBoard.Show();
  301. return;
  302. #endif
  303. #if UNITY_ANDROID
  304. Init();
  305. if (LoginProcess())
  306. {
  307. RequestLeaderBoard();
  308. Debug.Log("OnShow:LoginProcess:true");
  309. return;
  310. }
  311. Debug.Log("OnShow:LoginProcess:false");
  312. #endif
  313. }
  314. public static void SummitScore(long score)
  315. {
  316. #if UNITY_IPHONE
  317. GCLeaderBoard.SummitScore(score);
  318. return;
  319. #endif
  320. #if UNITY_ANDROID
  321. // PlayGameServices.submitScore(_leaderBoardID, score);
  322. #endif
  323. }
  324. public void SetScores(List<UserTop> list)
  325. {
  326. scores = list;
  327. }
  328. public List<GameObject> GetItems()
  329. {
  330. return _items;
  331. }
  332. public void SetItems(List<GameObject> list)
  333. {
  334. _items = list;
  335. }
  336. }