using UnityEngine;
using TMPro; // TextMesh Pro namespace'ini ekliyoruz
using UnityEngine.Networking;
using System;
using System.Collections;
public class FreeGamesDisplay : MonoBehaviour
{
public TMP_Text thisWeekGame1; // Text yerine TMP_Text kullanıyoruz
public TMP_Text thisWeekGame2;
public TMP_Text nextWeekGame1;
public TMP_Text nextWeekGame2;
public TMP_Text timeLeftText;
private string apiUrl = "https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions";
void Start()
{
// API'den veri almayı başlat
StartCoroutine(GetFreeGamesData());
}
IEnumerator GetFreeGamesData()
{
UnityWebRequest www = UnityWebRequest.Get(apiUrl);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
// Gelen JSON verisini işleyin
GameData gameData = JsonUtility.FromJson<GameData>(www.downloadHandler.text);
// Bu haftaki oyunları al
if (gameData.data.Catalog.searchStore.elements.Length > 0)
{
thisWeekGame1.text = gameData.data.Catalog.searchStore.elements[0].title;
DateTime endDate = DateTime.Parse(gameData.data.Catalog.searchStore.elements[0].endDate);
timeLeftText.text = "Bitiş: " + GetRemainingTime(endDate);
}
else
{
thisWeekGame1.text = "Bu hafta bedava oyun yok.";
}
if (gameData.data.Catalog.searchStore.elements.Length > 1)
{
thisWeekGame2.text = gameData.data.Catalog.searchStore.elements[1].title;
}
else
{
thisWeekGame2.text = "";
}
// Gelecek haftaki oyunları al
if (gameData.data.Catalog.searchStore.elements.Length > 2)
{
nextWeekGame1.text = gameData.data.Catalog.searchStore.elements[2].title;
}
else
{
nextWeekGame1.text = "";
}
if (gameData.data.Catalog.searchStore.elements.Length > 3)
{
nextWeekGame2.text = gameData.data.Catalog.searchStore.elements[3].title;
}
else
{
nextWeekGame2.text = "";
}
}
else
{
Debug.LogError("API isteği başarısız: " + www.error);
}
}
// Kalan süreyi hesaplayıp döndürme
string GetRemainingTime(DateTime endDate)
{
TimeSpan remainingTime = endDate - DateTime.Now;
if (remainingTime.TotalSeconds > 0)
{
return string.Format("{0} gün {1} saat {2} dakika {3} saniye",
remainingTime.Days, remainingTime.Hours, remainingTime.Minutes, remainingTime.Seconds);
}
else
{
return "Süre doldu!";
}
}
}
// JSON Verisi için gerekli sınıflar
[System.Serializable]
public class GameData
{
public Data data;
}
[System.Serializable]
public class Data
{
public Catalog Catalog;
}
[System.Serializable]
public class Catalog
{
public SearchStore searchStore;
}
[System.Serializable]
public class SearchStore
{
public Game[] elements;
}
[System.Serializable]
public class Game
{
public string title;
public string endDate;
public string url;
}