Return to site

Build A Bridge Swift Library To Unity

broken image


  • Task Library works cross-platform and is supported on Java, C, and Swift (coming soon). To use the Support Library in your Android app, we recommend using the AAR hosted at MavenCentral for Task Vision library and Task Text library, respectively. You can specify this in your build.gradle dependencies as follows.
  • Hi Guys, I am new to native iOS development. Just starting with Swift UI. After lots of failed attempts to create beautiful UI in Unity I stumbled upon Swift UI and Unity as a library. From Unity talk and some online resources it looks straight forward. Unfortunately all example are very old from Unity 5 times.
  • SwiftUI helps you build great-looking apps across all Apple platforms with the power of Swift — and as little code as possible. With SwiftUI, you can bring even better experiences to all users, on any Apple device, using just one set of tools and APIs.
  • Now, back in the Unity Editor, click the 'Build' button. When prompted for an output directory name, call it unityios. Once Unity finishes building, open the file Unity-iPhone.xcodeproj in the directory that was created with Xcode.

SwiftUI helps you build great-looking apps across all Apple platforms with the power of Swift — and as little code as possible. With SwiftUI, you can bring even better experiences to all users, on any Apple device, using just one set of tools and APIs.

Advanced app experiences and tools

Enhance your apps with new features, such as improved list views, better search experiences, and support for control focus areas. And gain more control over lower-level drawing primitives with the new Canvas API, a modern, GPU-accelerated equivalent of drawRect.

Accessibility improvements

Speed up interactions by exposing the most relevant items on a screen in a simple list using the new Rotor API. The current accessibility focus state, such as the VoiceOver cursor, can now be read and even changed programmatically. And with the new Accessibility Representation API, your custom controls easily inherit full accessibility support from existing standard SwiftUI controls.

SwiftUI improvements on macOS

New performance and API availability improvements, including support for multicolumn tables, make your macOS apps even better.

The service provides a factory creational design pattern to create and return a single Cat object (complete with a cute photo), or group of five cats in a CatList object (with multiple cats). It is hosted using IIS Express to quickly demo and test the service with a client. A client “tester” windows form application tests the service.

Always-On Retina Display support

On Apple Watch Series 5 and later, the Always-On Retina Display allows watchOS apps to stay visible, even when the watch face is dimmed, making key information available at a glance.

Widgets for iPadOS

Now widgets can be placed anywhere on the Home screen and increased to a new, extra-large widget size.

Declarative syntax

SwiftUI uses a declarative syntax, so you can simply state what your user interface should do. For example, you can write that you want a list of items consisting of text fields, then describe alignment, font, and color for each field. Your code is simpler and easier to read than ever before, saving you time and maintenance.

This declarative style even applies to complex concepts like animation. Easily add animation to almost any control and choose a collection of ready-to-use effects with only a few lines of code. At runtime, the system handles all of the steps needed to create a smooth movement, and even deals with interruption to keep your app stable. With animation this easy, you’ll be looking for new ways to make your app come alive.

Design tools

Xcode includes intuitive design tools that make building interfaces with SwiftUI as easy as dragging and dropping. As you work in the design canvas, everything you edit is completely in sync with the code in the adjoining editor. Code is instantly visible as a preview as you type, and any change you make to that preview immediately appears in your code. Xcode recompiles your changes instantly and inserts them into a running version of your app — visible, and editable at all times.

Drag and drop. Arrange components within your user interface by simply dragging controls on the canvas. Click to open an inspector to select font, color, alignment, and other design options, and easily rearrange controls with your cursor. Many of these visual editors are also available within the code editor, so you can use inspectors to discover new modifiers for each control, even if you prefer hand-coding parts of your interface. You can also drag controls from your library and drop them on the design canvas or directly on the code. Aperture 3 dmg download.

Dynamic replacement. The Swift compiler and runtime are fully embedded throughout Xcode, so your app is constantly being built and run. The design canvas you see isn’t just an approximation of your user interface — it’s your live app. And Xcode can swap edited code directly in your live app with “dynamic replacement,” a new feature in Swift.

Previews. You can now create one or many previews of any SwiftUI views to get sample data, and configure almost anything your users might see, such as large fonts, localizations, or Dark Mode. Previews can also display your UI in any device and any orientation.

Get started.

Download Xcode 13 and use these resources to build apps with SwiftUI for all Apple platforms.

So you want to trigger functionality in your Unity3D scene straight from your native Objective-C code? For example you have different scenes, and you want a regular iOS component (e.g. UIButton) to trigger a new scene. It takes some work, but it is doable.

Build A Bridge Swift Library To Unity

Read it … bitch 🙂

