Hello everyone,
I’m making a calendar-like app. When the user opens the app, he sees the “sentence of the day” or something like that, and every day has it’s own sentence. If the user swipes vertically, he can change day.
Now, my problem is that I’ve made a class where I initialize an array of my Sentence class, and populate it with the “sentences of the day”. Something like this:
dateTexts = new DateText[13, 32];
dateTexts[1, 1] = new DateText();
dateTexts[1, 2] = new DateText();
dateTexts[1, 3] = new DateText();
...
dateTexts[1, 1].setSetentce("bla bla....");
dateTexts[1, 2].setSetentce("bla bla....");
dateTexts[1, 3].setSetentce("bla bla....");
The problem with this approach is that the program takes too long to load.
I was wondering what you would do to solve this problem.
I was thinking of keeping all those in an excel file, and only load sentences I currently need and not every single one for the whole year, but I need to export my project for iOS and Android as well.
I could also probably save all these sentences with PlayerPrefs the first time I load the app.
Or…I don’t know. What do you suggest or what would you do?
You don’t need to load them all on the first frame right?
Try either making a coroutine that loads 10 at a time, then waits a frame. (yield return true;)
That way the application can load all the graphics for the first frame and wait a fraction of a second to load the text.
This shouldn’t cause a huge slowdown on start. Anyways, you should seperate pure data from code. If you just need a different sentence for each day you can simply store all your sentences in a text file, one sentence each line and use it as TextAsset in Unity. You can use a StringReader to get the sentence for a specific day. The current day of the year can be determined with System.DateTime.Now.DayOfYear which gives you an integer between 1 and 366 (or between 1 an 365)
// C#
public TextAsset textAsset;
string GetSentence1()
{
using (var reader = new System.IO.StringReader(textAsset.text))
{
int year = System.DateTime.Now.DayOfYear;
string str = "";
for (int i = 0; i < year; i++)
str = reader.ReadLine();
return str;
}
}
string GetSentence2()
{
var data = textAsset.text.Split('
');
int year = System.DateTime.Now.DayOfYear;
return data[year % data.Length].Replace(“\r”, “”);
}
There are two implementations using a TextAsset with the sentences. The first one (GetSentence1) is more efficient in the sense of memory consumption and garbage allocation. However it requires to have at least 366 lines in your text asset.
The second one (GetSentence2) is more simple but creates a lot garbage since it splits the whole text into seperate strings. However it allows you to have less than 366 lines. It just repeats them. So if you have just 10 sentences on day 11 you would get sentence 1 again. Same for 21, 31, 41, …
If you have 366 you should go with the first approach. But as i said it shouldn’t really matter in the sense of performance.