Unity打包成Android后打开后会黑屏一段时间

关于Unity打包成Android后打开后会黑屏一段时间

产考连接:https://blog.csdn.net/qq_43278826/article/details/107557784

解决方案
1、首先,你在AndroidManifest.xml中的activity, 给activity设置android:theme, 例如这样:

 <!-- android:theme 定义一个透明的主题-->
    <activity android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:launchMode="singleTask"
        android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
        android:hardwareAccelerated="false"
       
        android:theme="@android:style/Theme.Translucent"
        android:name=".UnityPlayerActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
    </activity>

因为默认android:theme不设置的话或者不是透明的话,那么你后面再设置透明主题的话是不会有任何修改的。
*注意:
android:theme="@android:style/Theme.Translucent" 用默认的透明主题,竖屏切到横屏后,下拉通知栏 会显示应用切至后台的画面,因为默认的透明主题 有一个windowAnimationStyle属性

 <item name="windowAnimationStyle">@style/Animation</item>

我的修改方式是:

<style name="UnityThemeSelector" parent="BaseUnityTheme">
	<item name="android:windowBackground">@android:color/transparent</item>
</style>

用这个做默认主题
2.接着在UnityPlayerActivity的onCreate里面设置成你自定义的style。

    private  UnityPlayerActivity _Activity;
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //修改AndroidManifest.xml中android:theme属性
        //前提 默认必须是透明的
        setTheme( R.style.UnityThemeSelector_Logo);
        super.onCreate(savedInstanceState);
        mUnityPlayer = new UnityPlayer(this);
        setContentView(mUnityPlayer);
		_Activity=this;
    }
  1. 自定义的style

在res目录下的values创建一个styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--自定义的启动图片-->
<style name="UnityThemeSelector.Logo" >
    <item name="android:windowBackground">@drawable/xbg</item>
    <item name="android:background">@drawable/xbg</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>
<!-- 透明的-->
 <style name="UnityThemeSelector.Logo1">
     <item name="android:windowBackground">@color/tt_transparent</item>
     <item name="android:background">@color/tt_transparent</item>
     <item name="android:windowNoTitle">true</item>
     <item name="android:windowFullscreen">true</item>
 </style>
</resources>
  1. 当Unity 那边的代码启动完成后再 回调 Android 这边的修改setTheme
    public void CloseSplash()
    {
        new Handler(Looper.getMainLooper()).post(new Runnable()
    	{
	        @Override
	        public void run()
	        {
	        	
	            _Activity.setTheme(R.style.UnityThemeSelector_Logo1);
	            
	           // _Activity.getTheme().applyStyle(R.style.UnityThemeSelector_Logo1,true);
	        }
	    });
    }

到这基本就完了
最后
在华为某个手机型号下,会打开前白一下屏,不知道其他手机会不会!
补充
在有悬浮窗的情况下会出问题!这是修改版本!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值