简体   繁体   English

在Unity iOS上打开设置应用程序

[英]Open Settings application on Unity iOS

I am in need of a way to make the user is taken to the Settings application to disable the multitasking gestures. 我需要一种使用户进入“设置”应用程序以禁用多任务手势的方法。 I know that in iOS 8 you can launch the Settings application programmatically through the URL in Objective-C: 我知道在iOS 8中,您可以通过Objective-C中的URL以编程方式启动“设置”应用程序:

NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];

But I do not know how to get this URL in Unity to use with Application.OpenURL() 但是我不知道如何在Unity中获取此URL以与Application.OpenURL()一起使用

You need to write a tiny iOS plugin for that, here is more information about it: http://docs.unity3d.com/Manual/PluginsForIOS.html 您需要为此编写一个微型iOS插件,此处是有关它的更多信息: http : //docs.unity3d.com/Manual/PluginsForIOS.html

And here is your solution, ask if something should be unclear. 这是您的解决方案,请问是否不清楚。

Script/Example.cs 脚本/ Example.cs

using UnityEngine;

public class Example 
{
    public void OpenSettings()
    {
        #if UNITY_IPHONE
            string url = MyNativeBindings.GetSettingsURL();
            Debug.Log("the settings url is:" + url);
            Application.OpenURL(url);
        #endif
    }
}

Plugins/MyNativeBindings.cs 插件/ MyNativeBindings.cs

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

        [DllImport ("__Internal")]
        public static extern void OpenSettings();
    #endif
}

Plugins/iOS/MyNativeBindings.mm 插件/ iOS版/ MyNativeBindings.mm

extern "C" {
    // 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];
    }
}

Using idea of JeanLuc, I create a empty XCode project and print the string constant UIApplicationOpenSettingsURLString and used in Unity with Application.OpenURL() to not have to use a plugin. 使用JeanLuc的想法,我创建了一个空的XCode项目,并打印字符串常量UIApplicationOpenSettingsURLString并在Unity中与Application.OpenURL()一起使用,而不必使用插件。 Works very nice. 效果很好。

The value for constant UIApplicationOpenSettingsURLString is: "app-settings:" (without quotas). 常量UIApplicationOpenSettingsURLString值为:“ app-settings:”(无配额)。

Use: Application.OpenURL("app-settings:") to open directly from unity 使用: Application.OpenURL("app-settings:")直接从统一打开

WARNING: The use of hardcoded strings is dangerous and can break your code if Apple change the value of constant UIApplicationOpenSettingsURLString . 警告:使用硬编码的字符串很危险,如果Apple更改常量UIApplicationOpenSettingsURLString的值,则可能会破坏您的代码。 Its just a workaround while Unity does not add a constant for reference in C# code. 这只是一个变通方法,而Unity并未在C#代码中添加常量以供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM