在UnityAppController.mm中
#import <sys/utsname.h>
+ (NSString*)getDeviceVersion
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceVersion = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
return deviceVersion;
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
//_window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
CGRect winSize = [UIScreen mainScreen].bounds;
if ([deviceVersion isEqualToString:@"iPhone10,3"] ||
[deviceVersion isEqualToString:@"iPhone10,6"])
{
//width>height
winSize.size.width -= 32;
winSize.origin.x = 32;
}
_window=[[UIWindow alloc]initWithFrame:winSize];
_unityView = [self createUnityView];
以上参考:https://blog.csdn.net/qqo_aa/article/details/78487775?readlog
//在Unity中
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()
{
}
}
}
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);
EditDefferingGestures(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 ([deviceVersion isEqualToString:@""iPhone10,3""] || [deviceVersion isEqualToString:@""iPhone10,6""])
{
//width>height
winSize.size.width -= 32;
winSize.origin.x = 32;
}
_window = [[UIWindow alloc] initWithFrame: winSize];
";
string unityAppControllerPath = path + "/Classes/UnityAppController.mm";
XClassExt UnityAppController = new XClassExt(unityAppControllerPath);
UnityAppController.Replace(src, dst);
}
//.....
public static void EditDefferingGestures(string path) {
}
#endif
//以上不完整,需要自己补充,就不多说了
参考:https://blog.csdn.net/yangxuan0261/article/details/79403244