在androidstudio中组织Unity的页面

        在前面已经简单的将unity生成的unitylibrary集成到android项目中【http://t.csdnimg.cn/5ndr3】,接下来记录如何简单组织unity的页面。

        1 引用UnityPlayer

        在android中,控制unity页面主要是由UnityPlayer来完成,因此主要是对UnityPlayer的管理

        1.1 将此处的implementation修改为api

        否则在app模块中无法使用unity中libs里面的包,是使用unitypalyer的时候会爆红。

        

        1.2 如果你的unity中还使用了其他包。

        如果有其他包,例如ARcore等等,运行的时候会报找不到文件的问题。
        

        修改方法:

        在settings.gradle里加入如下代码        

        

 flatDir {
            dirs 'libs'  // 主项目的本地 `.aar` 文件目录
            dirs 'unityLibrary/libs'  // Unity 项目的本地 `.aar` 文件目录
        }


                 

        2 新建一个工具类来管理UnityPlayer

        

public class UnityPlayerManager {

    private static UnityPlayer mUnityPlayer;

    // 初始化 UnityPlayer
    public static void initialize(Context context) {
        if (mUnityPlayer == null) {
            if (context!=null)
            {
                mUnityPlayer = new UnityPlayer(context);
            }

        }
    }

    // 获取 UnityPlayer 实例
    public static UnityPlayer getInstance() {
        if (mUnityPlayer == null) {
            throw new IllegalStateException("UnityPlayer 没有被初始化");
        }
        return mUnityPlayer;
    }

    // 销毁 UnityPlayer 实例
    public static void destroyInstance() {
        if (mUnityPlayer != null) {
            mUnityPlayer.quit();
            mUnityPlayer = null;
        }
    }
}

        3 新建一个activity准备使用UnityPlayer
 

public class UnityPage1 extends AppCompatActivity {

    UnityPlayer mUnityPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unity_page1);
        Bridge.setAndroid_corrent_activity(this);
        // 获取 UnityPlayer 的视图
        View unityPlayerView = UnityPlayerManager.getInstance().getView();
        mUnityPlayer=UnityPlayerManager.getInstance();
       /* unityPlayerView.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
            @Override
            public void onWindowFocusChanged(boolean hasFocus) {
                UnityPlayerManager.getInstance().windowFocusChanged(hasFocus);
            }
        });*/
        if(unityPlayerView.getParent() != null){
            ((ViewGroup)unityPlayerView.getParent()).removeAllViews();
        }
        // 设置 UnityPlayer 的视图
        setContentView(unityPlayerView);
        mUnityPlayer.requestFocus();



    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 处理返回键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @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.
        super.onNewIntent(intent);
        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();
    }

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


}

        4 准备跳转到该Activity

        

        跳转前在Mainactivity里面通过UnityPlayer初始化一下UnityPlayer
        

        至此就可以跳转到unity页面中了,并且全局只使用了一个UnityPlayer实例。

        5 android与unity的通信

        在android里面调用unity的函数:

        

 UnityPlayer.UnitySendMessage("gameObject","method","canshu");

        第一个参数是物体,第二个方法是物体上的脚本里的方法,第三个参数是方法的参数

        unity调用android:

        

AndroidJavaObject javaobject = new AndroidJavaObject("com.excample.xxxx.xxxx");
JavaObject().Call(methedName, args);

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值