12
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnityからiOSのステータスバーを動的にOn/Offする

Last updated at Posted at 2018-11-11

UnityからiOSのステータスバーを動的にOn/Offしようとしたらそれなりに安全なやり方は一手間必要だった

iOS 9からprefersStatusBarHiddenをoverrideする方式になった

  • objective c - Hide the status bar in ios 9 - Stack Overflow https://stackoverflow.com/questions/32965610/hide-the-status-bar-in-ios-9
  • iOS 9からprefersStatusBarHiddenをoverrideする方式になった
  • prefersStatusBarHiddenを設定から読むoverride実装があるので参考にする
  • FullScreenVideoPlayer.mmを参考にclass_replaceMethodでインスタンスのメソッドをreplaceする

IOSUtil.cs

Assets/IOSStatusBar/Plugins/iOS/IOSUtil.csあたりに配置

IOSUtil.cs
using System.Runtime.InteropServices;

namespace NativeUtil
{
    public static class IOSUtil
    {
#if UNITY_IOS && !UNITY_EDITOR
        [DllImport ("__Internal")]
	    static extern void _setStatusBarEnabled (bool isEnabled);
#endif

        public static void SetStatusBarEnabled(bool isEnabled)
        {
#if UNITY_IOS && !UNITY_EDITOR
            _setStatusBarEnabled(isEnabled);
#endif
        }
    }
}

IOSUtil.mm

Assets/IOSStatusBar/Plugins/iOS/IOSUtil.mmあたりに配置

IOSUtil.mm
#import <Foundation/Foundation.h>
#include "Unity/ObjCRuntime.h"
#include "UnityAppController.h"

#define UIViewController_preferStatusBarHidden_Enc "B16@0:8"

extern "C"
{
    // ref. FullScreenVideoPlayer.mm
    static bool _isStatusBarHidden = false;
    static bool preferStatusBarHidden_Impl(id self_, SEL _cmd){
        return _isStatusBarHidden;
    }
    
    static bool _isStatusBarInited = false;
    void _setStatusBarEnabled(bool isEnabled)
    {
        NSLog(@"_setStatusBarEnabled %d", isEnabled);
        UIViewController *vc = GetAppController().rootViewController;
        if (!_isStatusBarInited) {
            _isStatusBarHidden = vc.prefersStatusBarHidden;
            class_replaceMethod([vc class], @selector(prefersStatusBarHidden), (IMP)&preferStatusBarHidden_Impl,
                                UIViewController_preferStatusBarHidden_Enc);
            _isStatusBarInited = true;
        }
        _isStatusBarHidden = !isEnabled;
        [vc setNeedsStatusBarAppearanceUpdate];
    }
}

# つかいかた

IOSUtil.SetStatusBarEnabled(bool);

Unityプロジェクト一式

https://github.com/youten/UnityiOSStatusBar に置いた

License

Unity本体のObj-cなコード参考にしてる部分はたぶん普通のOSSライセンスで含められないのでCC0で放棄

12
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?