Skip to content

Instantly share code, notes, and snippets.

@dddnuts
Last active March 27, 2023 15:53
Show Gist options
  • Save dddnuts/bd5b69c23fa6b38c91a1e11a182e4b46 to your computer and use it in GitHub Desktop.
Save dddnuts/bd5b69c23fa6b38c91a1e11a182e4b46 to your computer and use it in GitHub Desktop.
Configure Unity Xcode project to be compatible with Swift & Carthage
github "questbeat/QBImagePicker"
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'xcodeproj'
proj = Xcodeproj::Project.new('Unity-iPhone.xcodeproj')
proj.initialize_from_file
proj.targets.each do |target|
# Delete if the same run script phase exists
target.build_phases.delete_if do |phase|
phase.is_a?(Xcodeproj::Project::Object::PBXShellScriptBuildPhase) &&
phase.name == 'Carthage Copy Frameworks'
end
# Create a new run script phase
run_script = proj.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
run_script.name = 'Carthage Copy Frameworks'
run_script.shell_script = '/usr/local/bin/carthage copy-frameworks'
run_script.input_paths = Dir.glob('Carthage/Build/iOS/*.framework').map do |framework|
"$(SRCROOT)/#{framework}"
end
target.build_phases << run_script
end
proj.save
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
public class CarthageConfigurator
{
[PostProcessBuild]
static void Configure(BuildTarget buildTarget, string buildPath)
{
if (buildTarget != BuildTarget.iOS)
return;
RunCarthageUpdate(buildPath);
AddCarthageFrameworks(buildPath);
AddCopyFrameworksBuildPhase(buildPath);
}
static void RunCarthageUpdate(string buildPath)
{
var files = new List<string>
{
"Cartfile",
"Cartfile.resolved"
};
foreach (var x in files)
{
File.Copy(CombinePaths(Application.dataPath, "Editor", "Carthage", x), CombinePaths(buildPath, x), true);
}
var command = new ProcessStartInfo
{
WorkingDirectory = buildPath,
FileName = "/usr/local/bin/carthage",
Arguments = "update --platform iOS",
CreateNoWindow = true
};
Process process = Process.Start(command);
process.WaitForExit();
process.Close();
}
static void AddCarthageFrameworks(string buildPath)
{
var project = new PBXProject();
var path = PBXProject.GetPBXProjectPath(buildPath);
project.ReadFromFile(path);
var target = project.TargetGuidByName(PBXProject.GetUnityTargetName());
var frameworks = Directory.GetDirectories(CombinePaths(buildPath, "Carthage", "Build", "iOS"), "*.framework");
foreach (var x in frameworks)
{
var name = Path.GetFileName(x);
project.AddFileToBuild(target, project.AddFile("Carthage/Build/iOS/" + name, "Frameworks/" + name, PBXSourceTree.Source));
}
project.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Carthage/Build/iOS");
project.SetBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
project.WriteToFile(path);
}
static void AddCopyFrameworksBuildPhase(string buildPath)
{
var files = new List<string>
{
"Gemfile",
"carthage_copy_frameworks.rb"
};
foreach (var x in files)
{
File.Copy(CombinePaths(Application.dataPath, "Editor", "Carthage", x), CombinePaths(buildPath, x), true);
}
var commands = new List<ProcessStartInfo>
{
new ProcessStartInfo
{
Arguments = "gem install bundler --no-ri --no-rdoc"
},
new ProcessStartInfo
{
Arguments = "bundle install --path vendor/bundle"
},
new ProcessStartInfo
{
Arguments = "bundle exec ruby carthage_copy_frameworks.rb"
}
};
foreach (var x in commands)
{
x.EnvironmentVariables["PATH"] = string.Join(":", new string[]
{
x.EnvironmentVariables["HOME"] + "/.rbenv/shims",
x.EnvironmentVariables["PATH"]
});
x.UseShellExecute = false;
x.FileName = "/bin/sh";
x.WorkingDirectory = buildPath;
x.CreateNoWindow = true;
var process = Process.Start(x);
process.WaitForExit();
process.Close();
}
}
private static string CombinePaths(params string[] paths)
{
return paths.Aggregate(Path.Combine);
}
}
source 'https://rubygems.org'
gem 'xcodeproj', '1.0.0.rc.1'
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
using System.Linq;
public class SwiftConfigurator
{
[PostProcessBuild]
static void Configure(BuildTarget buildTarget, string buildPath)
{
if (buildTarget != BuildTarget.iOS)
return;
EnableSwift(buildPath);
}
static void EnableSwift(string buildPath)
{
var project = new PBXProject();
var path = PBXProject.GetPBXProjectPath(buildPath);
project.ReadFromFile(path);
var target = project.TargetGuidByName(PBXProject.GetUnityTargetName());
project.SetBuildProperty(target, "EMBEDDED_CONTENT_CONTAINS_SWIFT", "YES");
project.WriteToFile(path);
}
}
@RomainBitard
Copy link

for SwiftConfigurator now with unity 2021.3+ I get this error
Method 'UnityEditor.iOS.Xcode.PBXProject.GetUnityTargetName()' is obsolete: This function is deprecated. There are two targets now, call GetUnityMainTargetGuid() - for app or GetUnityFrameworkTargetGuid() - for source/plugins to get Guid instead of calling to TargetGuidByName(GetUnityTargetName()).

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment