using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace TowerDevil { public sealed partial class TowerGame { [Serializable] sealed class ArtPart { public string key; public int w, h, u, v; } [Serializable] sealed class KeepLayout { public ArtPart[] parts; } readonly Dictionary keepParts = new Dictionary(); static readonly int[] PortraitIds = { 3, 0, 1, 2, 4, 5, 6, 7 }; static readonly string[] UpgradeIcons = { "boots", "tower", "hammer", "scope" }; static readonly string[] UpgradeEffects = { "+1 MOVE", "+1 TOWER", "+8% DAMAGE", "BOSS FORECAST" }; static readonly string[] ChestIcons = { "woodChest", "silverChest", "goldChest", "legendChest" }; Texture2D keepAtlas; KeepBackdrop keepBackdrop; string displayedPlayerID, gameCenterName; Texture2D gameCenterPhoto; Text headerName, profileName; RawImage headerPortrait, profilePortrait; RectTransform avatarChoices; void SetGameCenterProfile(string playerID, string displayName, byte[] png) { if (displayedPlayerID != playerID || png?.Length == 0) { Destroy(gameCenterPhoto); gameCenterPhoto = null; } displayedPlayerID = playerID; gameCenterName = displayName; if (png != null && png.Length > 0) { var photo = new Texture2D(2, 2, TextureFormat.RGBA32, false); if (photo.LoadImage(png, true)) { Destroy(gameCenterPhoto); gameCenterPhoto = photo; } else Destroy(photo); } RefreshPlayerIdentity(); } void RefreshPlayerIdentity() { string name = string.IsNullOrWhiteSpace(gameCenterName) ? profile.name : gameCenterName; if (headerName) headerName.text = name; if (profileName) profileName.text = name; UpdatePlayerPortrait(headerPortrait); UpdatePlayerPortrait(profilePortrait); if (avatarChoices) avatarChoices.gameObject.SetActive(!gameCenterPhoto); } void UpdatePlayerPortrait(RawImage image) { if (!image) return; if (gameCenterPhoto) { image.texture = gameCenterPhoto; image.uvRect = new Rect(0, 0, 1, 1); } else SetArtPart(image, keepParts["avatar" + Array.IndexOf(PortraitIds, profile.avatar)]); } void LoadKeepArt() { if (keepAtlas && keepParts.Count > 0) return; keepParts.Clear(); keepAtlas = Art("Keep/controls"); var json = Resources.Load("DevilArt/Keep/layout"); foreach (var part in JsonUtility.FromJson(json.text).parts) keepParts.Add(part.key, part); } void KeepScene(string routeName, string painting) { NewPage(routeName); LoadKeepArt(); keepBackdrop = Box(page, "Keep scenery", 0, 0, 750, 1334).gameObject.AddComponent(); keepBackdrop.SetPainting(Art("Keep/" + painting)); keepBackdrop.Fit(page, screen); } RawImage DrawPart(Transform parent, string art, float x, float y, float w, float h, bool contain = false) { var part = keepParts[art]; if (contain) { float scale = Mathf.Min(w / part.w, h / part.h); x += (w - part.w * scale) * .5f; y += (h - part.h * scale) * .5f; w = part.w * scale; h = part.h * scale; } var image = Box(parent, art, x, y, w, h).gameObject.AddComponent(); SetArtPart(image, part); image.raycastTarget = false; return image; } void SetArtPart(RawImage image, ArtPart part) { image.texture = keepAtlas; image.uvRect = new Rect((float)part.u / keepAtlas.width, (float)part.v / keepAtlas.height, (float)part.w / keepAtlas.width, (float)part.h / keepAtlas.height); } Button ArtButton(Transform parent, string name, string art, int x, int y, int w, int h, Action action, bool enabled = true) { var face = DrawPart(parent, art, x, y, w, h); face.name = name; face.raycastTarget = true; var button = face.gameObject.AddComponent