Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question SwiftUI and Unity as Library

Discussion in 'iOS and tvOS' started by Artpen, Dec 16, 2020.

  1. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    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.

    Does anyone has some up to date information how to integrate Swift UI and Unity 2019/2020?
    If someone can give me an example how to integrate a simple spinning cube it will be more than enough to get me started.

    Thank you for help.
     
  2. x2stone

    x2stone

    Joined:
    Dec 17, 2014
    Posts:
    22
    Hi, I'm looking for the same thing, did you find a way to do it ?
    Please let me know if you succeed, and I will do the same if I find something working.
     
  3. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    No unfortunately no. There are lots of information, but it is outdated or not working.
     
  4. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    512
    I have not tried doing this myself, all the following information are deducted through a typical iOS framework integration workflow, but I hope the information could give you some lead.

    Since UaaL is a new thing since 2019.3 (I assume you know this), it has to be one of the versions newer in order to first build a unity framework for embedding into your main app.

    Once you exported the Xcode project from Unity, build the framework by selecting the UnityFramework scheme in Xcode (top left corner) and select Any iOS Device to make sure building all the supported archs.

    upload_2021-1-8_13-46-36.png

    Once you have the thing built, find Products > UnityFramework.framework in project navigator, right click and Show in Finder. It will open Finder on that build folder and you can copy the UnityFramework.framework out to somewhere else. This is the executable for your Unity project.

    upload_2021-1-8_13-45-55.png

    As for the assets, copy the entire Data folder in the root of the exported Unity project.

    upload_2021-1-8_13-51-43.png

    Be sure to build on release mode after you are done with testing. See the following screenshots on how to change the build type.

    upload_2021-1-8_13-43-38.png

    upload_2021-1-8_13-43-1.png

    Then, include and embed the framework into your main app as you normally would with any other iOS frameworks. Include the copied Data folder in the Copy Bundle Resources build phase of your app.

    Take a look at function UnityFrameworkLoad() in MainApp/main.mm of the Unity Xcode project to see a reference on how to load the Unity library. After that, since I have not done this myself, you probably need to call method -runEmbeddedWithArgc:argv:appLaunchOpts: and then -showUnityWindow. The header file <UnityFramework/UnityFramework.h> should give you an idea what the interface looks like.
     
  5. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    512
    The forum only allows 5 attachments per post. Here the image of what the build folder looks like.

    upload_2021-1-8_13-58-49.png

    Here is another tip. Before you integrate the framework into your main app or update it, you should test your Unity project standalone (with the app target that the Unity Xcode project comes with) to help isolating issues.
     
    Artpen likes this.
  6. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Thank you @Neonlyte. It is very helpful. I will definitely try it...
     
  7. x2stone

    x2stone

    Joined:
    Dec 17, 2014
    Posts:
    22
    Hi Neonlyte, thanks you so much for helping us !
    @Artpen : Did you tried Neonlyte's method ? Did it work ?
     
  8. Chadobado

    Chadobado

    Joined:
    Oct 19, 2016
    Posts:
    26
    Hey @Neonlyte thanks for this. One question, any reason I can’t build/use multiple UnityFramework’s in a single XCode project? I need to compile for each SDK (arm64 for iOS and x86 for simulator) as SwiftUI’s previews use the iOS simulator. I thought I could associate each UnityFramework with each build target in XCode but haven’t been able to get it working yet. TIA!
     
  9. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    512
    Sorry, I have no further knowledge in this use case. I tried to integrate frameworks to work with SwiftUI in a non-Unity project, and it was pain.

    Edit: See if this matches what you were trying to do:
    https://developer.apple.com/forums/thread/86759
     
    Last edited: Jan 22, 2021
  10. Chadobado

    Chadobado

    Joined:
    Oct 19, 2016
    Posts:
    26
    Thanks, much appreciated
     
  11. Chadobado

    Chadobado

    Joined:
    Oct 19, 2016
    Posts:
    26
    Hey @Neonlyte sorry to ping you again, but still trying to get this working.

    Any idea if Uaal/UnitFramework is possibly stripping compiled architectures from custom plugins included in Unity? For the life of me I can't get this running in the simulator w/ a simple bridge plugin, lipo shows UnityFramework w/ x86_64, but compiling the Swift project using Uaal fails w/ an x86_64 linker error for the plugin...

    More details --> here.

    Thanks in advance, really appreciate it.
     
  12. higel

    higel

    Joined:
    May 7, 2021
    Posts:
    1
    Yeah, it is pretty hard to integrate SwiftUI and Unity but if you ask me it works especially good when combining SwiftUI with UIKit. Though some may think it may kill the last one :)