unity-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];
    
  • 运行效果

    这里写图片描述


自动化修改代码

不用每次打xcode后都需要手动修改

参考:https://www.cnblogs.com/pandawuwyj/p/6904770.html

  1. 增加一个修改 文件的类

    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()
           {
    
           }
       }
    }
    
  2. 打包后处理,动态修改 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
    
    }
    


参考资料

以前玩cocos2d-x 是2.2.6版本,最近因为某些原因,下载了一个cocos2d-x v3.9. 在AppDelegate.app 里面有如下代码 ``` //自动生成的 /* static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);*/ //我自己修改的 static cocos2d::Size designResolutionSize = cocos2d::Size(320, 480); static cocos2d::Size smallResolutionSize = cocos2d::Size(640, 960); static cocos2d::Size mediumResolutionSize = cocos2d::Size(720, 1280); static cocos2d::Size largeResolutionSize = cocos2d::Size(1080, 1920); bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if (!glview) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) glview = GLViewImpl::createWithRect("plane", Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); #else glview = GLViewImpl::create("plane"); #endif director->setOpenGLView(glview); } // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // Set the design resolution glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT); Size frameSize = glview->getFrameSize(); // if the frame's height is larger than the height of medium size. /* if (frameSize.height > mediumResolutionSize.height) { director->setContentScaleFactor(MIN(largeResolutionSize.height / designResolutionSize.height, largeResolutionSize.width / designResolutionSize.width)); } // if the frame's height is larger than the height of small size. else if (frameSize.height > smallResolutionSize.height) { director->setContentScaleFactor(MIN(mediumResolutionSize.height / designResolutionSize.height, mediumResolutionSize.width / designResolutionSize.width)); } // if the frame's height is smaller than the height of medium size. else { director->setContentScaleFactor(MIN(smallResolutionSize.height / designResolutionSize.height, smallResolutionSize.width / designResolutionSize.width)); } register_all_packages(); // create a scene. it's an autorelease object auto scene = GameStart::createScene(); // run director->runWithScene(scene); return true; } ``` 在我的GameStart.cpp代码是这样的 ``` bool GameStart::init() { if (!Layer::init()) return false; //在cocos2d-x 2.2.6中,下面这段代码获取的是屏幕的大小 auto winSize=Director::getInstance()->getWinSize(); //添加背景图片 auto mainSprite = Sprite::create("images/background-main.png"); //设置位置为中心 mainSprite->setPosition(Vec2(winSize.width / 2, winSize.height / 2)); addChild(mainSprite); //添加log.设置为中心位置 auto logoSprite = Sprite::create("images/chuhui-log.png"); logoSprite->setPosition(Vec2(winSize.width / 2, winSize.height / 2)); this->addChild(logoSprite); //开启触摸 auto listener=EventListenerTouchOneByOne::create(); listener->onTouchBegan = [](Touch * touch, Event * event){ return true; }; listener->onTouchEnded = [](Touch * touch, Event * event){ Director::getInstance()->replaceScene(MyDescription::createScene()); }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true; } ``` 结果就是,要么无法放大,要么拉伸过于明显.对了,还有我这个游戏是竖屏的. 希望大神能够解救一下.别再说什么百度了,我连谷歌都去了.可能是我太菜了,我依然还是没有能够解决这个问题. 希望大神能够帮我一下. 就这个问题,困扰我两天了,这个问题解决不了,下面的问题就无法进行. 在线等....
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页