-1

so I'm making a calendar app in unity, but not just any calendar.. it's a custom calendar app for my friend because he doesn't like georgian, anyway my question is: I have Date.TimeNow.Hour set to get the current hour in the device and i did the calculations for the calendar, the only thing im struggling with is calling the function that does all these calculations only each day. Here is an example:

 void Update()
{
    int hours = DateTime.Now.Hour;
    if (hours == 0)
    {
        calculateMonth();
    }
    
    solarHijri.text = $"{day:D2}-{month:D2}-{year:D2}";
}

So for this code i want for every new day (When hour reaches 0 or in other words 12AM) the calculateMonth function activates, it increases day by one and the other calculations are not really important. i know Update calls each frame but i wanna delay the call for 86400 seconds (a day), i tried using this method but the delay starts only when you open the app + it's not the way to do it.

void calculateMonth()
{
        day++;
        Debug.Log("Day today is: "+ day);
    if (month == 1 || month == 2 || month == 3 || month == 4 || month == 5)
    {
        if (day >= 31)
        {
            month++;
            day = 1;
        }

etc

Younis
  • 13
  • 1

1 Answers1

0

If you want your app to run in background, you'll need to setup a android service: https://developer.android.com/guide/components/services

Here's a example on how to do it on unity: https://github.com/nintendaii/unity-background-service

  • I think there is another way to do this, this seems too complicated i just want a code solution to fix my calendar like all calendar apps use, or do they all run in the background? – Younis Jul 11 '22 at 01:18
  • @Younis if you want a reliable calendar, you need a service to run in background, otherwise if you have an activity (unity app) out of screen, android will pause it and may kill it (reclaim memory). Causing your user to miss all events in its calendar. Alternatively for it to work without a service, you would need your app to be foreground all the time, unfeasible. – Not a privileged user Jul 11 '22 at 15:19