Hi everyone,
Many of our users are complaining about battery life when using our game.
So I’m here trying to figure out, how to optimize the battery consumption.
First, you need to know that the game is a 2D game using NGUI.
This is an airline tycoon game.
For my example, I will profile the game using the main screen with my account, that’s a really advanced account but this way you will see figures for a real case.
I’m running the game with xCode attached.
Here is a screenshot of the main screen:
For the record, the lines on the map are made with vectrosity, so they are basically meshes.(1 mesh)
The aircrafts are made with a particle system, so each aircraft is a particle updated every seconds.
Now, here is a screenshot of the energy impact in xCode:
As you can see the impact is really high, doing nothing heavy.
CPU usage is not really high as you can see.
Memory usage is OK too.
I was not able to profile GPU with my iPad Air, so I used an old iPhone 5 (perfs impact should be higher on this phone)
Still not really high.
We did a stat analysis in Unity Editor and the game does not have a really high amount of draw calls:
The thing that is using the most the CPU is the flight calculations on the map:
But remember that my account is an advanced account and that I have 625 flights to process.
Here is the algorithm processing flight position during the time:
public static WorldCoordinate Interpolate(WorldCoordinate from, WorldCoordinate to, float percent){
radFromLat = Mathf.Deg2Rad * from.north;
radFromLong = Mathf.Deg2Rad * from.east;
radToLat = Mathf.Deg2Rad * to.north;
radToLong = Mathf.Deg2Rad * to.east;
cosFromLat = Mathf.Cos(radFromLat);
cosToLat = Mathf.Cos(radToLat);
radDist = _computeDistanceInRadiansBetween(from,to);
sinRadDist = Mathf.Sin(radDist);
if(sinRadDist < 1.0E-6){
return new WorldCoordinate(from.north,from.east);
}
a = Mathf.Sin((1 - percent) * radDist) / sinRadDist;
b = Mathf.Sin(percent * radDist) / sinRadDist;
c = a * cosFromLat * Mathf.Cos(radFromLong) + b * cosToLat * Mathf.Cos(radToLong);
d = a * cosFromLat * Mathf.Sin(radFromLong) + b * cosToLat * Mathf.Sin(radToLong);
lat = Mathf.Rad2Deg * (Mathf.Atan2(a * Mathf.Sin(radFromLat) + b * Mathf.Sin(radToLat), Mathf.Sqrt(Mathf.Pow(c,2) + Mathf.Pow(d,2))));
lng = Mathf.Rad2Deg * (Mathf.Atan2(d,c));
return new WorldCoordinate(lat,lng);
}
static float p1RadLat;
static float p1RadLong;
static float p2RadLat;
static float p2RadLong;
protected static float _computeDistanceInRadiansBetween(WorldCoordinate coord1, WorldCoordinate coord2){
p1RadLat = Mathf.Deg2Rad * (coord1.north);
p1RadLong = Mathf.Deg2Rad * (coord1.east);
p2RadLat = Mathf.Deg2Rad * (coord2.north);
p2RadLong = Mathf.Deg2Rad * (coord2.east);
return (2 * Mathf.Asin(Mathf.Sqrt(Mathf.Pow(Mathf.Sin((p1RadLat - p2RadLat) / 2), 2) + Mathf.Cos(p1RadLat) * Mathf.Cos(p2RadLat) * Mathf.Pow(Mathf.Sin((p1RadLong - p2RadLong) / 2), 2))));
}
From what I see the problem is not from the CPU usage but is from the network usage.
The problem is that for sync concerns the game must talk to the server very often, at each action. But we don’t send large data, we just send forms. Does the network use the same amount of battery when you send a large amount of data and when you send a small amount?
If anyone have any clue on how to decrease battery usage, or any advice, please answer.