using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace UltraCombos.Marvel.DrawHeroes { public class RoleLocationManager : MonoBehaviour { public RectTransform highlight; public RectTransform magnify; public Vector4 magnifyScale; [Header("Debug")] public bool mode = false; [Range(0, 20)] public int index = 0; Dictionary roles; RectTransform target_role; List target_positions; private void Start() { var role_images = GetComponentsInChildren(); roles = new Dictionary(); for (int i = 0; i < role_images.Length; i++) { if (i == 0) continue; var img = role_images[i]; roles.Add(img.name, img); img.gameObject.SetActive(false); } #if UNITY_EDITOR if (string.IsNullOrEmpty(AppData.Instance.roleId)) { AppData.Instance.roleId = "ca"; } #endif string id = $"Role-{AppData.Instance.roleId}"; if (roles.ContainsKey(id)) { var role = roles[id]; role.texture = AppData.Instance.lastPainting; role.gameObject.SetActive(true); //highlight.position = roles[id].transform.position; target_role = roles[id].rectTransform; } var canvas = GetComponentInParent(); var size = canvas.pixelRect.size; size = (transform.parent as RectTransform).sizeDelta; var half_width = size.x * 0.5f; var half_height = size.y * 0.5f; target_positions = new List(); for (int i = 0; i < 3; i++) { target_positions.Add(new Vector3(Random.Range(-half_width, half_width), Random.Range(-half_height, half_height))); } //target_positions.Add(target_role.position); var pos = target_role.GetComponentInChildren().Position; target_positions.Add(new Vector3(pos.x, pos.y)); if (mode == false || Application.isEditor == false) { StartCoroutine(Shift()); } Utility.Pool.SoundPool.Instance.PlayOneShot("focus.aif"); } private void FixedUpdate() { var imgs = new RawImage[roles.Count]; roles.Values.CopyTo(imgs, 0); var i = Mathf.Clamp(index, 0, roles.Count - 1); var rt = imgs[i].rectTransform; var smooth = Time.fixedDeltaTime * 8.0f; if (mode && Application.isEditor) { var pos = rt.GetComponentInChildren().Position; highlight.position = Vector2.Lerp(highlight.position, pos, smooth); } float sc = Mathf.Lerp(magnifyScale.z, magnifyScale.w, Mathf.InverseLerp(magnifyScale.x, magnifyScale.y, rt.localScale.x)); magnify.localScale = Vector3.one * Mathf.Lerp(magnify.localScale.x, sc, smooth); } private IEnumerator Shift() { float smooth = 0.07f; for (int i = 0; i < target_positions.Count - 1; i++) { var pos = target_positions[i]; float timestamp = Time.time; float duration = Random.Range(0.2f, 0.8f); while (Time.time - timestamp < duration) { highlight.anchoredPosition = Vector2.Lerp(highlight.anchoredPosition, new Vector2(pos.x, pos.y), smooth); yield return null; } } while (true) { highlight.position = Vector3.Lerp(highlight.position, target_positions[target_positions.Count - 1], smooth); yield return null; } } } }