123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- 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<GameObject> _items = new List<GameObject>();
- //private List<GPGScore> _scores = new List<GPGScore>();
- public List<UserTop> scores = new List<UserTop>();
- 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<GCLeaderBoard>();
- Debug.Log("UILeaderBoard::Started");
- #endif
- _grid = this.gameObject.GetComponentInChildren<UIGrid>();
- }
- 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<UIPanel>(_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<UIDragPanelContents>();
- //go.gameObject.AddComponent<BoxCollider>();
- //go.gameObject.GetComponent<BoxCollider>().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<UILabel>(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<UILabel>(go.gameObject);
- var pos2 = NGUITools.AddChild<UILabel>(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<UILabel>(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<UILabel>(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<Action> _postAuthActions = new List<Action>();
- //private List<Action> _postRequestActions = new List<Action>();
- #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<string, object> 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<GPGScore> 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<UserTop> list)
- {
- scores = list;
- }
- public List<GameObject> GetItems()
- {
- return _items;
- }
- public void SetItems(List<GameObject> list)
- {
- _items = list;
- }
- }
|