asmadsen / react-native-unity-view

MIT License
203 stars 129 forks source link

Unity scene not visible inside React Native #84

Closed appjitsu closed 2 years ago

appjitsu commented 2 years ago

I'm using Unity v 2020.3.29f1. The project builds fine and is running on an iphone 8. There are no build or runtime errors, but the scene is not being rendered inside react-native. I put a red background on the UnityView and I can see that it is full screen. I can see the messages coming back and forth from react-native and unity when I click the "toggle rotation" button.

I noticed that this version of Unity does not support OpenGL for iOS. Should I be using Unity v2019?

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  Button,
  Alert,
} from "react-native";
import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from "react-native/Libraries/NewAppScreen";
import UnityView, { UnityModule } from "@asmadsen/react-native-unity-view";

export default function App() {
  const [count, setClickCount] = React.useState(0);
  console.log(count);
  const onUnityMessage = (handler) => {
    console.log({ handler });
  };

  const onClick = () => {
    UnityModule.postMessageToUnityManager({
      name: "ToggleRotate",
      data: "",
      callBack: (data) => {
        Alert.alert("Tip", JSON.stringify(data));
      },
    });
  };

  return (
    <View style={{ flex: 1 }}>
      <View style={{ flex: 1 }}>
        <UnityView
          style={{ flex: 1, backgroundColor: "red", borderWidth: 1 }}
          onMessage={onUnityMessage}
          onUnityMessage={onUnityMessage}
        />
      </View>
      <Button
        style={{ width: "100%" }}
        title="Toggle rotation"
        onPress={onClick}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: "absolute",
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: "600",
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: "400",
    color: Colors.dark,
  },
  highlight: {
    fontWeight: "700",
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: "600",
    padding: 4,
    paddingRight: 12,
    textAlign: "right",
  },
});
appjitsu commented 2 years ago

here is the XCode log:

i noticed the following:

2022-03-08 10:54:46.891305-0600 unitypoc[4416:855298] Unbalanced calls to begin/end appearance transitions for <UnityDefaultViewController: 0x104c7d790>.
2022-03-08 10:54:46.891736-0600 unitypoc[4416:855298] Unbalanced calls to begin/end appearance transitions for <UnityDefaultViewController: 0x104c7d310>.
**UnloadTime: 4.775292 ms**

does that mean that ios is unloading Unity because there was a problem?

2022-03-08 10:54:44.712446-0600 unitypoc[4416:855298] [native] Running application main ({
    initialProps =     {
    };
    rootTag = 1;
})
2022-03-08 10:54:45.614893-0600 unitypoc[4416:855478] [javascript] Running "main" with {"rootTag":1,"initialProps":{}}
2022-03-08 10:54:45.628700-0600 unitypoc[4416:855478] [javascript] 0
2022-03-08 10:54:45.636966-0600 unitypoc[4416:855298] Built from '2020.3/staging' branch, Version '2020.3.29f1 (2ff179115da0)', Build type 'Release', Scripting Backend 'il2cpp'
2022-03-08 10:54:45.637759-0600 unitypoc[4416:855298] MemoryManager: Using 'Default' Allocator.
-> applicationDidFinishLaunching()
2022-03-08 10:54:45.699784-0600 unitypoc[4416:855298] Metal API Validation Enabled

Setting UIViewControllerBasedStatusBarAppearance to NO is no longer supported.
Apple actively discourages that, and all application-wide methods of changing status bar appearance are deprecated

-> applicationDidBecomeActive()
GfxDevice: creating device client; threaded=1
Initializing Metal device caps: Apple A11 GPU
Initialize engine version: 2020.3.29f1 (2ff179115da0)
2022-03-08 10:54:46.891305-0600 unitypoc[4416:855298] Unbalanced calls to begin/end appearance transitions for <UnityDefaultViewController: 0x104c7d790>.
2022-03-08 10:54:46.891736-0600 unitypoc[4416:855298] Unbalanced calls to begin/end appearance transitions for <UnityDefaultViewController: 0x104c7d310>.
UnloadTime: 4.775292 ms
onMessage:
UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
Rotate:onMessage(MessageHandler)
MessageHandlerDelegate:Invoke(MessageHandler)
UnityMessageManager:onRNMessage(String)

2022-03-08 10:55:25.759052-0600 unitypoc[4416:855478] [javascript] { handler: 'Now it is not rotating' }
appjitsu commented 2 years ago

I added the following code to App.js. There are no errors.

console.log("Unity created") is never called isReady is false UnityModule.sendMessage is never called

UnityModule.createUnity().then(() => {
    console.log("Unity created");
  }).catch((err) => {
    console.log("Unity create error", err);
  });

  UnityModule.isReady()
    .then((isReady) => {
      console.log({ isReady });
      if (isReady) {
        UnityModule.sendMessage("HelloWorld", "Hello from React Native!");
      }
    })
    .catch((err) => {
      console.log("Unity isReady error", err);
    });
appjitsu commented 2 years ago

i just tried Unity 2019 and it does the same thing: the Unity scene does not show, but I can send messages to Unity and vice versa.

michaelrausch commented 2 years ago

I was having the same issue, downgrading to Unity version 2019.4.6f1 worked for me

appjitsu commented 2 years ago

OK i'll give that a shot. Thanks.

appjitsu commented 2 years ago

I was having the same issue, downgrading to Unity version 2019.4.6f1 worked for me

That was exactly it. Works perfectly now. Much appreciated!