using System; using System.Collections.Generic; using UnityEngine; using System.Collections; using UnityEngine.SocialPlatforms.GameCenter; public class UILeaderBoard : MonoBehaviour { [SerializeField] private UIButton Home; [SerializeField] private UIAtlas _atlas; [SerializeField] private string _spriteName; [SerializeField] private GameObject _scrollRoot; [SerializeField] private float Offset; [SerializeField] private UIFont _font; private int _maxScroll; private List _items = new List(); //private List _scores = new List(); public List scores = new List(); private WaitForSeconds wait = new WaitForSeconds(0.5f); //public bool _isScoresCreated = false; public UIButton Up; public UIButton Down; private UIGrid _grid; public static UILeaderBoard Instance; #if UNITY_ANDROID private const string _leaderBoardID = "CgkIl5Kk5v8bEAIQBw"; #endif // Use this for initialization void Start () { Instance = this; //Home.onClick.Add(new EventDelegate(OnHome)); #if UNITY_ANDROID Init(); #endif #if UNITY_IPHONE //gameObject.AddComponent(); Debug.Log("UILeaderBoard::Started"); #endif _grid = this.gameObject.GetComponentInChildren(); } void Update() { } // public void Reset() { //return; for (int i = 0; i < _items.Count; i++) { Destroy(_items[i]); } _items.Clear(); StartLoadScores(); } public void StartLoadScores() { StartCoroutine(LoadScores()); } void OnEnable() { //ServerGiftManager.Instance.RequestWeeksUpdate(week => //{ // if (string.IsNullOrEmpty(week)) // { // Debug.Log("Error"); // //todo: server error, or wrong number filled // } // else // { // Label.text = string.Format(week.ToString()); // } //}); } public IEnumerator LoadScores() { if (scores == null) { yield break; } for (int i = 0; i < scores.Count; i++) { if (i % 10 == 0) { yield return wait; } // ---- CREATE SPRITE var panel = NGUITools.AddChild(_scrollRoot);//.AddSprite(_scrollRoot, _atlas, _spriteName); var go = panel.transform; Destroy(panel); //go.MakePixelPerfect(); //go.transform.localPosition = new Vector3(0, -i * (go.height + 5) + Offset, 0); go.gameObject.AddComponent(); //go.gameObject.AddComponent(); //go.gameObject.GetComponent().size = new Vector3(400, 110, 0); //go.type = UISprite.Type.Simple; //go.width = 450; //go.height = 110; // ---- ADD USER_PLACE TO SPRITE var place = NGUITools.AddChild(go.gameObject); place.name = "Place"; place.pivot = UIWidget.Pivot.Left; place.font = _font; place.color = new Color(249f / 255f, 205f / 255f, 82f / 255f, 1); //place.effectStyle = UILabel.Effect.Outline; place.effectColor = Color.white; place.text = (i + 1).ToString() + "."; place.depth = 0; place.transform.localPosition = new Vector3(-180, 10, -1); place.transform.localScale = new Vector3(1f, 1f, 1); // ---- ADD USER_NAME TO SPRITE var pos = NGUITools.AddChild(go.gameObject); var pos2 = NGUITools.AddChild(go.gameObject); pos.name = "FacebookUserName"; pos2.name = "FacebookSecondName"; pos2.pivot = pos.pivot = UIWidget.Pivot.Left; pos.font = _font; pos.color = new Color(249f/255f, 205f / 255f, 82f / 255f, 1); //pos.effectStyle = UILabel.Effect.Outline; pos.effectColor = Color.white; pos.text = scores[i].User.FirstName;// +" " + _scores[i].User.LastName; pos.depth = 0; pos.transform.localPosition = new Vector3(-140, 10, -1); pos.transform.localScale = new Vector3(0.9f, 0.9f, 1); // ---- ADD USER_SCORE TO SPRITE var lname = NGUITools.AddChild(go.gameObject); lname.name = "Distance"; lname.font = _font; lname.color = Color.white; // lname.effectStyle = UILabel.Effect.Outline; lname.effectColor = new Color(249f / 255f, 205f / 255f, 82f / 255f, 1); ; lname.text = scores[i].User.Score.ToString() + " Stig"; lname.depth = 0; lname.transform.localPosition = new Vector3(35, 10, 0); lname.transform.localScale = new Vector3(0.9f, 0.9f, 1); // ---- ADD USER_gifts TO SPRITE //var gift = NGUITools.AddChild(go.gameObject); //gift.name = "Gifts"; //gift.font = _font; //gift.text = _scores[i].User.Gifts.ToString(); //gift.depth = 0; //gift.transform.localPosition = new Vector3(160, -22, 0); //gift.transform.localScale = new Vector3(0.6f, 0.6f, 1); if (i < 3) { // ---- ADD SANTA_ICONS_TOP_3 TO SPRITE string spriteName = ""; //int idBadge = _scores[i].User.SantaBadge; switch (i) { case 0: spriteName = "medal_gold"; break; case 1: spriteName = "medal_silver"; break; case 2: spriteName = "medal_bronze"; break; } var santa = NGUITools.AddSprite(go.gameObject, _atlas, spriteName); santa.name = "santaBadge"; santa.transform.localPosition = new Vector3(160, 10, -1); santa.MakePixelPerfect(); santa.width = 63; santa.height = 63; santa.depth = 0; } _items.Add(go.gameObject); _grid.Reposition(); } //_isScoresCreated = true; yield break; } // private bool _initDone; public UILabel Label; #if UNITY_ANDROID private void Init() { if (_initDone) { return; } _initDone = true; /* GPGManager.authenticationSucceededEvent += OnAuthDone; GPGManager.authenticationFailedEvent += OnAuthFail; GPGManager.submitScoreFailedEvent += OnSubmitScoreFail; GPGManager.submitScoreSucceededEvent += OnSubmitScoreDone; GPGManager.loadScoresFailedEvent += OnGetLeadeboardFail; GPGManager.loadScoresSucceededEvent += OnGetLeadeboardDone;*/ //FileLog.Init(Application.persistentDataPath,"leaderboard",true,true,true,FileLog.LogLevels.Log); LoginProcess(); Debug.Log("Init"); } #endif //private List _postAuthActions = new List(); //private List _postRequestActions = new List(); #if UNITY_ANDROID private bool LoginProcess() { // if (!PlayGameServices.isSignedIn()) { //#if UNITY_IPHONE // if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_id", ""))) // { // PlayGameServices.init(PlayerPrefs.GetString("user_id"), true); // } // else // { // PlayGameServices.authenticate(); // } //#else if (!string.IsNullOrEmpty(PlayerPrefs.GetString("user_id", ""))) { //PlayGameServices.attemptSilentAuthentication(); } else { // PlayGameServices.authenticate(); Debug.Log("LoginProcess:authenticate"); } //#endif return false; } return true; } private void OnAuthDone(string userId) { Debug.Log("OnAuthDone " + userId); PlayerPrefs.SetString("user_id",userId); if (NGUIMenuScript.Instance.getCurrentMenu() == NGUIMenuScript.NGUIMenus.LeaderBoard) { RequestLeaderBoard(); } } private void OnAuthFail(string fail) { Debug.LogError("OnAuthFail " + fail); //PlayGameServices.authenticate(); } private void OnSubmitScoreDone(string s, Dictionary dictionary) { /* Debug.Log("OnSubmitScoreDone " + s); if (NGUIMenuScript.Instance.getCurrentMenu() == NGUIMenuScript.NGUIMenus.LeaderBoard) { RequestLeaderBoard(); }*/ } private void OnSubmitScoreFail(string a, string b) { //Debug.Log("OnSubmitScoreFail " + a + " : " + b); } private void OnGetLeadeboardDone(/*List gpgScores*/) { /*Debug.Log("OnGetLeadeboardDone " + gpgScores.Count); _scores = gpgScores; Reset();*/ } private void OnGetLeadeboardFail(string a, string b) { //Debug.Log("OnGetLeadeboardFail " + a + " : " + b); } #endif private void OnHome() { if (NGUIMenuScript.Instance.CurStatePopup == NGUIMenuScript.StatesMenuForPopup.Popup || NGUIMenuScript.Instance.CurStatePopup == NGUIMenuScript.StatesMenuForPopup.GameOver) { //NGUIMenuScript.Instance.CloseMenu(NGUIMenuScript.NGUIMenus.LeaderBoardNew); //NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.Banner); InGameScriptCS.Instance.LoadStartSanta(6); return; } NGUIMenuScript.Instance.CloseMenu(NGUIMenuScript.NGUIMenus.LeaderBoardNew); NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.Menu); } /* private void OnDrag(Vector2 drag) { _scrollRoot.transform.localPosition+= Vector3.up * drag.y; if (_scrollRoot.transform.localPosition.y < 0) { _scrollRoot.transform.localPosition = Vector3.zero; } if (_scrollRoot.transform.localPosition.y > _maxScroll) { _scrollRoot.transform.localPosition = new Vector3(0, _maxScroll,0); } }*/ #if UNITY_ANDROID public void RequestLeaderBoard() { //PlayGameServices.showLeaderboard(_leaderBoardID, GPGLeaderboardTimeScope.AllTime); } #endif public void OnShow() { #if UNITY_IPHONE //GCLeaderBoard.Show(); return; #endif #if UNITY_ANDROID Init(); if (LoginProcess()) { RequestLeaderBoard(); Debug.Log("OnShow:LoginProcess:true"); return; } Debug.Log("OnShow:LoginProcess:false"); #endif } public static void SummitScore(long score) { #if UNITY_IPHONE GCLeaderBoard.SummitScore(score); return; #endif #if UNITY_ANDROID // PlayGameServices.submitScore(_leaderBoardID, score); #endif } public void SetScores(List list) { scores = list; } public List GetItems() { return _items; } public void SetItems(List list) { _items = list; } }