Documentation / 08

Unity

In the Xcode project Unity exports for iOS, Unity-iPhone embeds UnityFramework, the target that holds the plugins' native code. The SwiftPods package for Unity installs the Podfile's dependencies into UnityFramework after every iOS build.

Setup

  1. Add this entry under dependencies in Packages/manifest.json:
    "org.swiftpods.unity": "https://github.com/swiftpods-org/swiftpods.git?path=Unity#v<version>"
  2. Commit Packages/packages-lock.json to record the Unity package's exact Git revision.
  3. Install SwiftPods on every Mac that builds the game and set SWIFTPODS to its trusted absolute path. An editor started from the Dock or Unity Hub does not see variables set in a shell, so set it for apps and restart Unity Hub:
    launchctl setenv SWIFTPODS /opt/homebrew/bin/swiftpods
    The setting lasts until you log out; in CI, set the variable in the job.
  4. Write the Podfile in ios/ next to Assets/. Inside Assets/, Unity would import its Swift files as plugins.
    platform :ios, '15.0'
    
    target 'UnityFramework' do
      spm 'Firebase', '12.0.0', products: %w[FirebaseCore FirebaseAnalytics FirebaseCrashlytics]
      spm 'Facebook', '~> 18.0.0', products: %w[FacebookCore FacebookLogin FacebookShare FacebookGamingServices]
    end
    Leave out project: the package passes the exported one. Declare the versions the Unity plugins list in their *Dependencies.xml; the native libraries of the Firebase Unity SDK, for example, are built for one Firebase iOS version.
  5. Add ios/SwiftPods/ to .gitignore.

Unity-iPhone needs no entry: SwiftPods embeds the dynamic frameworks that UnityFramework links.

On every iOS build

If EDM4U's iOS Resolver is installed, the package turns off its Podfile generation before the build. EDM4U saves the setting in ProjectSettings/GvhProjectSettings.xml; commit that change.

After the other plugins' post-processing, it updates the Firebase Crashlytics Run Script to use Xcode's checkout of firebase-ios-sdk, then runs swiftpods install --project <export>/Unity-iPhone.xcodeproj in ios/. A SwiftPods error fails the Unity build.

Both build modes work: Replace exports a new project, and Append keeps it for swiftpods install to bring up to date. There is no .xcworkspace, so build Unity-iPhone.xcodeproj with xcodebuild -project or Fastlane's project:.

Environment

SWIFTPODS_PODFILE_DIRECTORYThe Podfile's directory, relative to the Unity project. Default: ios.
SWIFTPODSRequired on macOS: a trusted absolute path to swiftpods. The Unity package does not search the ambient PATH.
SWIFTPODS_ALLOW_EXTERNAL_FILE_LISTSSet to 1 only to trust script file lists outside the Podfile directory.

On Windows the package installs nothing and logs the command to run on a Mac.