• 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); }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143

    }
    `

    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:" );
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    来回切后台导致的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:" );
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    最终修改版本

    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.os.Build;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.Window;
    import android.database.Cursor;
    import android.net.Uri;
    import com.bh.sdk.Interface.LTBaseSDK;
    import com.unity3d.player.IUnityPlayerLifecycleEvents;
    //import com.unity3d.player.MultiWindowSupport;
    import com.unity3d.player.UnityPlayer;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.os.Process;
    
    public class MainActivity extends Activity implements IUnityPlayerLifecycleEvents {
        private final static String LOG_TAG = "MainActivity";
        private int TAKE_PHOTO = 3;
        private int TAKE_VIDEO = 2;
        private int SELECT_PHOTO = 4;
        private int SELECT_VIDEO = 5;
        private static String m_listenerName = "";
        public static Context instance;
        public static String m_Spath = "";
        public Uri originalUri = null;
        public Bitmap m_Bitmap = null;
        public String m_Extension = "";
        public String m_tbPath = "";
        public Cursor cursor = null;
        public int column_index = -1;
        public String _path = "";
    
    
        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) {
            instance = getApplicationContext();
            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();
            //Log.d(LOG_TAG, "onCreate:" );
        }
    
        // 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);
            LTBaseSDK.getInstance(this).onNewIntent(intent);
            //Log.d(LOG_TAG, "onNewIntent:" );
        }
    
        // Quit Unity
        @Override
        protected void onDestroy() {
            mUnityPlayer.destroy();
            super.onDestroy();
            LTBaseSDK.getInstance(this).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();
            LTBaseSDK.getInstance(this).onStop();
    //        if (!MultiWindowSupport.getAllowResizableWindow(this))
    //            return;
    
           // mUnityPlayer.pause();
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            LTBaseSDK.getInstance(this).onStart();
    //        if (!MultiWindowSupport.getAllowResizableWindow(this))
    //            return;
    
           // mUnityPlayer.resume();
    
        }
    
        // Pause Unity
        @Override
        protected void onPause() {
            super.onPause();
            LTBaseSDK.getInstance(this).onPause();
    //        if (MultiWindowSupport.getAllowResizableWindow(this))
    //            return;
    
            mUnityPlayer.pause();
    
        }
    
        // 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:" );
        }
    
        // 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);
            LTBaseSDK.getInstance(this).onConfigurationChanged(newConfig);
        }
    
        // Notify Unity of the focus change.
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            mUnityPlayer.windowFocusChanged(hasFocus);
            //Log.d(LOG_TAG, "onWindowFocusChanged:"+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) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                LTBaseSDK.getInstance(this).LTBaseSDKExitGame();
                return mUnityPlayer.injectEvent(event);
            }
            return super.onKeyDown(keyCode, event);
    
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return mUnityPlayer.injectEvent(event);
        }
    
        /*API12*/
        public boolean onGenericMotionEvent(MotionEvent event) {
            return mUnityPlayer.injectEvent(event);
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            LTBaseSDK.getInstance(this).onRestart();
        }
    
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            LTBaseSDK.getInstance(this).onBackPressed();
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            LTBaseSDK.getInstance(this).onActivityResult(requestCode, resultCode, data);
    
        }
    
        @Override
        public void attachBaseContext(Context newBase) {
            super.attachBaseContext(newBase);
            LTBaseSDK.getInstance(this).attachBaseContext(newBase);
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            LTBaseSDK.getInstance(this).onRequestPermissionsResult(requestCode, permissions, grantResults);
    
            if (BaseSDK.mListenerName != "") {
                m_listenerName = BaseSDK.mListenerName;
            }
    
            JSONObject jo = new JSONObject();
            if (requestCode == 1003 || requestCode == 1004 || requestCode == 1002) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
                    try {
    
                        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
    
                            jo.putOpt(SDKJsonDefine.RESULT, false);
                            jo.putOpt(SDKJsonDefine.PERMISSION_RESULTCODE, requestCode);
                            jo.putOpt(SDKJsonDefine.MSG, "权限获取失败");
                        } else {
    
                            jo.putOpt(SDKJsonDefine.RESULT, true);
                            jo.putOpt(SDKJsonDefine.PERMISSION_RESULTCODE, requestCode);
                            jo.putOpt(SDKJsonDefine.MSG, "权限获取成功");
                        }
    
                        if (m_listenerName != "") {
    
                            UnityPlayer.UnitySendMessage(m_listenerName, SDKCallbackDefine.ONPERMISSION, jo.toString());
                        } else {
                            Log.i("OnGetPermissions", "Unity接收回调对象不存在!!!");
                        }
                    } catch (JSONException e) {
    
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
  • 相关阅读:
    从业务场景分析腾讯云服务器选型最佳实践!
    Linux压缩与解压缩
    Java String.substring()方法具有什么功能呢?
    Web APIs:事件高级--键盘事件,京东按键输入及模拟京东快递单查询案例
    JavaScript 讲述数据结构 - 栈结构
    2.5 计算机网络
    Linux被控主机反弹shell给Cobalt_Strike(CS)-详细实战笔记
    Linux常用命令分类整理
    掌握优先级队列:提升效率的关键技巧
    滑动窗口与前缀和算法总结和例题
  • 原文地址:https://blog.csdn.net/osuckseed/article/details/125564032