- Home /
How optimization cache with more 2.000 images
Hi everyone, my team and I are developing a mobile game that uses more than 2,000 images, at the moment the initial upload async is through Cache, but it takes more than 50 seconds to complete it. Here is my code:
assetLoadRequest = Resources.LoadAsync<Sprite>(line);
yield return assetLoadRequest.asset;
int ft_id = int.Parse(curFileName);
tmpSprite = assetLoadRequest.asset as Sprite;
tmpInt = tmpSprite.texture.width;
Globals.features.Add(ft_id, tmpSprite);
We need to use these number of images because our games is based on it.
Is there any way to optimize the loading time to reduce drastically the start up?
Thank you all <3
EDIT:
I have N characters in my project, when the user clicks on a character, the client randomly chooses 150 images of 2,000, and inserts them in the character's clothing, if the user clicks on the same character, the 150 images are chosen by randomly again
$$anonymous$$aybe you can try to compress the sprites to reduce the size it needs to load and thus also increase the load speed?
I think that compression would increase the upload time ..
Loading and uploading Nk of images are quite different issues. I think you should probably clarify what you're after here exactly
When loading nk of images at game startup the issue is mostly figuring out the order and time at which those images needs to be in memory. You never really need 1000 images at time zero but some % of them and the rest can load in bg thread.
When uploading nk of images to some kind of server you need a system that deter$$anonymous$$es which images must be sent and which ones server has already cached from previous sessions. You do that by calculating hashes for your files and comparing them with hashes calculated on server files. And to do that your hash algorithm must always generate identical hashes for identical files (!).
Actually we don't use AssetBundle, I have everything in my application.
We need 2,000 images in the Cache, because in our project we randomly need 150 images at a time, but its are random, and to avoid stuttering, we have opted to store everything in Cache
Answer by andrew-lukasik · Jun 05, 2020 at 01:54 PM
You absolutely need to know that randomness in CS isn't, well, random at all. You can predict future set of "random" numbers by setting up System.Random
's seed number manually. And this is how you can predetermine which images to load and which to ignore (or queue to be loaded in bg).
Here is an example of how to use System.Random to generate predictable indices. Indices can well represent images you need to load before any human input happened:
public void TestGeneratePredictablyRandomNumbers ()
{
int seed = 123456789;
// int seed = System.DateTime.Now.Millisecond;
int click = 1;
var arr = new int[ 150 ];
GetRandomNumbers( numbers:arr , min:0 , max:3000 , seed:seed , generation: click );
string run_1 = $"click #{click}: {tostring(arr)}";
click++;
GetRandomNumbers( numbers:arr , min:0 , max:3000 , seed:seed , generation: click );
string run_2 = $"click #{click}: {tostring(arr)}";
click++;
GetRandomNumbers( numbers:arr , min:0 , max:3000 , seed:seed , generation: click );
string run_3 = $"click #{click}: {tostring(arr)}";
click -= 2;
GetRandomNumbers( numbers:arr , min:0 , max:3000 , seed:seed , generation: click );
string run_4 = $"click #{click}: {tostring(arr)}";
Debug.Log($"{nameof(TestGeneratePredictablyRandomNumbers)}():\n\t{run_1}\n\t{run_2}\n\t{run_3}\n\t{run_4}\n");
string tostring (int[] values)
{
var sb = new System.Text.StringBuilder();
foreach( int num in values ) { sb.Append(num); sb.Append(','); }
return sb.ToString();
}
}
public void GetRandomNumbers ( int[] numbers , int min , int max , int seed , int generation )
{
var rnd = new System.Random( seed * generation );
for( int i=0 ; i<numbers.Length ; i++ )
numbers[i] = rnd.Next( min , max );
}
TestGeneratePredictablyRandomNumbers
generates these numbers:
TestGeneratePredictablyRandomNumbers():
click #1: 1525,533,1865,1208,2750,2058,240,2715,1669,2186,1889,2985,2859,1417,2743,1650,2667,286,1958,2343,2455,2254,2685,1633,1167,2212,1974,2491,339,420,2219,2736,2921,1994,888,412,1620,457,398,2475,2624,1842,1107,1302,2239,1769,2497,1595,2383,1132,2894,1769,1446,2633,1365,2270,847,231,41,537,83,748,2375,1248,2967,2152,63,865,529,2331,29,2210,2888,2482,2719,613,1147,1383,2394,2398,2715,378,107,2207,526,449,1290,288,629,1618,2564,1388,416,2861,2391,1875,2466,2859,1335,87,1706,1631,1066,52,1102,684,1881,1964,2913,752,1123,2463,837,643,822,2705,640,168,721,2517,862,2775,235,1911,2767,1640,1793,26,90,844,1146,1287,48,2307,692,1083,2312,55,1104,2842,1567,2326,374,2877,495,100,551,2773,2038,2686,
click #2: 871,1614,1427,743,1882,2439,763,1104,405,551,2903,1568,821,1427,2541,209,2748,587,1884,743,2460,1965,2396,169,235,2846,1146,2920,2038,598,877,1113,1952,1023,2848,1535,150,2772,2768,2078,732,1536,1858,1784,1757,1154,982,1819,1327,1600,1558,1383,1033,803,2145,1906,2218,1257,508,2036,1293,842,2065,2807,2674,1789,2616,2798,1578,1006,59,2976,819,2805,10,923,107,611,1412,2080,1863,2326,1593,438,2039,2494,80,1149,1877,942,2316,1892,2264,732,785,2890,2471,2051,2110,2968,1538,1184,241,321,1540,1582,563,1227,792,1222,1798,1606,2845,1427,172,1966,2248,1627,767,180,1709,1467,920,635,1689,1166,711,87,2020,120,1452,1056,1501,1443,542,679,2085,1272,1897,457,1930,1852,356,655,2143,709,2047,837,559,1819,
click #3: 217,2696,989,277,1014,2821,1285,2492,2141,1917,917,152,1783,1436,2338,1769,2828,888,1811,2143,2465,1675,2106,1705,2303,480,318,349,737,775,2535,2490,982,52,1809,2658,1680,2086,2137,1681,1841,1231,2608,2266,1276,539,2468,2043,270,2068,222,996,620,1973,2926,1541,589,2283,974,534,2502,936,1755,1366,2381,1426,2169,1731,2626,2680,88,741,1750,129,302,1233,2067,2839,429,1763,1011,1275,79,1668,553,1538,1870,2009,126,267,2068,2396,1112,1603,2179,905,2475,1242,2884,2850,1370,736,2416,590,1979,2480,2246,491,1670,1692,2474,749,1853,2211,2522,1227,856,86,812,843,2556,159,1605,2359,611,692,2629,147,950,2396,1757,824,2955,579,393,274,1858,2488,2689,1073,2292,1378,338,1433,792,1318,543,1901,2080,951,
click #1: 1525,533,1865,1208,2750,2058,240,2715,1669,2186,1889,2985,2859,1417,2743,1650,2667,286,1958,2343,2455,2254,2685,1633,1167,2212,1974,2491,339,420,2219,2736,2921,1994,888,412,1620,457,398,2475,2624,1842,1107,1302,2239,1769,2497,1595,2383,1132,2894,1769,1446,2633,1365,2270,847,231,41,537,83,748,2375,1248,2967,2152,63,865,529,2331,29,2210,2888,2482,2719,613,1147,1383,2394,2398,2715,378,107,2207,526,449,1290,288,629,1618,2564,1388,416,2861,2391,1875,2466,2859,1335,87,1706,1631,1066,52,1102,684,1881,1964,2913,752,1123,2463,837,643,822,2705,640,168,721,2517,862,2775,235,1911,2767,1640,1793,26,90,844,1146,1287,48,2307,692,1083,2312,55,1104,2842,1567,2326,374,2877,495,100,551,2773,2038,2686,