关于Unity启动时间过长(启动⿊屏时间长)的问题
好吧,Unity启动确实⽐其他引擎⽣成的游戏包慢些。关键是你启动的时候还要等上⼀段时间才显⽰Splash那个logo图。
最近项⽬有个蛋疼得需求,需要在启动界⾯加进度帧动画。。我也是醉了。
刚开始的思路:⽤Unity单独做个启动场景,让Splash那张图⽤成纯⿊。那么问题来了,除了Unity刚启动的⿊屏+显⽰Splash的⿊图,再到显⽰loading动画界⾯⾄游戏场景加载出来,这时间都在⼗多秒以上了,。项⽬要求游戏从启动到显⽰游戏场景不能超过5秒。。哎。。太难为我了。好长⼀段时间都没能解决这个问题。
中间还想到⼀个⽅案就是⽤Android做⼀个插件的启动Activity场景,然后把这个场景设置成启动场景,之后再到Unity的场景。但是这个问题是⽆法绕过Unity的启动Splash。。⽆奈放弃。
因为不熟悉Android开发,所以。。。
我分析了Unity⽣成Android,对就是从Unity的UnityPlayerActivity⼊⼿。既然绕不过,那就不绕过,思路:
1、Unity⽣成的Android应⽤必然要从这个UnityPlayerActivity启动,那么他的启动Splash view必然显⽰在这个activity⾥⾯;
2、那么我可以继承UnityPlayerActivity,然后在onCreate⽅法⾥⾯在创建⼀个view来覆盖Splash,是的。这是可⾏的。激动。。。。。。
3、那这个启动界⾯上我们也同样可以做动画咯,O(∩_∩)O哈哈~现在的问题是当Unity Splash显⽰完毕之后,或者初始化完成之后怎么来隐藏我们所创建的View和动画。
4、现在我们要在Unity建⼀个空场景⽤来加载我们的第⼀个游戏场景,当加载完成之后通知我们的⾃定义Activity移除我们创建的界⾯和动画,是不是很完美呢?
现在不⽤看⿊屏了,也绕过了Unity的Splash,只是加载的时间还是⽐其他引擎慢了,不过也能接受,因为很快的 就开到了我们的启动动画界⾯,等待加载到游戏场景。
最后贴点代码上来。。。。
package com.u3d.plugins;
import java.util.Locale;
import android.annotation.SuppressLint;
t.Context;
aphics.drawable.AnimationDrawable;
dia.AudioManager;
dia.AudioManager.OnAudioFocusChangeListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity
{
static final String TAG = "com.u3d.plugins";
private ImageView bgView = null;
private View view = null;
AnimationDrawable animationDrawable = null;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle arg0)
{
bgView=new ImageView(this);
String Default().getLanguage();
String bgName="splash_bg_en";
if(lanStr.equals("zh"))
{
bgName="splash_bg_zh";
}
Log.d(TAG, "System Lan:"+bgName);
int splash_bg=getResources().getIdentifier(bgName, "drawable", getPackageName());
bgView.setBackgroundResource(splash_bg);
//  this.addContentView(bgView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mUnityPlayer.addView(bgView);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
float scaleX=dm.widthPixels/1024f;
float scaleY=dm.heightPixels/600f;
Log.d(TAG, "Screen Width:"+dm.widthPixels+";Screen Height:"+dm.heightPixels);
LayoutInflater flater = LayoutInflater.from(this);
int layoutID=getResources().getIdentifier("activity_splash", "layout", getPackageName());
view = flater.inflate(layoutID, null);
int frame_Resources().getIdentifier("splash_frame", "id", getPackageName());
ImageView frameView=(ImageView)view.findViewById(frame_id);//new ImageView(this);
frameView.setBackgroundResource(R.anim.splash_gif);
//  this.addContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mUnityPlayer.addView(view);
frameView.setScaleX(scaleX);
frameView.setScaleY(scaleY);
//  frameView=new ImageView(this);
//  frameView.setBackgroundResource(R.anim.splash_gif);
//
//  LinearLayout ll=new LinearLayout(this);
//  LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //  params.leftMargin=scaleX*620;
//  ll.setLayoutParams(params);
//  ll.addView(frameView);
//  mUnityPlayer.addView(ll);
animationDrawable = (AnimationDrawable) Background();
animationDrawable.start();
}
public void HideSplash()
{
Log.d(TAG, "HideSplash");
new MainLooper()).post(new Runnable()
{
@Override
public void run()
{
Log.d(TAG, "HideSplash run");
animationDrawable.stop();
//    ((Parent()).removeView(bgView);
/
unity3d animation/    ((Parent()).removeView(view);
bgView=null;
view=null;
animationDrawable=null;
}
});
}
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
Log.d(TAG, "onRestart");
new MainLooper()).post(new Runnable()
{
@Override
public void run()
{
muteAudioFocus(MainActivity.this,true);
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
Log.d(TAG, "onStop");
new MainLooper()).post(new Runnable()
{
@Override
public void run()
{
muteAudioFocus(MainActivity.this,false);
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.d(TAG, "onStart");
new MainLooper()).post(new Runnable()
{
@Override
public void run()
{
muteAudioFocus(MainActivity.this,true);
}
});
}
/
**@param bMute 值为true时为关闭背景⾳乐。*/
public boolean muteAudioFocus(Context context, boolean bMute)  {
if(context == null)
{
Log.d(TAG, "context is null.");
return false;
return false;
}
boolean bool = false;
AudioManager am = (SystemService(Context.AUDIO_SERVICE);
if(bMute)
{
int result = am.requestAudioFocus(afChangeListener,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);  bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
else
{
int result = am.abandonAudioFocus(afChangeListener);
bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
Log.d(TAG, "pauseMusic bMute="+bMute +" result="+bool);
return bool;
}
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener()
{
public void onAudioFocusChange(int focusChange)
{
Log.d(TAG, "focusChange:"+focusChange);
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// Pause playback
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
muteAudioFocus(MainActivity.this,false);
// Stop playback
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
{
// Lower the volume
}
else if (focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
// Resume playback or Raise it back to normal
}
}
};
}
Unity的启动场景代码:
using UnityEngine;
using System.Collections;
public class Launcher : MonoBehaviour
{
public string loadScene;
void Awake()
{
Debug.Log("Launcher Awake");
DontDestroyOnLoad(gameObject);
}
void Start()
{
Debug.Log("Launcher Start");
StartCoroutine(LoadSence());
}
IEnumerator LoadSence()
{
if (!string.IsNullOrEmpty(loadScene))
{
Application.LoadLevelAsync(loadScene);
}
else
{
int levelCount = Application.levelCount;
int curLevel = Application.loadedLevel;
if (curLevel + 1 < levelCount)
Application.LoadLevelAsync(curLevel + 1);
}
yield return 0;
Invoke("OnFinish", 0.5f);
//OnFinish();
}
void OnFinish()
{
if (Application.platform.Equals(RuntimePlatform.Android))
{
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))            {
using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
{
jo.Call("HideSplash");
}
}
}
Destroy(gameObject);
}
}
最后把我们的Android项⽬⽣成jar包供Unity使⽤

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。