using Assets.Scenes.Ride.Scripts; using Assets.Scripts; using Assets.Scripts.Apis.Models; using System; using System.Linq; 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; Dictionary cacheByte; GameObject map; ScrollRect scroll; Dictionary FavDict; List routeIds { get; set; } protected override void Awake() { FavDict = new Dictionary() { {false,Resources.Load("Images/NewDesign/icon_heji_unlike") }, {true,Resources.Load("Images/NewDesign/icon_heji_like") }, }; caches = new Dictionary(); cacheByte = 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; cacheByte = null; Resources.UnloadUnusedAssets(); GC.Collect(); Debug.Log("list empty"); } public override void Show() { base.Show(); transform.MyDOFade(); 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() { UIManager.AddEvent(transform.Find("Container/Left/Fav").gameObject, EventTriggerType.PointerClick, async b => { Assets.Scripts.Apis.JsonResult res = null; var p = ((PointerEventData)b).pointerEnter; 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) { p.GetComponent().sprite = FavDict[!act]; //Utils.showToast(null, App.GetLocalString("Success"), type: 1); } else { Utils.showToast(null, res.errMsg); } }); #if UNITY_ANDROID || UNITY_IOS var nav = transform.Find("Tmp/MainNav-mobile").GetComponent(); nav.SetButtonActive(new List { 0, 3, 4, 6, 7 }, 0); newNav = nav; #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(); } float timer = 1f; // 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); } } //timer -= Time.deltaTime; //while (timer < 0) //{ // var list = MapUDPService.GetAllOnlineUserList(); // if (list.Count > 0 && routeIds != null) // { // var onlineUserList = list.Where(c => routeIds.Contains(c.RouteId)).ToList(); // //当前集合骑行人数 // var currentUserCount = onlineUserList.Count(); // Debug.Log($"当前集合骑行人数{currentUserCount}"); // } // timer += 1f; //} } 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/DescScroll/Viewport/Content/Desc").GetComponent() .text = string.Empty; if (area.CoverImage != null) { Utils.DisplayImageTempDict(left.Find("Icon").GetComponent(), area.CoverImage, caches , b => { if (b != null) { cacheByte.Add(area.CoverImage, b); } else if(cacheByte.ContainsKey(area.CoverImage)) { b = cacheByte[area.CoverImage]; } #if UNITY_EDITOR var _t = new Texture2D(4, 4, TextureFormat.DXT5, false); #else #if UNITY_IOS var _t = new Texture2D(4, 4, TextureFormat.ASTC_RGB_4x4,false); Debug.Log("使用了壓縮"); #elif UNITY_ANDROID var _t = new Texture2D(4, 4, TextureFormat.ASTC_RGB_4x4,false); Debug.Log("使用了壓縮"); #else //pc var _t = new Texture2D(4, 4, TextureFormat.DXT5,false); #endif #endif _t.LoadImage(b); //Graphics.CopyTexture(t, _t); //Debug.Log($"{t.width},{t.height}"); left.Find("Thumbnail").GetComponent().texture = ScaleTextureCutOut(_t, 0, _t.height / 2, _t.width / 3, _t.height / 3); }); //Utils.DisplayImageTempDict(left.Find("Thumbnail").GetComponent(), area.CoverImage, caches); } Debug.Log(153 + area.IsFav.ToString()); left.Find("Name").GetComponent().text = area.Name; GetData(); GetList(); } Texture2D ScaleTextureCutOut(Texture2D originalTexture, float startX, float startY, float originalWidth, float originalHeight) { originalWidth = Mathf.Clamp(originalWidth, 0, Mathf.Max(originalTexture.width - startX, 0)); originalHeight = Mathf.Clamp(originalHeight, 0, Mathf.Max(originalTexture.height - startY, 0)); Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight)); int maxX = originalTexture.width - 1; int maxY = originalTexture.height - 1; for (int y = 0; y < newTexture.height; y++) { for (int x = 0; x < newTexture.width; x++) { float targetX = x + startX; float targetY = y + startY; int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX)); int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY)); int x2 = Mathf.Min(maxX, x1 + 1); int y2 = Mathf.Min(maxY, y1 + 1); float u = targetX - x1; float v = targetY - y1; float w1 = (1 - u) * (1 - v); float w2 = u * (1 - v); float w3 = (1 - u) * v; float w4 = u * v; Color color1 = originalTexture.GetPixel(x1, y1); Color color2 = originalTexture.GetPixel(x2, y1); Color color3 = originalTexture.GetPixel(x1, y2); Color color4 = originalTexture.GetPixel(x2, y2); Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4), Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4), Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4), Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4) ); newTexture.SetPixel(x, y, color); } } newTexture.anisoLevel = 2; newTexture.Apply(); return newTexture; } 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/DescScroll/Viewport/Content/Desc").GetComponent() .text = res.data.Description; Utils.DisplayImageTempDict(transform.Find("Container/Left/Create/Avatar").GetComponent(), res.data.CreateUserHead, caches); transform.Find("Container/Left/Create/Name").GetComponent().text = res.data.CreateUserName; LayoutRebuilder.ForceRebuildLayoutImmediate(transform.Find("Container/Left/DescScroll").GetComponent().content); transform.Find("Container/Left/Fav").GetComponent().sprite = FavDict[res.data.IsFav]; LayoutRebuilder.ForceRebuildLayoutImmediate(transform.Find("Container/Left/RideContainer").GetComponent()); var userinfo = Resources.Load("UI/Prefab/NewRoute/UserInfo"); transform.Find("Container/Left/NOData").gameObject.SetActive(res.data.UserList.Count == 0); transform.Find("Container/Left/Users").DestroyChildren(); foreach (var item in res.data.UserList) { var info = Instantiate(userinfo); Utils.DisplayImageTempDict(info.transform.Find("Avatar").GetComponent(), item.WxHeadImg, caches); info.transform.Find("NickName").GetComponent().text = item.NickName; info.transform.SetParent(transform.Find("Container/Left/Users")); info.transform.localScale = Vector3.one; } } } private async void GetList() { var res = await ConfigHelper.mapApi.GetMapRouteAreaDetailList(area.Id, pageIndex, pageSize); if (res.result) { if (res.data.RouteList.Count > 0) { DisplayMaps(res.data.RouteList); routeIds = res.data.RouteIds; } 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); } } } }