First Commit
This commit is contained in:
commit
eed6b79efc
2836 files changed
+948481
No files matched your search
@@ -0,0 +1,386 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
/// <summary>
|
||||
/// Красивая 3D GPS-навигация и экранная метка пути до купленного или выбранного для встречи автомобиля.
|
||||
/// Включает объемный луч-маяк в мире, экранную плашку с расстоянием, стрелку направления и оповещение о прибытии.
|
||||
/// </summary>
|
||||
public class MeetingWaypointHUD : MonoBehaviour
|
||||
{
|
||||
[Header("Ссылки")]
|
||||
[SerializeField] private CarMarketManager marketManager;
|
||||
|
||||
[Header("Настройки метки")]
|
||||
[SerializeField] private float heightAboveCar = 2.4f;
|
||||
[SerializeField] private float arrivalDistance = 8f;
|
||||
|
||||
private Camera mainCam;
|
||||
private GameObject beaconWorldRoot;
|
||||
private GameObject beaconPillar;
|
||||
private Transform activeTargetCar;
|
||||
private CarListing currentListing;
|
||||
private bool hasTriggeredArrival = false;
|
||||
|
||||
// UI элементы на Canvas
|
||||
private GameObject hudContainer;
|
||||
private RectTransform waypointRect;
|
||||
private Image waypointBg;
|
||||
private Image iconImage;
|
||||
private TextMeshProUGUI titleText;
|
||||
private TextMeshProUGUI distanceText;
|
||||
private RectTransform arrowRect;
|
||||
private Image arrowImage;
|
||||
|
||||
// Баннер прибытия
|
||||
private GameObject arrivalBanner;
|
||||
private CanvasGroup arrivalBannerGroup;
|
||||
private float arrivalBannerTimer = 0f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (marketManager == null)
|
||||
marketManager = FindAnyObjectByType<CarMarketManager>();
|
||||
|
||||
CreateHUDHierarchy();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (marketManager != null)
|
||||
{
|
||||
marketManager.OnMeetingArranged += HandleMeetingArranged;
|
||||
marketManager.OnMeetingCleared += HandleMeetingCleared;
|
||||
if (marketManager.ActiveMeetingListing != null)
|
||||
HandleMeetingArranged(marketManager.ActiveMeetingListing);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (marketManager != null)
|
||||
{
|
||||
marketManager.OnMeetingArranged -= HandleMeetingArranged;
|
||||
marketManager.OnMeetingCleared -= HandleMeetingCleared;
|
||||
}
|
||||
|
||||
if (beaconWorldRoot != null)
|
||||
Destroy(beaconWorldRoot);
|
||||
}
|
||||
|
||||
private void CreateHUDHierarchy()
|
||||
{
|
||||
Canvas canvas = FindAnyObjectByType<Canvas>();
|
||||
if (canvas == null)
|
||||
{
|
||||
GameObject canvasObj = new GameObject("WaypointHUDCanvas");
|
||||
canvas = canvasObj.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
canvas.sortingOrder = 95;
|
||||
CanvasScaler scaler = canvasObj.AddComponent<CanvasScaler>();
|
||||
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
|
||||
scaler.referenceResolution = new Vector2(1920, 1080);
|
||||
scaler.matchWidthOrHeight = 0.5f;
|
||||
canvasObj.AddComponent<GraphicRaycaster>();
|
||||
}
|
||||
|
||||
// Корневой контейнер
|
||||
hudContainer = new GameObject("MeetingWaypointHUD");
|
||||
hudContainer.transform.SetParent(canvas.transform, false);
|
||||
hudContainer.SetActive(false);
|
||||
|
||||
// 1. Плашка путевой метки
|
||||
GameObject wpObj = new GameObject("WaypointBadge");
|
||||
wpObj.transform.SetParent(hudContainer.transform, false);
|
||||
waypointRect = wpObj.AddComponent<RectTransform>();
|
||||
waypointRect.sizeDelta = new Vector2(230f, 48f);
|
||||
waypointRect.pivot = new Vector2(0.5f, 0.5f);
|
||||
|
||||
waypointBg = wpObj.AddComponent<Image>();
|
||||
waypointBg.sprite = ProceduralUISprites.GetSlicedRoundedRect(new Color(0.08f, 0.10f, 0.14f, 0.92f), new Color(0.06f, 0.75f, 0.45f, 0.85f), 2f, 14);
|
||||
waypointBg.type = Image.Type.Sliced;
|
||||
waypointBg.raycastTarget = false;
|
||||
|
||||
// Иконка машинки / пина
|
||||
GameObject iconObj = new GameObject("Icon");
|
||||
iconObj.transform.SetParent(wpObj.transform, false);
|
||||
RectTransform iconRt = iconObj.AddComponent<RectTransform>();
|
||||
iconRt.anchorMin = new Vector2(0f, 0.5f);
|
||||
iconRt.anchorMax = new Vector2(0f, 0.5f);
|
||||
iconRt.pivot = new Vector2(0f, 0.5f);
|
||||
iconRt.anchoredPosition = new Vector2(12f, 0f);
|
||||
iconRt.sizeDelta = new Vector2(28f, 28f);
|
||||
|
||||
iconImage = iconObj.AddComponent<Image>();
|
||||
iconImage.sprite = ProceduralUISprites.GetMapPinIcon(64, new Color(0.1f, 0.95f, 0.55f));
|
||||
iconImage.raycastTarget = false;
|
||||
|
||||
// Название автомобиля
|
||||
GameObject titleObj = new GameObject("CarTitle");
|
||||
titleObj.transform.SetParent(wpObj.transform, false);
|
||||
RectTransform titleRt = titleObj.AddComponent<RectTransform>();
|
||||
titleRt.anchorMin = new Vector2(0f, 0.5f);
|
||||
titleRt.anchorMax = new Vector2(1f, 1f);
|
||||
titleRt.offsetMin = new Vector2(46f, 0f);
|
||||
titleRt.offsetMax = new Vector2(-12f, -4f);
|
||||
|
||||
titleText = titleObj.AddComponent<TextMeshProUGUI>();
|
||||
titleText.fontSize = 14f;
|
||||
titleText.fontStyle = FontStyles.Bold;
|
||||
titleText.color = Color.white;
|
||||
titleText.alignment = TextAlignmentOptions.Left;
|
||||
titleText.raycastTarget = false;
|
||||
|
||||
// Дистанция в метрах
|
||||
GameObject distObj = new GameObject("Distance");
|
||||
distObj.transform.SetParent(wpObj.transform, false);
|
||||
RectTransform distRt = distObj.AddComponent<RectTransform>();
|
||||
distRt.anchorMin = new Vector2(0f, 0f);
|
||||
distRt.anchorMax = new Vector2(1f, 0.5f);
|
||||
distRt.offsetMin = new Vector2(46f, 4f);
|
||||
distRt.offsetMax = new Vector2(-12f, 0f);
|
||||
|
||||
distanceText = distObj.AddComponent<TextMeshProUGUI>();
|
||||
distanceText.fontSize = 13f;
|
||||
distanceText.fontStyle = FontStyles.Bold;
|
||||
distanceText.color = new Color(0.2f, 0.95f, 0.5f);
|
||||
distanceText.alignment = TextAlignmentOptions.Left;
|
||||
distanceText.raycastTarget = false;
|
||||
|
||||
// Стрелка направления на краю экрана
|
||||
GameObject arrowObj = new GameObject("DirectionArrow");
|
||||
arrowObj.transform.SetParent(wpObj.transform, false);
|
||||
arrowRect = arrowObj.AddComponent<RectTransform>();
|
||||
arrowRect.sizeDelta = new Vector2(20f, 20f);
|
||||
arrowRect.pivot = new Vector2(0.5f, 0.5f);
|
||||
|
||||
arrowImage = arrowObj.AddComponent<Image>();
|
||||
arrowImage.sprite = ProceduralUISprites.GetPointerArrow(32, new Color(0.1f, 0.95f, 0.55f));
|
||||
arrowImage.raycastTarget = false;
|
||||
arrowObj.SetActive(false);
|
||||
|
||||
// 2. Всплывающий баннер прибытия на локацию
|
||||
arrivalBanner = new GameObject("ArrivalBanner");
|
||||
arrivalBanner.transform.SetParent(canvas.transform, false);
|
||||
RectTransform bannerRt = arrivalBanner.AddComponent<RectTransform>();
|
||||
bannerRt.anchorMin = new Vector2(0.5f, 1f);
|
||||
bannerRt.anchorMax = new Vector2(0.5f, 1f);
|
||||
bannerRt.pivot = new Vector2(0.5f, 1f);
|
||||
bannerRt.anchoredPosition = new Vector2(0f, -40f);
|
||||
bannerRt.sizeDelta = new Vector2(560f, 72f);
|
||||
|
||||
Image bannerBg = arrivalBanner.AddComponent<Image>();
|
||||
bannerBg.sprite = ProceduralUISprites.GetSlicedRoundedRect(new Color(0.06f, 0.08f, 0.12f, 0.94f), new Color(0.10f, 0.85f, 0.45f, 0.9f), 2f, 16);
|
||||
bannerBg.type = Image.Type.Sliced;
|
||||
bannerBg.raycastTarget = false;
|
||||
|
||||
arrivalBannerGroup = arrivalBanner.AddComponent<CanvasGroup>();
|
||||
arrivalBannerGroup.alpha = 0f;
|
||||
|
||||
GameObject bannerTextObj = new GameObject("BannerText");
|
||||
bannerTextObj.transform.SetParent(arrivalBanner.transform, false);
|
||||
RectTransform btRt = bannerTextObj.AddComponent<RectTransform>();
|
||||
btRt.anchorMin = Vector2.zero;
|
||||
btRt.anchorMax = Vector2.one;
|
||||
btRt.offsetMin = new Vector2(20f, 8f);
|
||||
btRt.offsetMax = new Vector2(-20f, -8f);
|
||||
|
||||
TextMeshProUGUI bText = bannerTextObj.AddComponent<TextMeshProUGUI>();
|
||||
bText.fontSize = 15f;
|
||||
bText.alignment = TextAlignmentOptions.Center;
|
||||
bText.color = Color.white;
|
||||
bText.text = "<color=#10b981><b>📍 ВЫ ПРИБЫЛИ НА МЕСТО ВСТРЕЧИ!</b></color>\nОсмотрите кузов, проверьте наличие деталей и состояние колес.";
|
||||
bText.raycastTarget = false;
|
||||
|
||||
arrivalBanner.SetActive(false);
|
||||
}
|
||||
|
||||
private void HandleMeetingArranged(CarListing listing)
|
||||
{
|
||||
currentListing = listing;
|
||||
hasTriggeredArrival = false;
|
||||
|
||||
if (listing != null && listing.spawnedInstance != null)
|
||||
{
|
||||
activeTargetCar = listing.spawnedInstance.transform;
|
||||
CreateWorldBeacon(activeTargetCar.position);
|
||||
hudContainer.SetActive(true);
|
||||
titleText.text = $"{listing.catalogItem.carTitle} ({listing.year})";
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleMeetingCleared()
|
||||
{
|
||||
currentListing = null;
|
||||
activeTargetCar = null;
|
||||
if (beaconWorldRoot != null) beaconWorldRoot.SetActive(false);
|
||||
if (hudContainer != null) hudContainer.SetActive(false);
|
||||
}
|
||||
|
||||
private void CreateWorldBeacon(Vector3 pos)
|
||||
{
|
||||
if (beaconWorldRoot == null)
|
||||
{
|
||||
beaconWorldRoot = new GameObject("MeetingGPS_WorldBeacon");
|
||||
|
||||
// Вертикальный полупрозрачный столб света
|
||||
GameObject pillar = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
||||
Destroy(pillar.GetComponent<Collider>());
|
||||
pillar.transform.SetParent(beaconWorldRoot.transform, false);
|
||||
// Столб начинается над крышей машины и виден издалека; вблизи скрывается, чтобы не мешать осмотру
|
||||
pillar.transform.localPosition = new Vector3(0f, 28f, 0f);
|
||||
pillar.transform.localScale = new Vector3(0.35f, 24f, 0.35f);
|
||||
beaconPillar = pillar;
|
||||
|
||||
Renderer r = pillar.GetComponent<Renderer>();
|
||||
r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
||||
r.sharedMaterial = PartCatalog.CreateUnlitMaterial(new Color(0.15f, 1f, 0.55f, 0.35f), true);
|
||||
|
||||
// Наземный круг
|
||||
GameObject ring = GameObject.CreatePrimitive(PrimitiveType.Quad);
|
||||
Destroy(ring.GetComponent<Collider>());
|
||||
ring.transform.SetParent(beaconWorldRoot.transform, false);
|
||||
ring.transform.localPosition = new Vector3(0f, 0.15f, 0f);
|
||||
ring.transform.localRotation = Quaternion.Euler(90f, 0f, 0f);
|
||||
ring.transform.localPosition = new Vector3(0f, 0.03f, 0f);
|
||||
ring.transform.localScale = new Vector3(7f, 7f, 1f);
|
||||
|
||||
Renderer ringRend = ring.GetComponent<Renderer>();
|
||||
ringRend.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
||||
Material ringMat = PartCatalog.CreateUnlitMaterial(Color.white, true);
|
||||
Texture2D ringTex = new Texture2D(128, 128, TextureFormat.RGBA32, false);
|
||||
for (int y = 0; y < 128; y++)
|
||||
for (int x = 0; x < 128; x++)
|
||||
{
|
||||
float d = Vector2.Distance(new Vector2(x, y), new Vector2(63.5f, 63.5f)) / 64f;
|
||||
float a = Mathf.Clamp01(1f - Mathf.Abs(d - 0.85f) * 12f) * 0.8f;
|
||||
ringTex.SetPixel(x, y, new Color(0.2f, 1f, 0.55f, a));
|
||||
}
|
||||
ringTex.Apply();
|
||||
ringMat.mainTexture = ringTex;
|
||||
if (ringMat.HasProperty("_BaseMap")) ringMat.SetTexture("_BaseMap", ringTex);
|
||||
ringRend.sharedMaterial = ringMat;
|
||||
}
|
||||
|
||||
beaconWorldRoot.transform.position = pos;
|
||||
beaconWorldRoot.SetActive(true);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateArrivalBanner();
|
||||
|
||||
if (activeTargetCar == null)
|
||||
{
|
||||
if (hudContainer != null && hudContainer.activeSelf)
|
||||
hudContainer.SetActive(false);
|
||||
if (beaconWorldRoot != null && beaconWorldRoot.activeSelf)
|
||||
beaconWorldRoot.SetActive(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (beaconPillar != null && Camera.main != null)
|
||||
{
|
||||
bool far = Vector3.Distance(Camera.main.transform.position, activeTargetCar.position) > 18f;
|
||||
if (beaconPillar.activeSelf != far) beaconPillar.SetActive(far);
|
||||
}
|
||||
if (beaconWorldRoot != null) beaconWorldRoot.transform.position = activeTargetCar.position;
|
||||
|
||||
// Если игрок уже сел внутрь машины — выключаем метку
|
||||
VehicleInteraction vi = activeTargetCar.GetComponent<VehicleInteraction>();
|
||||
if (vi != null && vi.isPlayerInside)
|
||||
{
|
||||
HandleMeetingCleared();
|
||||
return;
|
||||
}
|
||||
|
||||
if (mainCam == null || !mainCam.gameObject.activeInHierarchy)
|
||||
mainCam = Camera.main ?? FindAnyObjectByType<Camera>();
|
||||
|
||||
if (mainCam == null) return;
|
||||
|
||||
Vector3 targetWorldPos = activeTargetCar.position + Vector3.up * heightAboveCar;
|
||||
float dist = Vector3.Distance(mainCam.transform.position, targetWorldPos);
|
||||
|
||||
// Обновляем текст расстояния
|
||||
if (dist >= 1000f)
|
||||
distanceText.text = $"{(dist / 1000f):F1} км до авто";
|
||||
else
|
||||
distanceText.text = $"{dist:F0} м до встречи";
|
||||
|
||||
// Проверяем приближение
|
||||
if (dist <= arrivalDistance && !hasTriggeredArrival)
|
||||
{
|
||||
hasTriggeredArrival = true;
|
||||
ShowArrivalBanner();
|
||||
}
|
||||
|
||||
// 3D -> 2D Screen Position
|
||||
Vector3 screenPos = mainCam.WorldToScreenPoint(targetWorldPos);
|
||||
bool isBehind = screenPos.z < 0f;
|
||||
|
||||
float marginX = 130f;
|
||||
float marginY = 50f;
|
||||
Vector2 screenCenter = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
|
||||
|
||||
bool isOffscreen = isBehind || screenPos.x < marginX || screenPos.x > (Screen.width - marginX)
|
||||
|| screenPos.y < marginY || screenPos.y > (Screen.height - marginY);
|
||||
|
||||
if (isOffscreen)
|
||||
{
|
||||
if (isBehind)
|
||||
{
|
||||
screenPos.x = Screen.width - screenPos.x;
|
||||
screenPos.y = Screen.height - screenPos.y;
|
||||
}
|
||||
|
||||
Vector2 fromCenter = (new Vector2(screenPos.x, screenPos.y) - screenCenter).normalized;
|
||||
if (fromCenter == Vector2.zero) fromCenter = Vector2.up;
|
||||
|
||||
float clampedX = Mathf.Clamp(screenCenter.x + fromCenter.x * (screenCenter.x - marginX), marginX, Screen.width - marginX);
|
||||
float clampedY = Mathf.Clamp(screenCenter.y + fromCenter.y * (screenCenter.y - marginY), marginY, Screen.height - marginY);
|
||||
|
||||
waypointRect.position = new Vector3(clampedX, clampedY, 0f);
|
||||
|
||||
// Поворачиваем стрелку в направлении машины
|
||||
arrowRect.gameObject.SetActive(true);
|
||||
float angle = Mathf.Atan2(fromCenter.y, fromCenter.x) * Mathf.Rad2Deg - 90f;
|
||||
arrowRect.rotation = Quaternion.Euler(0f, 0f, angle);
|
||||
arrowRect.anchoredPosition = new Vector2(fromCenter.x * 125f, fromCenter.y * 32f);
|
||||
}
|
||||
else
|
||||
{
|
||||
waypointRect.position = screenPos;
|
||||
arrowRect.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
// Мягкая пульсация иконки
|
||||
float pulse = 1f + Mathf.Sin(Time.time * 4f) * 0.08f;
|
||||
iconImage.transform.localScale = new Vector3(pulse, pulse, 1f);
|
||||
}
|
||||
|
||||
private void ShowArrivalBanner()
|
||||
{
|
||||
if (arrivalBanner == null) return;
|
||||
arrivalBanner.SetActive(true);
|
||||
arrivalBannerTimer = 5f;
|
||||
PhoneAudioFeedback.Instance.PlayArrival();
|
||||
}
|
||||
|
||||
private void UpdateArrivalBanner()
|
||||
{
|
||||
if (arrivalBanner == null || !arrivalBanner.activeSelf) return;
|
||||
|
||||
if (arrivalBannerTimer > 0f)
|
||||
{
|
||||
arrivalBannerTimer -= Time.deltaTime;
|
||||
arrivalBannerGroup.alpha = Mathf.MoveTowards(arrivalBannerGroup.alpha, 1f, Time.deltaTime * 3f);
|
||||
}
|
||||
else
|
||||
{
|
||||
arrivalBannerGroup.alpha = Mathf.MoveTowards(arrivalBannerGroup.alpha, 0f, Time.deltaTime * 2f);
|
||||
if (arrivalBannerGroup.alpha <= 0.01f)
|
||||
arrivalBanner.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user