Unity exposes a way to call code in your Unity3D project (normally this will be javascript or C#). There are 3 components you need to get in place.

  1. Have a Unity3D scene
  2. Have a Unity3D script
  3. Have an Objective-C class

The way it works is that you can bridge your code from Objective-C to C# or Javascript in Unity3D with the ‘UnitySendMessage’ method.

First you’ll need to create a Unity3D project. Open up Unity3D and go to File > New project. Give it a name and place it somewhere on your hard drive. I’ll call my project ‘UnityMethodCalling’.

Unity3D will automatically create a scene for you, go nuts (or just put a box in it) and make sure the camera can see it.

Now create a folder in your ‘Assets’ folder and call it ‘scenes’. Save your scene to this folder and call it ‘scene1’.

Now create another scene via File > New scene. Go nuts again (or put a sphere in it). Just make sure it’s something else visually so you will be able to see the difference when we switch scenes by code. When you’re done, save the scene again in the same folder, but call it ‘scene2’.

Alright, so now we have 2 scenes and the goal of this tutorial is to switch between these 2 scenes at runtime when you press a UIButton.

First we’ll need to create a C# file in which we will declare the API methods we want to expose to our iOS project.

Create a new C# file in the ‘Assets’ folder and call it SceneChanger. Open the file by double-clicking on it. It should open in MonoDevelop.

You’ll see 2 methods here:

  1. Start: this method gets called first (duh…) and in this one you can initialise some properties
  2. Update: this methods get called every frame

We will write our custom method ‘ChangeScene’. This method will accept a level argument of type string. The string to use to load a new scene is the name of the scene. So in our case it will be ‘scene1’ or ‘scene2’.

Now we need to do one more thing and that is import the interop services package. Just add the following at the top of the file.

OK, that’s it for the Unity scripting part.

Now we need to move over to our Xcode project. Go to File > Build Settings and add the two scenes to the build. You can do this by dragging them from the Assets library.

Next choose iOS as your platform and click on ‘Player Settings’ at the bottom of the dialog. This will open the ‘Inspector panel’ and let you set all the settings for the Xcode project. In my case I will change the following settings (but they can -and probably will- be different of yours).

Build A Bridge Swift Library To Unity

  • Default orientation : Landscape right
  • Bundle identifier: be.thenerd.unityMessaging
  • Target device: iPad
  • Target iOS version: 7.0

Now click on ‘Build & Run’ in the Build Settings dialog. Unity will ask you where to put the Xcode project. I will put it in a folder called ‘ios-builds’ outside of my Unity project and call it ‘unity-native-messaging’.

Click ‘Save’ and let Unity sprinkle its magic and create the Xcode project (this can take some time). If all goes well, your app will boot on your device and you should see the scene you’ve made.

Build A Bridge Swift Library To Unity

Now we’ll have to add a native UI layer above the scene. I’ve written a blogpost how you can do this. I won’t repeat how you can do this right here, so if you want to know … read up on the other blog post and come back when you have done the necessary steps.

The code to put in the TNAppController (or how you called it) is the following:

If all went well and you run the app. You should see the following.

Alright almost there, now we just have to call the method in our C# file. We can do this by using the UnitySendMessage method. This method expects 3 arguments:

  1. the name of the game object in Unity3D to which you have attached the C# script
  2. the name of the method
  3. parameters you would like to send to this method

This is a C method which allows us to bridge the gap between Objective-C++ and C#.

Create the following method.

We can now call this method from our ‘switchScenesTapped’ method.

You will maybe ask yourself … where is that ‘dummy’ string coming from. Well go back to Unity3D and open Scene1. To make this work, your C# script has to be attached to a game object. This is easy to do … just drag and drop the script on for example the cube. You will see in the inspector that a ‘Script component’ has been added to the cube. Also pay attention that we aren’t sending NSStrings, but C strings! (so no @ in front of the quotes)

Ok … but still, where is the dummy string coming from. Well that is actually just the name of the game object on which you have attached the script. You can change the name at the top of the Inspector panel.

Build A Bridge Swift Library To Unity Church

In a real world application you would probably give it another name, and probably attach this script also to something else. But this is just for demonstration purposes.

Build A Bridge Swift Library To Unity Bridge

So if we now look back at our code.

Build

Read it … bitch 🙂

Unity exposes a way to call code in your Unity3D project (normally this will be javascript or C#). There are 3 components you need to get in place.

  1. Have a Unity3D scene
  2. Have a Unity3D script
  3. Have an Objective-C class

The way it works is that you can bridge your code from Objective-C to C# or Javascript in Unity3D with the ‘UnitySendMessage’ method.

First you’ll need to create a Unity3D project. Open up Unity3D and go to File > New project. Give it a name and place it somewhere on your hard drive. I’ll call my project ‘UnityMethodCalling’.

Unity3D will automatically create a scene for you, go nuts (or just put a box in it) and make sure the camera can see it.

Now create a folder in your ‘Assets’ folder and call it ‘scenes’. Save your scene to this folder and call it ‘scene1’.

Now create another scene via File > New scene. Go nuts again (or put a sphere in it). Just make sure it’s something else visually so you will be able to see the difference when we switch scenes by code. When you’re done, save the scene again in the same folder, but call it ‘scene2’.

Alright, so now we have 2 scenes and the goal of this tutorial is to switch between these 2 scenes at runtime when you press a UIButton.

First we’ll need to create a C# file in which we will declare the API methods we want to expose to our iOS project.

Create a new C# file in the ‘Assets’ folder and call it SceneChanger. Open the file by double-clicking on it. It should open in MonoDevelop.

You’ll see 2 methods here:

  1. Start: this method gets called first (duh…) and in this one you can initialise some properties
  2. Update: this methods get called every frame

We will write our custom method ‘ChangeScene’. This method will accept a level argument of type string. The string to use to load a new scene is the name of the scene. So in our case it will be ‘scene1’ or ‘scene2’.

Now we need to do one more thing and that is import the interop services package. Just add the following at the top of the file.

OK, that’s it for the Unity scripting part.

Now we need to move over to our Xcode project. Go to File > Build Settings and add the two scenes to the build. You can do this by dragging them from the Assets library.

Next choose iOS as your platform and click on ‘Player Settings’ at the bottom of the dialog. This will open the ‘Inspector panel’ and let you set all the settings for the Xcode project. In my case I will change the following settings (but they can -and probably will- be different of yours).

Build A Bridge Swift Library To Unity

  • Default orientation : Landscape right
  • Bundle identifier: be.thenerd.unityMessaging
  • Target device: iPad
  • Target iOS version: 7.0

Now click on ‘Build & Run’ in the Build Settings dialog. Unity will ask you where to put the Xcode project. I will put it in a folder called ‘ios-builds’ outside of my Unity project and call it ‘unity-native-messaging’.

Click ‘Save’ and let Unity sprinkle its magic and create the Xcode project (this can take some time). If all goes well, your app will boot on your device and you should see the scene you’ve made.

Now we’ll have to add a native UI layer above the scene. I’ve written a blogpost how you can do this. I won’t repeat how you can do this right here, so if you want to know … read up on the other blog post and come back when you have done the necessary steps.

The code to put in the TNAppController (or how you called it) is the following:

If all went well and you run the app. You should see the following.

Alright almost there, now we just have to call the method in our C# file. We can do this by using the UnitySendMessage method. This method expects 3 arguments:

  1. the name of the game object in Unity3D to which you have attached the C# script
  2. the name of the method
  3. parameters you would like to send to this method

This is a C method which allows us to bridge the gap between Objective-C++ and C#.

Create the following method.

We can now call this method from our ‘switchScenesTapped’ method.

You will maybe ask yourself … where is that ‘dummy’ string coming from. Well go back to Unity3D and open Scene1. To make this work, your C# script has to be attached to a game object. This is easy to do … just drag and drop the script on for example the cube. You will see in the inspector that a ‘Script component’ has been added to the cube. Also pay attention that we aren’t sending NSStrings, but C strings! (so no @ in front of the quotes)

Ok … but still, where is the dummy string coming from. Well that is actually just the name of the game object on which you have attached the script. You can change the name at the top of the Inspector panel.

Build A Bridge Swift Library To Unity Church

In a real world application you would probably give it another name, and probably attach this script also to something else. But this is just for demonstration purposes.

Build A Bridge Swift Library To Unity Bridge

So if we now look back at our code.

Argument 1: ‘dummy’ is the name (identifier) of our game object in Unity3D on which we have attached the C# script

Argument 2: ‘ChangeScene’ is the method name which we wrote in our C# script

Argument 3: ‘scene2’ is the name of the scene file we want to load

Build A Bridge Swift Library To Unity Maine Nh

So build and run and if you tap on the switch button, you should see the scenes switch.

If you want to return, you will need to add the script also to an object in scene2 and call it dummy and put some logic in the switchScenesTapped method to check which scene is active.

Build A Bridge Swift Library To Unity Main

I hope I could help some of you guys/girls out with messaging between Objective-C and Unity3D. I’ve uploaded my Unity3D project and Objective-C code to GitHub, so you can check it out yourself.

Build A Bridge Swift Library To Unity Maine N

If you have any questions or remarks, please put them in the comments!





broken image