IphoneX刘海

在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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值