KITASENJU DESIGN BLOG

memo, html, javascript, unity

iosのdialogueをunityで出す

iOSのダイアログを出す

github.com

_button.onClick.AddListener(_onClick);

    private void _onClick(){
        
        string url = MyNativeBindings.GetSettingsURL();
        CBNativeDialog.Instance.Show(title: "アクセス許可",
            message: "動画を保存するには写真へのアクセスを許可してください",
            positiveButtonTitle: "OK",
            positiveButtonAction: () => { Application.OpenURL(url); },
            negativeButtonTitle: "CANCEL",
            negativeButtonAction: () => { Debug.Log("CANCEL"); });

    }

設定を開く

//MyNativeBindings.cs

    using UnityEngine;
    using UnityEngine.Scripting;
    using System;
    using System.Runtime.InteropServices;

public class MyNativeBindings 
{
    #if UNITY_IPHONE
        [DllImport ("__Internal")]
        public static extern string GetSettingsURL();

        [DllImport ("__Internal")]
        public static extern void OpenSettings();
    #endif
}
//MyNativeBindings.mm
extern "C" {

    //https://stackoverflow.com/questions/30010334/open-settings-application-on-unity-ios

    // Helper method to create C string copy
    char* MakeStringCopy (NSString* nsstring)
    {
        if (nsstring == NULL) {
            return NULL;
        }
        // convert from NSString to char with utf8 encoding
        const char* string = [nsstring cStringUsingEncoding:NSUTF8StringEncoding];
        if (string == NULL) {
            return NULL;
        }

        // create char copy with malloc and strcpy
        char* res = (char*)malloc(strlen(string) + 1);
        strcpy(res, string);
        return res;
    }

    const char* GetSettingsURL () {
         NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
         return MakeStringCopy(url.absoluteString);
    }

    void OpenSettings () {
        NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL: url];
    }
}
"FOOTER"