Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to control unity's camra in react-native? #74

Open
yanlei1234 opened this issue Dec 5, 2018 · 1 comment
Open

How to control unity's camra in react-native? #74

yanlei1234 opened this issue Dec 5, 2018 · 1 comment

Comments

@yanlei1234
Copy link

It can be used on the native side, but it is not integrated into the react-native.
Can you help me to solve this problem, thanks.

@JanOwiesniak
Copy link

JanOwiesniak commented Dec 5, 2018

React Native

Bind onUnityMessage in render

<UnityView
  ref={(ref) => this.unity = ref as any}
  style={{ position: 'absolute', left: 0, right: 0, top: 0, bottom: 0 }}
  onUnityMessage={this.onUnityMessage.bind(this)}>
</UnityView>

Send messages from React Native to Unity via postMessageToUnityManager

private unity: UnityView;

this.unity.postMessageToUnityManager({
  name: 'toUnity',
  data: JSON.stringify({foo: "bar"})
});

Listen to Unity messages in React Native via onUnityMessage

private onUnityMessage(hander: MessageHandler) {
  console.log(hander);
}

Unity

Call onMessage every time a new React Native message comes in

protected virtual void Awake()
{
  UnityMessageManager.Instance.OnRNMessage += onMessage;
}

protected virtual void OnDestroy()
{
  UnityMessageManager.Instance.OnRNMessage -= onMessage;
}

Listen to messages from React Native in Unity via onMessage

protected virtual void onMessage(MessageHandler message)
{
  var dataString = message.getData<string>();

  if (message.name == "toUnity")
  {
    var data = JsonConvert.DeserializeAnonymousType(dataString, new { foo = "" });

    // Do whatever you want e.g. grab camera and do things related to your use case
    // You can use additional data from react e.g. data.foo => "bar"
    GameObject.Find("Camera");

    // Send message back to React Native (optional)
    JObject dataToRN = JObject.FromObject(new
    {
      om: "nom"
    });
      
    UnityMessageManager.Instance.SendMessageToRN(new UnityMessage()
    {
      name = "toReactNative",
      data = dataToRN,
      callBack = (dataFromRN) =>
      {
        Debug.Log("React Native: " + dataFromRN);
      }
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants