Unity-Technologies / uaal-example

Other
733 stars 233 forks source link

Xamarin support for Android and iOS #21

Open Grzesik opened 4 years ago

Grzesik commented 4 years ago

Hi, I tried the example and it works very well with Android Studio and XCode. I've tried to create an Xamarin.Android project to use Unity in Xamarin. I created the unityLibrary-release.aar from the example and used it in Xamarin Bindings. It seems to work. Unfortunatelly if I try to start the MainUnityActivity it doesn't start Unity, it crahes. I think it would be a great think for the Xamarin community, if we could, like in the example, to start and manipulate a Unity Game from Xamarin.

UnityAndroidApp2.zip

Hesketh commented 4 years ago

Hi, I took a look at your solution as I'm working on also integrating Unity as a library into Xamarin.

The crash you were having was because of a missing android resource string that the library wanted. By providing the missing keys and values in a string.xml within the unityLibrary module or adding it in your xamarin forms app project (Resources/values/strings.xml) makes Unity work as expected.

The keys that need to be defined for Unity are "app_name" and "game_view_content_description".(You already had "app_name" defined within the Xamarin Android project).

To summarise, I changed your "string.xml" file in the Xamarin Android Project to the following

<resources>
  <string name="app_name">Unity Engine Project</string>
  <string name="game_view_content_description">Game</string>
  <string name="action_settings">Settings</string>
</resources>
Grzesik commented 4 years ago

Hi, thanks a lot. It really works as expected!

I'll try to create the same project for iOS. Do you have any experience with the integration of Unity in Xamarin.iOS ? I've found only this https://github.com/andressbarajas/UnityBinding It compiles by me, but it doesn't implement the interface from the unity example. I'll try to extend it and than use in an Xamarin.iOS app.

Hesketh commented 4 years ago

Sadly I haven't had chance to look into iOS yet but it is on my list in the next few weeks. I'll keep you updated if I manage to find any other resources

Grzesik commented 4 years ago

Hi UnityiOSApp.zip UnityiOSApp.zip

, I’ve tried to implement the iOS sample with Xamarin.iOS. I’ve got the Binding file from the original XCode project. This is a debug version of the UnityFramework Library for iOS. I created the binding library in Xamarin and added the definitions like described here: https://github.com/andressbarajas/UnityBinding The iOS Project is actually nearly the same like the ObjectiveC one. The only thing I couldn’t find is , how to implement the NativeCallsProtocol. This Protocol is very important for the communication from Unity to the iOS App. The main problem in the Xamarin project is, that I can’t initialize the UnityFramework. In the function UnityFrameworkLoad the app crahses, because something is here wrong:

MachHeader header = new MachHeader(); ufw.SetExecuteHeader(ref header);

If I omit the code, the app crashes later. If you could see the project it would be great. The ObjectiveC project is not so easy to substitute with C#. The Android project works fine with Xamarin, thanks once again for help! I have only to find, how to support the 64 Bit version, because the project can only be exported in 32 Bit version.