如何从Android工作室的正常活动中启动统一活动?

时间:2017-02-01 21:22:12

标签: android android-activity unity3d

我已将Unity项目导出为android工作室项目。我在android studio中导入了该项目。我创建了一个活动,我想从该活动中启动UnityplayerActivity。但它给了我一个例外。

Main2Activity.java

package com.ultimate;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

import com.unity3d.player.UnityPlayer;

public class Main2Activity extends AppCompatActivity {
Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){


            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),com.ultimate.UnityPlayerActivity.class);
               startActivity(intent);


            }
        });


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

content_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ultimate.Main2Activity"
    tools:showIn="@layout/activity_main2">


    <Button
        android:text="Primary Device"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="135dp"
        android:id="@+id/button" />
</RelativeLayout>

UnityPlayerActivity.java

package com.ultimate;

import com.unity3d.player.*;
import android.app.Activity;
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;

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

        getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

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

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

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

错误: -

02-02 02:22:36.522 17389-17422/com.ultimate E/HAL: hw_get_module_by_class: module name gralloc
02-02 02:22:36.522 17389-17422/com.ultimate E/HAL: hw_get_module_by_class: module name gralloc
02-02 02:22:38.362 17389-17389/com.ultimate E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.ultimate, PID: 17389
                                                              android.content.ActivityNotFoundException: Unable to find explicit activity class {com.ultimate/com.ultimate.UnityPlayerActivity}; have you declared this activity in your AndroidManifest.xml?
                                                                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1801)
                                                                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
                                                                  at android.app.Activity.startActivityForResult(Activity.java:3978)
                                                                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                  at android.app.Activity.startActivityForResult(Activity.java:3939)
                                                                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                  at android.app.Activity.startActivity(Activity.java:4262)
                                                                  at android.app.Activity.startActivity(Activity.java:4230)
                                                                  at com.ultimate.Main2Activity$1.onClick(Main2Activity.java:30)
                                                                  at android.view.View.performClick(View.java:5207)
                                                                  at android.view.View$PerformClick.run(View.java:21177)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5441)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
谢谢。 。

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题。

您必须在UnityPlayerActivity文件中添加AndroidManifest.xml