Unity iPhoneX适配教程
发表于2018-06-21
随着iPhoneX的发布,与之对应的软件和游戏都需要根据iPhoneX制定相应的适配工作,而下面和大家介绍的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];
运行效果

自动化修改代码
增加一个修改 文件的类
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()
{
}
}
}
打包后处理,动态修改 xcode配置 及 文件内容
using System;
using System.IO;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.Callbacks;
#if UNITY_EDITOR_OSX
using UnityEditor.iOS.Xcode;
using UnityEditor.XCodeEditor;
#endif
public 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 target
PBXProject 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
}
