unity启动黑屏问题
19 Jan 2017随着游戏开发的内容越来越多,启动的时间越来越长,所以启动的时候,需要等待很长的黑屏
时间(7s左右)。为了优化这部分体验,我们做了调研,发现一个有效的方法可以缓解这个问题。
第一步
在AndroidManifest.xml
中配置启动activity
的theme
。这个在游戏启动后有效果。
<style name="my_theme" parent="android:Theme.Black.NoTitleBar.Fullscreen">
<item name="android:windowBackground">@drawable/start_page</item>
</style>
第二步
在主Activity中onCreate
加载一个ImageView到unityPlayer
中。
在unity层加载好了,可以调用HideSplash
。
//显示启动图
void ShowSplash() {
try {
InputStream is = getAssets().open("bin/Data/splash.png");
Bitmap splashBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
splashBitmap = BitmapFactory.decodeStream(is, null, options);
is.close();
bgView = new ImageView(this);
bgView.setImageBitmap(splashBitmap);
bgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
unityPlayer.addView(bgView);
} catch (Exception e) {
NLog.e(TAG, "Exception while load splash:" + e.toString());
}
}
//关闭启动图
void HideSplash() {
this.runOnUiThread(new Runnable() {
@Overide
public void run() {
unityPlayer.removeView(bgView);
bgView = null;
}
})
}
这样,游戏启动之后,就一直是Splash图
了,不会有黑屏问题,但是加载时间没有减少,所以只能说缓解了这个问题。