Unity和Android通信系列文章2——扩展UnityPlayerActivity

1.Unity这边开发完成后,导出Android工程。

将unity切换到android环境,勾选Export Project,点击Export按钮,指定路径导出android工程。

image.png

2.Android Studio中打开Unity导出的工程

打开Unity导出的android工程,看一下UnityPlayerActivity.class这个类。

说明:Unity导出的android工程,只有一个Acitivity,就是UnityPlayerActivity;当需要修改某些工程时,需要扩展这个类。

2.1 扩展UnityPlayerActivity.class类

需求:给unity开发的游戏添加一个默认的启动背景。

原UnityPlayerActivity.class类代码:

package com.fable.test;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;

import com.unity3d.player.UnityPlayer;

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

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

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

@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);
}

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

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

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

@Override protected void onStart()
{
    super.onStart();
    mUnityPlayer.start();
}

@Override protected void onStop()
{
    super.onStop();
    mUnityPlayer.stop();
}

// 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 与 Android的布局管理 1. 简述 在上一章我们讨论了最基础的Unity与Android数...
    伯夷考阅读 4,231评论 1 6
  • 引言 最近为了实现Unity与Android之间的通信,在网络上发现了很多种实现方案。有打包Jar的,有打包aar...
    Pieces_Jing阅读 30,557评论 44 76
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,784评论 0 17
  • 1979年,亦舒小说《喜宝》由香港明窗出版社出版,为文学评论公认的亦舒代表作。1988年,该部小说由香港公司注资改...
    析子阅读 528评论 12 15
  • 无论战术多么勤奋 都掩盖不了战略的愚蠢 不要高估自己的判断 不要纵容自己的善变 不要低估他人的能力 珍视别人的付出...
    漫珠阅读 324评论 0 0