解决!阅读我的答案。
我正在尝试在本机React应用程序中显示Unity View 。此UnityPlayer显示了使用Vuforia的AR的相机供稿。我们需要将此 View 包含在iOS和Android的React-native应用程序中。
我遵循了许多指南,但在Android版本中却出现了黑屏。
如果我在由react-native生成的Android项目(而不是ReactActivity)中运行UnityPlayerActivity(来自Unity导出的Android项目),则可以正确看到UnityPlayer。所以我想我的项目包含所有必要的依赖项。
我的js代码的代码段:
const UnityIosContainerView = requireNativeComponent("UnityContainerView",
null);
const UnityAndroidContainerView = requireNativeComponent("RCT3DView", null);
class ArPage extends React.Component {
render() {
return (
<View style={{ flex: 1, backgroundColor: "red" }}>
{Platform.OS === "ios" ? (
<UnityIosContainerView style={{ flex: 1 }} />
) : (
<UnityAndroidContainerView style={{ flex: 1 }} />
)}
</View>
);
}
}
遵循本指南https://facebook.github.io/react-native/docs/native-components-android.html
我有一个自定义包
public class CustomReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(
new React3DViewManager()
);
}
}
这是我的ViewManager连接到MainReactActivity的生命周期,以正确管理UnityPlayer
public class React3DViewManager extends SimpleViewManager<View> implements LifecycleEventListener {
public static final String REACT_CLASS = "RCT3DView";
private UnityPlayer mUnityPlayer;
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected View createViewInstance(ThemedReactContext reactContext) {
reactContext.addLifecycleEventListener(this);
mUnityPlayer = new UnityPlayer(reactContext);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
mUnityPlayer.start();
mUnityPlayer.requestFocus();
return mUnityPlayer.getView();
}
@Override
public void onHostResume() {
mUnityPlayer.resume();
}
@Override
public void onHostPause() {
mUnityPlayer.pause();
}
@Override
public void onHostDestroy() {
mUnityPlayer.quit();
}
}
生成的屏幕截图:
验证点:
Unity 2017.2.0f3
反应16.0.0.0-alpha12
反应本色0.48.4
有没有人遇到过这个问题?
应用日志:
01-05 13:39:27.142 7233-7240/? I/art: Starting a blocking GC Instrumentation
01-05 13:39:27.156 7233-7233/? W/ActivityThread: Application com.protoreactnative is waiting for the debugger on port 8100...
01-05 13:39:27.156 7233-7233/? I/System.out: Sending WAIT chunk
01-05 13:39:29.228 7233-7240/com.protoreactnative I/art: Debugger is active
01-05 13:39:29.365 7233-7233/com.protoreactnative I/System.out: Debugger has connected
01-05 13:39:29.365 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:29.567 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:29.768 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:29.970 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.171 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.372 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.573 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.775 7233-7233/com.protoreactnative I/System.out: debugger has settled (1451)
01-05 13:39:31.131 7233-7233/com.protoreactnative I/InstantRun: starting instant run server: is main process
01-05 13:39:31.142 7233-7233/com.protoreactnative V/fb-UnpackingSoSource: locked dso store /data/user/0/com.protoreactnative/lib-main
01-05 13:39:31.143 7233-7233/com.protoreactnative I/fb-UnpackingSoSource: dso store is up-to-date: /data/user/0/com.protoreactnative/lib-main
01-05 13:39:31.143 7233-7233/com.protoreactnative V/fb-UnpackingSoSource: releasing dso store lock for /data/user/0/com.protoreactnative/lib-main
01-05 13:39:31.217 7233-7240/com.protoreactnative I/art: Starting a blocking GC Instrumentation
01-05 13:39:31.222 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.ctor()
01-05 13:39:31.272 7233-7233/com.protoreactnative D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-05 13:39:31.292 7233-7233/com.protoreactnative I/SensorManager: registerListenerImpl: listener = com.facebook.react.common.ShakeDetector@f6f1ff2, sensor = {Sensor name="Accelerometer Sensor", vendor="hTC Corp.", version=1, type=1, maxRange=39.2266, resolution=0.01, power=0.17, minDelay=10000}, delay = 66667, handler = null
01-05 13:39:31.315 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.createReactContextInBackground()
01-05 13:39:31.316 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
01-05 13:39:31.318 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.onJSBundleLoadedFromServer()
01-05 13:39:31.321 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.recreateReactContextInBackground()
01-05 13:39:31.321 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread()
01-05 13:39:31.359 7233-7254/com.protoreactnative I/art: Thread[15,tid=7254,Native,Thread*=0xf2e86b00,peer=0x12dcf280,"Thread-4"] recursive attempt to load library "/data/app/com.protoreactnative-2/lib/arm/libfb.so"
01-05 13:39:31.364 7233-7254/com.protoreactnative D/ReactNative: ReactInstanceManager.createReactContext()
01-05 13:39:31.394 7233-7233/com.protoreactnative W/unknown:ReactNative: Packager connection already open, nooping.
01-05 13:39:31.396 7233-7258/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:39:31.396 7233-7255/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:39:31.396 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:39:31.397 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:39:31.397 7233-7255/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:39:31.399 7233-7255/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:39:31.401 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:39:31.402 7233-7258/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:39:31.403 7233-7258/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:39:31.411 7233-7257/com.protoreactnative W/unknown:InspectorPackagerConnection: Couldn't connect to packager, will silently retry
01-05 13:39:31.457 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTGroupViewManager
01-05 13:39:31.467 7233-7262/com.protoreactnative I/Adreno: QUALCOMM build : b756f35, I9267f384df
Build Date : 10/19/16
OpenGL ES Shader Compiler Version: XE031.09.00.03
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.BF64.1.2.3_RB1.07.00.00.258.005
Remote Branch : NONE
Reconstruct Branch : NOTHING
01-05 13:39:31.467 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTGroupShadowNode
01-05 13:39:31.489 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTShapeViewManager
01-05 13:39:31.490 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTShapeShadowNode
01-05 13:39:31.503 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTTextViewManager
01-05 13:39:31.504 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTTextShadowNode
01-05 13:39:31.509 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.checkbox.ReactCheckBoxManager
01-05 13:39:31.540 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.uimanager.LayoutShadowNode
01-05 13:39:31.589 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.picker.ReactDialogPickerManager
01-05 13:39:31.600 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.drawer.ReactDrawerLayoutManager
01-05 13:39:31.608 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.picker.ReactDropdownPickerManager
01-05 13:39:31.612 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.scroll.ReactHorizontalScrollViewManager
01-05 13:39:31.631 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.scroll.ReactHorizontalScrollContainerViewManager
01-05 13:39:31.635 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.progressbar.ReactProgressBarViewManager
01-05 13:39:31.641 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.progressbar.ProgressBarShadowNode
01-05 13:39:31.646 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.scroll.ReactScrollViewManager
01-05 13:39:31.659 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.slider.ReactSliderManager
01-05 13:39:31.665 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.slider.ReactSliderManager$ReactSliderShadowNode
01-05 13:39:31.669 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.switchview.ReactSwitchManager
01-05 13:39:31.672 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.switchview.ReactSwitchManager$ReactSwitchShadowNode
01-05 13:39:31.676 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.toolbar.ReactToolbarManager
01-05 13:39:31.683 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.webview.ReactWebViewManager
01-05 13:39:31.692 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.swiperefresh.SwipeRefreshLayoutManager
01-05 13:39:31.697 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTSurfaceViewManager
01-05 13:39:31.699 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTSurfaceViewShadowNode
01-05 13:39:31.704 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.frescosupport.FrescoBasedReactTextInlineImageViewManager
01-05 13:39:31.705 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.frescosupport.FrescoBasedReactTextInlineImageShadowNode
01-05 13:39:31.711 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.image.ReactImageManager
01-05 13:39:31.720 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.modal.ReactModalHostManager
01-05 13:39:31.723 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.modal.ModalHostShadowNode
01-05 13:39:31.725 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactRawTextManager
01-05 13:39:31.726 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactRawTextShadowNode
01-05 13:39:31.728 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.textinput.ReactTextInputManager
01-05 13:39:31.746 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.textinput.ReactTextInputShadowNode
01-05 13:39:31.757 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactTextViewManager
01-05 13:39:31.764 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactTextShadowNode
01-05 13:39:31.768 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.view.ReactViewManager
01-05 13:39:31.778 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.viewpager.ReactViewPagerManager
01-05 13:39:31.782 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactVirtualTextViewManager
01-05 13:39:31.783 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactVirtualTextShadowNode
01-05 13:39:31.785 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.protoreactnative.react.React3DViewManager
01-05 13:39:31.844 7233-7254/com.protoreactnative D/ReactNative: Initializing React Xplat Bridge.
01-05 13:39:31.849 7233-7254/com.protoreactnative D/ReactNative: Initializing React Xplat Bridge before initializeBridge
01-05 13:39:31.863 7233-7254/com.protoreactnative D/ReactNative: Initializing React Xplat Bridge after initializeBridge
01-05 13:39:31.863 7233-7254/com.protoreactnative D/ReactNative: CatalystInstanceImpl.runJSBundle()
01-05 13:39:31.864 7233-7270/com.protoreactnative D/ReactNative: ReactInstanceManager.setupReactContext()
01-05 13:39:31.864 7233-7270/com.protoreactnative D/ReactNative: CatalystInstanceImpl.initialize()
01-05 13:39:31.866 7233-7270/com.protoreactnative W/unknown:ReactNative: Packager connection already open, nooping.
01-05 13:39:31.871 7233-7270/com.protoreactnative D/ReactNative: ReactInstanceManager.attachRootViewToInstance()
01-05 13:39:32.249 7233-7270/com.protoreactnative I/WebViewFactory: Loading com.android.chrome version 62.0.3202.84 (code 320208452)
01-05 13:39:32.293 7233-7270/com.protoreactnative I/cr_LibraryLoader: Time to load native libraries: 6 ms (timestamps 2695-2701)
01-05 13:39:32.311 7233-7270/com.protoreactnative I/chromium: [INFO:library_loader_hooks.cc(46)] Chromium logging enabled: level = 0, default verbosity = 0
01-05 13:39:32.313 7233-7270/com.protoreactnative I/cr_LibraryLoader: Expected native library version number "62.0.3202.84", actual native library version number "62.0.3202.84"
01-05 13:39:32.467 7233-7237/com.protoreactnative I/art: Do partial code cache collection, code=31KB, data=30KB
01-05 13:39:32.467 7233-7237/com.protoreactnative I/art: After code cache collection, code=30KB, data=29KB
01-05 13:39:32.467 7233-7237/com.protoreactnative I/art: Increasing code cache capacity to 128KB
01-05 13:39:32.692 7233-7269/com.protoreactnative I/ReactNativeJS: Running application "protoreactnative" with appParams: {"rootTag":1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
01-05 13:40:29.776 7233-7233/com.protoreactnative I/Unity: onResume
01-05 13:40:29.797 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:40:29.797 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:40:29.799 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:40:29.827 7233-7233/com.protoreactnative D/Unity: SetWindow 0 0xe3b38e08
01-05 13:40:29.827 7233-7233/com.protoreactnative D/Unity: SetWindow 0 0xe3b38e08
01-05 13:40:31.871 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:40:31.872 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:40:31.881 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
最佳答案
有关更多信息,请在此处进行描述:
Part 1
Part 2
确定,经过多次尝试,我找到了解决方案。
将UnityPlayer连接到MainActivity的所有生命周期非常重要。 ViewManager类不支持所有 Activity 生存期。您只需将管理器连接到恢复,暂停和销毁即可。但是UnityPlayer需要onConfigurationChanged来确保布局正确。我直接从Unity生成的UnityPlayerActivity类中获取代码,并公开UnityPlayer以在ViewManager中使用。
这是我使用UnityPlayer初始化的MainActivity。
public class MainActivity extends ReactActivity {
private UnityPlayer mUnityPlayer;
public UnityPlayer getUnityPlayer() {
return mUnityPlayer;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
}
public void onDestroy() {
super.onDestroy();
mUnityPlayer.quit();
}
public void onResume() {
super.onResume();
mUnityPlayer.resume();
}
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "protoreactnative";
}
// 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); }
}
这是 View 管理器:
public class React3DViewManager extends SimpleViewManager<UnityPlayer> {
public static final String REACT_CLASS = "RCT3DView";
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected UnityPlayer createViewInstance(ThemedReactContext reactContext) {
MainActivity activity = (MainActivity)reactContext.getCurrentActivity();
return activity.getUnityPlayer();
}
}
我不知道此解决方案是否是最佳解决方案,但是该解决方案可以满足我的需求。
谢谢