目录(?)[-]
- 核心代码
- 自动化修改代码
- 参考资料
iPhoneX适配,比较搓的一种方式,在不修改分辨率(720 x 1280)的情况下适配
iphone X 主屏尺寸: 5.8英寸 主屏分辨率: 2436 x 1125
核心代码
修改 工程目录/Classes/UnityAppController.mm 文件
判断的方式比较搓,以 iPhone X 的宽高的 与众不同来判断
为了不调整分辨率的情况下适配(这里是 竖屏 应用),高度改为 150 是最好的值。(横屏 应用修改 height 为 width,y为 x 即可)
找到方法
代码
// _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];CGRect winSize = [UIScreen mainScreen].bounds;if (winSize.size.height / winSize.size.width > 2) {winSize.size.height -= 150;winSize.origin.y = 75;::printf("-> is iphonex hello world\n");} else {::printf("-> is not iphonex hello world\n");}_window = [[UIWindow alloc] initWithFrame: winSize];
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
运行效果
自动化修改代码
不用每次打xcode后都需要手动修改
参考:https://www.cnblogs.com/pandawuwyj/p/6904770.html
增加一个修改 文件的类
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO;namespace UnityEditor.XCodeEditor {public partial class XClassExt : System.IDisposable{private string filePath;public XClassExt(string fPath){filePath = fPath;if (!System.IO.File.Exists(filePath)){Debug.LogError(filePath + "not found in path.");return;}}public void WriteBelow(string below, string text){StreamReader streamReader = new StreamReader(filePath);string text_all = streamReader.ReadToEnd();streamReader.Close();int beginIndex = text_all.IndexOf(below);if (beginIndex == -1){Debug.LogError(filePath + " not found sign in " + below);return;}int endIndex = text_all.LastIndexOf("\n", beginIndex + below.Length);text_all = text_all.Substring(0, endIndex) + "\n" + text + "\n" + text_all.Substring(endIndex);StreamWriter streamWriter = new StreamWriter(filePath);streamWriter.Write(text_all);streamWriter.Close();}public void Replace(string below, string newText){StreamReader streamReader = new StreamReader(filePath);string text_all = streamReader.ReadToEnd();streamReader.Close();int beginIndex = text_all.IndexOf(below);if (beginIndex == -1){Debug.LogError(filePath + " not found sign in " + below);return;}text_all = text_all.Replace(below, newText);StreamWriter streamWriter = new StreamWriter(filePath);streamWriter.Write(text_all);streamWriter.Close();}public void Dispose(){}} }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
打包后处理,动态修改 xcode配置 及 文件内容
using System; using System.IO; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEditor.Callbacks;#if UNITY_EDITOR_OSXusing UnityEditor.iOS.Xcode; using UnityEditor.XCodeEditor;#endifpublic class Package {#if UNITY_EDITOR_OSX[PostProcessBuildAttribute(100)]public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject) {if (target != BuildTarget.iOS) {Debugger.LogWarning("Target is not iPhone. XCodePostProcess will not run");return;}// Create a new project object from build targetPBXProject project = new PBXProject();string configFilePath = PBXProject.GetPBXProjectPath(pathToBuiltProject);project.ReadFromFile(configFilePath);string targetGuid = project.TargetGuidByName("Unity-iPhone");string debug = project.BuildConfigByName(targetGuid, "Debug");string release = project.BuildConfigByName(targetGuid, "Release");project.AddBuildPropertyForConfig(debug, "CODE_SIGN_RESOURCE_RULES_PATH", "$(SDKROOT)/ResourceRules.plist");project.AddBuildPropertyForConfig(release, "CODE_SIGN_RESOURCE_RULES_PATH", "$(SDKROOT)/ResourceRules.plist");project.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", true);project.AddFrameworkToProject(targetGuid, "Security.framework", true);project.AddFrameworkToProject(targetGuid, "libz.tbd", true);project.AddFrameworkToProject(targetGuid, "libc++.tbd", true);project.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");project.WriteToFile(configFilePath);EditSuitIpXCode(pathToBuiltProject);}public static void EditSuitIpXCode(string path) {//插入代码//读取UnityAppController.mm文件string src = @"_window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];";string dst = @"// _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];CGRect winSize = [UIScreen mainScreen].bounds;if (winSize.size.height / winSize.size.width > 2) {winSize.size.height -= 150;winSize.origin.y = 75;::printf(""-> is iphonex aaa hello world\n"");} else {::printf(""-> is not iphonex aaa hello world\n"");}_window = [[UIWindow alloc] initWithFrame: winSize];";string unityAppControllerPath = path + "/Classes/UnityAppController.mm";XClassExt UnityAppController = new XClassExt(unityAppControllerPath);UnityAppController.Replace(src, dst);}#endif}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
参考资料
- http://blog.csdn.net/nicepainkiller/article/details/78926077