Skip to content

Instantly share code, notes, and snippets.

@sapanda
Created October 16, 2017 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapanda/a09ff0fbc49e4bb58e44cbfa758f66c2 to your computer and use it in GitHub Desktop.
Save sapanda/a09ff0fbc49e4bb58e44cbfa758f66c2 to your computer and use it in GitHub Desktop.
Unity PostBuild script to solve the iOS WWW crash on Unity 2017
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
namespace CompanyName.Editor {
/// <summary>
/// Override the Xcode project file source code on PostBuild
/// </summary>
public class UnityIOSSourceOverridePostBuild {
private const string OverridePath = "UnityOverride/Overrides";
private const string OverrideExtension = ".override";
private static string[] IgnoredExtensions = new string[] {
".meta",
".DS_Store"
};
[PostProcessBuild(100)]
public static void OnPostprocessBuild(BuildTarget buildTarget, string buildPath) {
if (buildTarget.ToString () == "iOS" || buildTarget.ToString () == "iPhone") {
SwapUnityIOSSource (buildTarget, buildPath);
}
}
private static void SwapUnityIOSSource(BuildTarget buildTarget, string buildPath) {
string srcPath = Path.Combine(Application.dataPath, OverridePath);
string dstPath = buildPath;
ReplaceFiles (srcPath, dstPath);
}
/// <summary>
/// Replace files in dstPath with files in srcPath
/// </summary>
private static void ReplaceFiles(string srcPath, string dstPath) {
DirectoryInfo dir = new DirectoryInfo(srcPath);
DirectoryInfo[] subDirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files) {
if (!IgnoreFile(file)) {
string dstFilePath = Path.Combine(dstPath, file.Name);
int overrideExtensionIndex = dstFilePath.LastIndexOf (OverrideExtension);
if (overrideExtensionIndex >= 0) {
dstFilePath = dstFilePath.Remove (overrideExtensionIndex);
}
file.CopyTo(dstFilePath, true /* replace */);
}
}
foreach (DirectoryInfo subDir in subDirs) {
string dstSubDir = Path.Combine(dstPath, subDir.Name);
ReplaceFiles(subDir.FullName, dstSubDir);
}
}
/// <summary>
/// Should this file be ignored because of its extension?
/// </summary>
private static bool IgnoreFile(FileInfo file) {
bool ignore = false;
foreach (string ext in IgnoredExtensions) {
if (file.Extension.Equals (ext)) {
ignore = true;
break;
}
}
return ignore;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment