Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Integration Unity as a library in native iOS app

Discussion in '2019.3 Beta' started by PavelLU, May 27, 2019.

  1. Bray22

    Bray22

    Joined:
    Nov 26, 2019
    Posts:
    4
    Thank you so, so much for doing all this work to integrate Unity with Swift. I am trying to integrate the unity view programmatically into a SwiftUI view, and I am having some trouble. In newer versions of xcode or unity, The initial swift integration method stopped working, but there was a workaround using the following code:

    Code (CSharp):
    1. self.navigationController?.pushViewController(UnityEmbeddedSwift.getUnityRootview(), animated: true)
    instead of just using UnityEmbeddedSwift.showUnity function.

    However, I have an issue where implemented programmatically, the unity window will initialize, but not show the unity widow. It will give the following output:


    [B]2020-01-12 12:40:02.843694-0600 NativeApp[13773:3589118] Metal API Validation Enabled
    -> applicationWillEnterForeground()
    -> applicationDidBecomeActive()
    GfxDevice: creating device client; threaded=1
    Initializing Metal device caps: Apple A9 GPU
    Initialize engine version: 2019.3.0f3 (6c9e2bfd6f81)
    UnloadTime: 2.072958 ms[/B]


    This is similar to the error people have gotten by just using UnityEmbeddedSwift.showUnity function, so I believe my implementation has broken the viewcontroller workaround.

    I've attached my sample project to this post. It has code for both a storyboard implementation and a programmatic implementation, and you will see that the storyboard implementation works (when downloaded to a device), while the programmatic implementation does not work. Does anyone have an idea why this issue is occurring? Any help would be greatly appreciated.

    Here is my project
     
    ROBYER1 likes this.
  2. Bray22

    Bray22

    Joined:
    Nov 26, 2019
    Posts:
    4
    For some reason, the forum is not letting me edit this post due to spam rules. I've figured out the problem, and it was a mistake on my end. Essentially, I needed to create an empty navigation controller within my UIKit ViewController wrapper protocol to handle opening a new unity window. Here is a link to the fixed code, I am new to coding so be warned, my solution is likely not the best solution out there, but it avoids storyboards entirely (which I do not understand well) and is completely programmatic in implementation. This is good for projects edited by multiple people under source control, where storyboards start to become more useless.
     
  3. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    Guys I have small doubt, Here iOS Plugin NativeCallProxy create in objective c
    can we create NativeCallProxy plugin in swift
     
  4. teamamplify

    teamamplify

    Joined:
    Nov 24, 2016
    Posts:
    12
    As Unity only supports Obj-C, i think there is some kind of bridge needed.

    I use notifications to forward the calls from unity:
    Code (CSharp):
    1.     extern "C" {
    2.     void UnitySomethingHappened(const char *key) {
    3.         NSDictionary* dict = @{ @"key": [NSString stringWithUTF8String:key] };
    4.         [[NSNotificationCenter defaultCenter] postNotificationName: @"UnitySomethingHappened" object:nil userInfo:dict];
    5.     }
    6. }
    7.  
    And handle them in a swift file:
    Code (CSharp):
    1.     init() {
    2.         NotificationCenter.default.addObserver(self, selector: #selector(somethingHappened(_:)), name:NSNotification.Name("UnitySomethingHappened"), object: nil)
    3.         // more notifications if needed
    4.     }
    5.  
    6.     @objc func somethingHappened(_ n: NSNotification) {
    7.         let key = n.userInfo?["key"] as! String
    8.         // notify other classes
    9.     }
     
    planetmatrix and kushwaha03 like this.
  5. Asseks

    Asseks

    Joined:
    Nov 22, 2019
    Posts:
    13
    Get this error
    runtime: UI API called from background thread: -[UIViewController init] must be used from main thread only
     
  6. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    Yaa I created bridge I will check it again .
    I just want to know only....
    Thnaks:)
     
  7. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    which unity version you are use
    use 2019.3 version bcz that is unity side bug. i trey to resolve my self but no luck after that i move 2019.3 version
    now its fixed out
     
  8. ExtremeKaos

    ExtremeKaos

    Joined:
    Dec 4, 2016
    Posts:
    7
    Hi, with Xcode 11 and unity 2020.* , i have add Unityframework.framework to the new group "Framworks,Libraries, and Embedded Content" with "Do not embed, embed without signing and embed & sign", and have a error like "Framework not found UnityFramework".

    Any clue/tip for this using objective-c ...
     
  9. bibloc

    bibloc

    Joined:
    Jan 31, 2018
    Posts:
    17
    Hello everyone,

    We are trying to automate step 5 (Expose NativeCallProxy.h) and step 6 (Make Data folder be part of the UnityFrameWork).

    We can't manage to expose NativeCallProxy.h with https://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.html.
    Look like we need to add NatviceCallProxy.h to PBXHeadersBuildPhase (name and type is null from PBXProject).

    Did anyone succeed ?

    Code (CSharp):
    1. [PostProcessBuild]
    2.     public static void OnPostBuild (BuildTarget target, string pathToBuiltProject) {
    3.         PBXProject pbxProject = new PBXProject ();
    4.         string projectPath = PBXProject.GetPBXProjectPath (pathToBuiltProject);
    5.         pbxProject.ReadFromFile (projectPath);
    6.  
    7.         // Set data folder target membership to UnityFramework
    8.         string dataGuid = pbxProject.FindFileGuidByProjectPath ("Data");
    9.         string unityFrameworkGuid = pbxProject.GetUnityFrameworkTargetGuid ();
    10.         pbxProject.RemoveFileFromBuild (pbxProject.GetUnityMainTargetGuid (), dataGuid);
    11.         pbxProject.AddFileToBuild (unityFrameworkGuid, dataGuid);
    12.  
    13.         // Set NativeCallProxy.h target membership to UnityFramework
    14.         string nativeCallGuid = pbxProject.FindFileGuidByProjectPath ("Libraries/Plugins/iOS/NativeCallProxy.h");
    15.         string sectionGuid = GetPBXHeadersBuildPhaseGuid (pbxProject, unityFrameworkGuid);
    16.         // pbxProject.AddFileToBuild (unityFrameworkGuid, nativeCallGuid);
    17.         pbxProject.AddFileToBuildSection (unityFrameworkGuid, sectionGuid, nativeCallGuid);
    18.  
    19.         File.WriteAllText (projectPath, pbxProject.WriteToString ());
    20.     }
    21.  
    22.     private static string GetPBXHeadersBuildPhaseGuid (PBXProject pbxProject, string targetGuid) {
    23.         string[] allBuildPhaseGuid = pbxProject.GetAllBuildPhasesForTarget (targetGuid);
    24.         foreach (string buildPhaseGuid in allBuildPhaseGuid) {
    25.             string tmpBuildPhaseName = null;
    26.             try {
    27.                 tmpBuildPhaseName = pbxProject.GetBuildPhaseName (buildPhaseGuid);
    28.             } catch (Exception e) { }
    29.             string type = null;
    30.             try {
    31.                 type = pbxProject.GetBuildPhaseType (buildPhaseGuid);
    32.             } catch (Exception e) { }
    33.             if (string.IsNullOrEmpty (tmpBuildPhaseName) && string.IsNullOrEmpty (type)) {
    34.                 return buildPhaseGuid;
    35.             }
    36.         }
    37.         return null;
    38.     }
     
  10. Lez_Andrew

    Lez_Andrew

    Joined:
    Jan 17, 2020
    Posts:
    1
    Hi everyone,

    Using Unity 2019.3.0b4 & working my way through the OP instructions. I too am having similar compile problems:

    'UnityFramework/UnityFramework.h' file not found".

    Ive built with a device attached but still get the error. Im pretty sure its a problem with adding the UnityFramework.framework (section 4 in the OP instructions). Im using Xcode Version 11.3 (11C29) and I can't see the Embedded Binaries section under the General settings (see screenshot). Im probably missing some setting/option somewhere but can't think what.
     

    Attached Files:

    Last edited: Jan 17, 2020
  11. ExtremeKaos

    ExtremeKaos

    Joined:
    Dec 4, 2016
    Posts:
    7
    UPDATED 20/01/2020
    - build first of all the unity project in Xcode (inside workspace) , then change to native project and build. No need to add routes to header or frameworks then.
     
  12. OldHaggis

    OldHaggis

    Joined:
    Oct 7, 2019
    Posts:
    2
    After trying several different things it all now seems to be working. I added the UnityFramework.framework to the "Frameworks, Libraries, and Embedded Content" section, cleaned the build folder and deleted derived data.
     
  13. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39

    just run you unity export app then directly reference data and native file with unityframework (in target)
    after that go to your project folder then open unityframework and copy and paste your native app
    then integrate whatever you ant
    or if you looking workspace project then use this link https://drive.google.com/open?id=1ElDzo1Uno14bM0cKNThMTC6KOUjDl8Hb
     
  14. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    guys let me know How can we send image or others data (not string) unity to Native iOS
    or if possible iOS to unity
    I want to send data unity to ios native main controller and reload view
    how is that possible.
    basically im using Navigation
     
    Last edited: Jan 22, 2020
  15. unity_gTYv0VO3muDQTQ

    unity_gTYv0VO3muDQTQ

    Joined:
    Jan 20, 2020
    Posts:
    3

    Facing same issue, Please update if you found answer.
    Xcode - 11
    Unity Version - 2019.3.0f5
     
    AnisimovArtur likes this.
  16. unity_gTYv0VO3muDQTQ

    unity_gTYv0VO3muDQTQ

    Joined:
    Jan 20, 2020
    Posts:
    3
    No such module 'UnityFramework' in UnityEmbeddedSwift.swift file
     
  17. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
  18. unity_gTYv0VO3muDQTQ

    unity_gTYv0VO3muDQTQ

    Joined:
    Jan 20, 2020
    Posts:
    3
  19. Bray22

    Bray22

    Joined:
    Nov 26, 2019
    Posts:
    4
    I go this error when trying to build to the iOS simulator -- but it did work when I built to my iPhone. Does anybody have a resource for creating custom simulator build settings that allows for rapid UI development in swift without loading unity at all?
     
  20. Bray22

    Bray22

    Joined:
    Nov 26, 2019
    Posts:
    4
    I embedded a UIKit view of the unity player in a zstack under swiftUI buttons, which seems to work, but not incredibly well. Did you end up figuring anything out on your end?
     
  21. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    Try to Run only Device because some portions are not supports in simulator. if you use nothing in unity then you can change player setting for simulators.
     
  22. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    Forgot about all. just try to run your Unity build (after export unity ) . if its successful then give your build unity data folder reference to unityframework target
    Go product folder and copy unityframework.framework and paste your native app
    then Run.

    or

    if you want to use workspace only then First Build your unity and run then give data folder reference to unityframework

    after that add your native app in workspace and import unityframework in your target framework and library
    still you get again same thing then just remove unityframework and again import
     
    Last edited: Jan 27, 2020
  23. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
  24. ExtremeKaos

    ExtremeKaos

    Joined:
    Dec 4, 2016
    Posts:
    7
    Same problem, when debug in device works ok (Unity 2020). When try to publish for AppStore the error hits and cancel archive of project. Anyone have published something with this problem solved ??
    Thanks

    UPDATE: Have detected that my project target support iOS 9.0 (in debug works), when i change to release (archive) don't. If i change minimun support to iOS 11.0 then it works. Any clue ??
     
    Last edited: Feb 5, 2020
  25. rwetzler779

    rwetzler779

    Joined:
    Mar 9, 2019
    Posts:
    4
    My app crashes when trying to reload unity after an unload. I know people were having this problem a few months ago. What was the consensus on this issue? Does anyone have a way to reload that works for them?
     
  26. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    Guys if i passed controller via navigation, it is taking time
    I mean controller passed slow at launch unity time. how can i fixed that.
    any other way to launch unity fast
     
  27. NSWell

    NSWell

    Joined:
    Jul 30, 2017
    Posts:
    87
    Native view freezes when transitioning to Unity view....
    How to fixed that.
    My Code:
    Code (CSharp):
    1. self.navigationController?.pushViewController(communication.getARLibraryAppController().rootViewController, animated: true)
    Like this video :
     
  28. Asseks

    Asseks

    Joined:
    Nov 22, 2019
    Posts:
    13
    How can I send message from Unity to native iOS app?
    I want to send message from Unity button "Exit" to the native iOS application and exit from Unity
     
  29. toeadd31

    toeadd31

    Joined:
    Jul 29, 2014
    Posts:
    4
    I was able to to this, but I had to download the xcodeapi repo from https://bitbucket.org/Unity-Technologies/xcodeapi/src/stable/ and modify it to support PBXHeadersBuildPhaseSection as well as Public membership on build file settings.

    After that I was able to just do:
    Code (CSharp):
    1.  
    2. // Project
    3. PBXProject pbxProject = new PBXProject();
    4. string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
    5. pbxProject.ReadFromFile(projectPath);
    6. string unityFrameworkGuid = pbxProject.TargetGuidByName("UnityFramework");
    7.  
    8. // Set data folder target membership to UnityFramework
    9. string dataGuid = pbxProject.FindFileGuidByProjectPath("Data");
    10. pbxProject.AddFileToBuild(unityFrameworkGuid, dataGuid);
    11.  
    12. // Set NativeCallProxy target membership to UnityFramework
    13. string nativeCallGuid = pbxProject.FindFileGuidByProjectPath("Libraries/Plugins/iOS/NativeCallProxy.h");
    14. pbxProject.AddFileToBuild(unityFrameworkGuid, nativeCallGuid);
    15.  
    16. File.WriteAllText(projectPath, pbxProject.WriteToString());
    17.  
     
    bibloc likes this.
  30. aguilar_unity

    aguilar_unity

    Joined:
    Oct 3, 2019
    Posts:
    1
    I am following the example with Unity 2019.3.0f6 and Xcode 11.3. The only difference is that my Unity project has third party dependencies FBSDK, FirebaseCore, etc..

    I am unable to build because the generated xcconfig "Pods-UnityFramework.debug.xcconfig" is using the following environment variables to locate libraries.

    PODS_BUILD_DIR = ${BUILD_DIR}
    PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)


    If I directly build Unity-iPhone.xcodeproj there is no problem. BUILD_DIR points to ~/Library/Developer/Xcode/DerivedData/Unity-iPhone-(HASH) and PODS_CONFIGURATION_BUILD_DIR is pointing to a place where the libraries exist.

    However if I follow the instructions and make a new workspace then BUILD_DIR points to the new build directory associated with the new workspace. There are no libraries there. So when I build my target I get a linking error about my libraries not being found.

    Am I missing a step or does UnityFramework not work well with 3rd party dependencies?
     
    Last edited: Mar 4, 2020
  31. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    can be use self.navigationController?.present(ufw.appController().rootViewController, animated: true)
     
    rkiOSDeveloper likes this.
  32. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    hi bro can have a sample code to unload unity
    Thanks .
     
  33. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    hello guys unity build integration is done with native ios but may i know how to unload unity from native.
    because of some issues are happening with unload unity from native in terms of memory and reopening unity multiple times please give me any suggession and solutions.
    Thanks in advance.
     
  34. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    Hi bro thanks for providing code ,
    self.navigationController?.pushViewController(UnityEmbeddedSwift.getUnityRootC(), animated: true) //this was not working for me
    self.navigationController?.present(UnityEmbeddedSwift.getUnityRootC(), animated: true)
    //this is working fine but no back button bro but i want quit unity to move native screen
    and unload unity giving me crash bro
    //this message i am getting in console
    SendMessage: object NativeCommunication not found!

    But touch events on buttons and back button on navigation bar not working can i have any solutions here..
    Thanks in advance.
     
    Last edited: Mar 12, 2020
  35. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    actually I was passing by self.navigationController?.pushViewController(UnityEmbeddedSwift.getUnityRootC(), animated: true) so that time when you ll need to back native button then you should have to make communication files like checks https://forum.unity.com/threads/integration-unity-as-a-library-in-native-ios-app.685219/ project there is NativeCallProxy so just rename with my own name
    and //this message i am getting in console
    SendMessage: object NativeCommunication not found!
    for this create new object and rename with NativeCommunication (remeber function name should be have in c# file)

    so just simple add button in unity side then make function in c# then call with button then create ios/plugin like NativeCallProxy use same function name both side

    if you used this then for back you need to use pocontroller and if u use present then you should use dismiss for back.

    Hope this is helping for you or let me know any issues


    check another references code https://drive.google.com/open?id=1ElDzo1Uno14bM0cKNThMTC6KOUjDl8Hb
     
    Last edited: Mar 12, 2020
    rkiOSDeveloper likes this.
  36. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17

    thanks for reply bro,
    can i have step by step procedure (with sample code) please because i am native ios developer so i can implement.
    Thanks.
     
    Last edited: Mar 12, 2020
  37. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    hi bro in this video unity camera is opening fine is back button is working fine in your code please let me know because in my case its not working bro,
    Thanks in advance.
     
  38. NSWell

    NSWell

    Joined:
    Jul 30, 2017
    Posts:
    87
    the back button event comes with the system, I only judged. Sorry bro, Im not a iOS developer~
    Do u know how to fix the freeze?

    Here is our code:
    Code (CSharp):
    1.     override func viewDidAppear(_ animated: Bool) {
    2.         if(ARLibraryEmbeddedSwift.shared != nil){
    3.             ARLibraryEmbeddedSwift.shared?.closeARLibrary()
    4.         }
    5.        
    6.     }
     
    rkiOSDeveloper likes this.
  39. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    hi ,
    There is no such method with name of "closeARLibrary()" in UnityFrameWork where did you find this ? let me know.
    Thanks for Reply.
     
  40. NSWell

    NSWell

    Joined:
    Jul 30, 2017
    Posts:
    87
    closeARLibrary() is my custom method.
    This is the detail:
    Code (CSharp):
    1.     ARLibraryEmbeddedSwift.ufw.unloadApplication()
    2.         ARLibraryEmbeddedSwift.ufw.unregisterFrameworkListener(self)
    3.         ARLibraryEmbeddedSwift.ufw = nil
    4.         ARLibraryEmbeddedSwift.shared = nil
     
    rkiOSDeveloper likes this.
  41. NSWell

    NSWell

    Joined:
    Jul 30, 2017
    Posts:
    87
    sorry bro im later! present is not work for my app. I dont know why
     
  42. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    its ok now i find batter solution, no need for push and present :);)
     
    rkiOSDeveloper likes this.
  43. NSWell

    NSWell

    Joined:
    Jul 30, 2017
    Posts:
    87
    ;)Can you tell me how you did it? I m facing the freeze.
     
  44. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    Thanks for reply ,
    actually i am native ios developre so you explain me how to close ar from native ios or can i have example project bro.
    Thanks in advance.
     
    Last edited: Mar 17, 2020
  45. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    may i know how did you done it ??
     
  46. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    may i know what you implemented in closeARLibrary method
    Thanks in advance.
     
  47. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    I justed added only native app with showUnity only.
    hope you can integare communicate part or let me know if you unable then i ll create demo unity project with some button

    project link https://github.com/kushwaha03/NativeUnityiOS
     
    rkiOSDeveloper likes this.
  48. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    Ok sure Thank you.
     
  49. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    i have seen this example in my device if i launch unity my mobile RAM Usage to 120 MB but if i unload unity its still 128 MB can clear that memory while unload unity from native application ??
    Thanks in advance..
     
  50. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    let me check onnce
     
    rkiOSDeveloper likes this.