UnityPlayerActivity详解

本文详细介绍了UnityPlayerActivity的使用方法,包括如何查找该组件、其路径及class.jar文件位置,并提供了源码解析。同时,文章还关注了Unity升级时需要注意的内容,以及重写UnityPlayerActivity时常见的黑屏问题解决方案。

如何查找UnityPlayerActivity

UnityPlayerActivity路径

C:\Program Files\Unity\Hub\Editor\2020.3.32f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player

class.jar路径(UnityPlayerActivity代码引用与calss.jar)

注意:什么时候选择il2pp。主要看unity中的设置

C:\Program Files\Unity\Hub\Editor\2020.3.32f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes

源码解析

`// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
package com.unity3d.player;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.os.Process;

public class UnityPlayerActivity extends Activity implements IUnityPlayerLifecycleEvents
{
protected UnityPlayer mUnityPlayer; // don’t change the name of this variable; referenced from native code

// Override this in your custom UnityPlayerActivity to tweak the command line arguments passed to the Unity Android Player
// The command line arguments are passed as a string, separated by spaces
// UnityPlayerActivity calls this from 'onCreate'
// Supported: -force-gles20, -force-gles30, -force-gles31, -force-gles31aep, -force-gles32, -force-gles, -force-vulkan
// See https://docs.unity3d.com/Manual/CommandLineArguments.html
// @param cmdLine the current command line arguments, may be null
// @return the modified command line string or null
protected String updateUnityCommandLineArguments(String cmdLine)
{
    return cmdLine;
}

// Setup activity layout
@Override protected void onCreate(Bundle savedInstanceState)
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    String cmdLine = updateUnityCommandLineArguments(getIntent().getStringExtra("unity"));
    getIntent().putExtra("unity", cmdLine);

    mUnityPlayer = new UnityPlayer(this, this);
    setContentView(mUnityPlayer);
    mUnityPlayer.requestFocus();
}

// When Unity player unloaded move task to background
@Override public void onUnityPlayerUnloaded() {
    moveTaskToBack(true);
}

// Callback before Unity player process is killed
@Override public void onUnityPlayerQuitted() {
}

@Override protected void onNewIntent(Intent intent)
{
    // To support deep linking, we need to make sure that the client can get access to
    // the last sent intent. The clients access this through a JNI api that allows them
    // to get the intent set on launch. To update that after launch we have to manually
    // replace the intent with the one caught here.
    setIntent(intent);
    mUnityPlayer.newIntent(intent);
}

// Quit Unity
@Override protected void onDestroy ()
{
    mUnityPlayer.destroy();
    super.onDestroy();
}

// If the activity is in multi window mode or resizing the activity is allowed we will use
// onStart/onStop (the visibility callbacks) to determine when to pause/resume.
// Otherwise it will be done in onPause/onResume as Unity has done historically to preserve
// existing behavior.
@Override protected void onStop()
{
    super.onStop();

    if (!MultiWindowSupport.getAllowResizableWindow(this))
        return;

    mUnityPlayer.pause();
}

@Override protected void onStart()
{
    super.onStart();

    if (!MultiWindowSupport.getAllowResizableWindow(this))
        return;

    mUnityPlayer.resume();
}

// Pause Unity
@Override protected void onPause()
{
    super.onPause();

    if (MultiWindowSupport.getAllowResizableWindow(this))
        return;

    mUnityPlayer.pause();
}

// Resume Unity
@Override protected void onResume()
{
    super.onResume();

    if (MultiWindowSupport.getAllowResizableWindow(this))
        return;

    mUnityPlayer.resume();
}

// Low Memory Unity
@Override public void onLowMemory()
{
    super.onLowMemory();
    mUnityPlayer.lowMemory();
}

// Trim Memory Unity
@Override public void onTrimMemory(int level)
{
    super.onTrimMemory(level);
    if (level == TRIM_MEMORY_RUNNING_CRITICAL)
    {
        mUnityPlayer.lowMemory();
    }
}

// This ensures the layout will be correct.
@Override public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    mUnityPlayer.configurationChanged(newConfig);
}

// Notify Unity of the focus change.
@Override public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
    mUnityPlayer.windowFocusChanged(hasFocus);
}

// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
    if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
        return mUnityPlayer.injectEvent(event);
    return super.dispatchKeyEvent(event);
}

// Pass any events not handled by (unfocused) views straight to UnityPlayer
@Override public boolean onKeyUp(int keyCode, KeyEvent event)     { return mUnityPlayer.injectEvent(event); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event)   { return mUnityPlayer.injectEvent(event); }
@Override public boolean onTouchEvent(MotionEvent event)          { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event)  { return mUnityPlayer.injectEvent(event); }

}
`

Unity升级需要注意及时更新和替换

Unity升级之后需要修改,看版本比对内容
也可以直接拷贝,然后activity 继承
在这里插入图片描述

重写UnityPlayerActivity需要注意的问题(不正确使用导致的黑屏)

直接从引擎内拷贝,尽量少修改

启动黑屏和切后台黑屏


    // Resume Unity
    @Override
    protected void onResume() {
   
   
        super.onResume();

//        if (MultiWindowSupport.getAllowResizableWindow(this))
//            return;
        LTBaseSDK.getInstance(this).onResume();
        mUnityPlayer.resume();

		//修复 sdk 切后台回来黑屏的问题,
        mUnityPlayer.postInvalidate();
        //下面的写法会引起启动黑屏,不建议使用
        //mUnityPlayer.windowFocusChanged(true);
        //mUnityPlayer.requestFocus();
       //onWindowFocusChanged(true);
        //Log.d(LOG_TAG, "onResume:" );
    }

来回切后台导致的unity焦点丢失黑屏


    // Resume Unity
    @Override
    protected void onResume() {
   
   
        super.onResume();

//        if (MultiWindowSupport.getAllowResizableWindow(this))
//            return;
        LTBaseSDK.getInstance(this).onResume();
        mUnityPlayer.resume();

		//修复 sdk 切后台回来黑屏的问题,
        mUnityPlayer.postInvalidate();
        //下面的写法会引起启动黑屏,不建议使用
        //mUnityPlayer.windowFocusChanged(true);
        //mUnityPlayer.requestFocus();
       //onWindowFocusChanged(true);
        //Log.d(LOG_TAG, "onResume:" );
    }

最终修改版本

package com.longtugame.rjsdk;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值