using Assets.Scripts; using Assets.Scripts.Apis.Models; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class NewRouteDetailController : PFUIPanel { // Start is called before the first frame update Dictionary caches; GameObject map; ScrollRect scroll; Dictionary FavDict = new Dictionary() { {false,Resources.Load("Images/NewDesign/icon_heji_unlike") }, {true,Resources.Load("Images/NewDesign/icon_heji_like") }, }; protected override void Awake() { caches = new Dictionary(); scroll = transform.Find("Container/Right/Scroll View").GetComponent(); UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag); #if UNITY_ANDROID || UNITY_IOS map = Resources.Load("UI/Prefab/MapList/MapItem-Mobile"); #else map = Resources.Load("UI/Prefab/MapList/MapItem"); #endif } protected override void OnDestroy() { caches = null; Resources.UnloadUnusedAssets(); GC.Collect(); Debug.Log("list empty"); } public override void Show() { base.Show(); if (App.TextureCache.ContainsKey("rotateImage")) { transform.Find("RawImage").GetComponent().texture = App.TextureCache["rotateImage"]; } else if (App.DefaultRotateTexture != null) { transform.Find("RawImage").GetComponent().texture = App.DefaultRotateTexture; } } void Start() { #if UNITY_ANDROID || UNITY_IOS var nav = transform.Find("Tmp/MainNav-mobile").GetComponent(); nav.SetButtonActive(new List { 0, 3, 4, 6, 7 }, 0); UIManager.AddEvent(transform.Find("Container/Left/Fav").gameObject, EventTriggerType.PointerClick, async b => { Assets.Scripts.Apis.JsonResult res = null; var act = ((PointerEventData)b).pointerEnter.GetComponent().sprite.name == "icon_heji_like"; if (act) { res = await ConfigHelper.mapApi.CancelAreaFav(area.Id); } else { res = await ConfigHelper.mapApi.AddAreaFav(area.Id); } if (res.result) { b.selectedObject.GetComponent().sprite = FavDict[!act]; Utils.showToast(null, App.GetLocalString("Success"), type: 1); } else { Utils.showToast(null, res.errMsg); } }); #else transform.Find("MainNav").GetComponent().ShowBack(); #endif var rect = transform.GetComponent(); rect.offsetMax = new Vector2(rect.offsetMax.x, 0); rect.offsetMin = new Vector2(rect.offsetMin.x, 0); BindHeadImage(); } // Update is called once per frame void Update() { if (Input.GetAxis("Mouse ScrollWheel") != 0) { if (scroll.verticalNormalizedPosition <= 0) { startMouse = true; } } else { if (startMouse) { startMouse = false; OnEndDrag(null); } } } MapRouteAreaItem area; int pageIndex = 0, pageSize = 10; bool isEnd = false, startMouse = false; public async void Initial(MapRouteAreaItem area) { isEnd = false; pageIndex = 0; this.area = area; var left = transform.Find("Container/Left"); left.Find("Icon").GetComponent().texture = null; scroll.content.DestroyChildren(); transform.Find("Container/Left/RideContainer/Text").GetComponent() .text = string.Empty; transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent() .text = string.Empty; transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent() .text = string.Empty; transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent() .text = string.Empty; transform.Find("Container/Left/Desc").GetComponent() .text = string.Empty; if (area.BannerImage != null) { Utils.DisplayImageTempDict(left.Find("Icon").GetComponent(), area.CoverImage, caches); Utils.DisplayImageTempDict(left.Find("Thumbnail").GetComponent(), area.CoverImage, caches); } //transform.Find("Container/Left/Fav").GetComponent().sprite; left.Find("Name").GetComponent().text = area.Name; GetData(); GetList(); } private void OnEndDrag(BaseEventData arg0) { Debug.Log(scroll.verticalNormalizedPosition); if (scroll.verticalNormalizedPosition <= 0 && !isEnd) { pageIndex++; GetList(); } } private async void GetData() { var res = await ConfigHelper.mapApi.GetMapRouteAreaDetail(area.Id); if (res.result) { transform.Find("Container/Left/RideContainer/Text").GetComponent() .text = res.data.RideCount.ToString(); transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent() .text = $"{res.data.TotalDistance.ToString("#0")}KM"; transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent() .text = $"{res.data.TotalClimb.ToString("#0")}M"; transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent() .text = $"{res.data.AverageGrade.ToString("#0.00")}%"; transform.Find("Container/Left/Desc").GetComponent() .text = res.data.Description; LayoutRebuilder.ForceRebuildLayoutImmediate(transform.Find("Container/Left/RideContainer").GetComponent()); } } private async void GetList() { var res = await ConfigHelper.mapApi.GetMapRouteAreaDetailList(area.Id, pageIndex, pageSize); if (res.result) { if (res.data.Count > 0) { DisplayMaps(res.data); } else { isEnd = true; } } } void DisplayMaps(List list) { if (map != null) { foreach (var item in list) { var obj = Instantiate(map); obj.GetComponent().Initial(item, caches, area); //obj.SendMessage("Initial", ); obj.transform.SetParent(scroll.content.transform); obj.transform.localScale = new Vector3(1, 1, 1); } } } }