Unity打包成Android后打开后会黑屏一段时间这个版本在有悬浮窗会出问题! 解决方案 思路就是在android 启动时,先加载一个ImageView 盖在最上层遮挡住启动动画, 后面等Unity 启动好了后再关闭道具这个遮挡的ImageView。 1.创建一个ImageView ,在android 启动时调用
@Override protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mUnityPlayer = new UnityPlayer(this);
UnitySplashSDK.getInstance().SetmUnityPlayer(mUnityPlayer);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
UnitySplashSDK.getInstance().onShowSplash();
}
public void onShowSplash() {
if (bgView != null)
return;
try {
Log.e("==========", "onShowSplash: 1 " );
bgView = new ImageView(UnityPlayer.currentActivity);
bgView.setBackgroundColor(Color.parseColor("#ffffffff"));
bgView.setImageResource(R.drawable.logo1);
bgView.setAdjustViewBounds(false);
bgView.setScaleType( ImageView.ScaleType.CENTER_CROP);
mUnityPlayer.addView(bgView);
} catch (Exception e) {
e.printStackTrace();
}
}
2.等待Unity 启动完成
public void onHideSplash() {
try {
if (bgView == null)
return;
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
mUnityPlayer.removeView(bgView);
bgView = null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
3.完整代码
package com.plugin.sdk;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import com.unity3d.player.UnityPlayer;
import co.niuyou.kaile.huawei.MainApplication;
import co.niuyou.kaile.huawei.R;
public class UnitySplashSDK {
private ImageView bgView = null;
private UnityPlayer mUnityPlayer = null;
private static UnitySplashSDK mInstance;
public static UnitySplashSDK getInstance() {
if (null == mInstance) {
synchronized (UnitySplashSDK.class) {
if (null == mInstance) {
mInstance = new UnitySplashSDK();
}
}
}
return mInstance;
}
public void SetmUnityPlayer( UnityPlayer pl){
mUnityPlayer=pl;
}
public void onShowSplash() {
if (bgView != null)
return;
try {
Log.e("==========", "onShowSplash: 1 " );
bgView = new ImageView(UnityPlayer.currentActivity);
bgView.setBackgroundColor(Color.parseColor("#ffffffff"));
bgView.setImageResource(R.drawable.logo1);
bgView.setAdjustViewBounds(false);
bgView.setScaleType( ImageView.ScaleType.CENTER_CROP);
mUnityPlayer.addView(bgView);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onHideSplash() {
try {
if (bgView == null)
return;
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
mUnityPlayer.removeView(bgView);
bgView = null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
|