Unity调用IOS的StoreKit实现在游戏内部的对游戏进行星级评价和评论

一 Xcode端的OC代码

在Xcode里面新建一个空的工程(不会搞的百度一下),然后创建一个.h和.m文件,记住要把.m的后缀改成.mm(.mm文件和.m文件的区别就是:.mm文件除了可以包含Objective-C和C代码以外,还可以包含C++代码),这个类要继承自NSObject

.h 代码如下:

//

//  UnityStoreKit.h

//  test

//

//  Created by HH on 2018/4/13.

//  Copyright © 2018年 HH. All rights reserved.

//

扫描二维码关注公众号,回复: 37039 查看本文章


#import <Foundation/Foundation.h>

#import <StoreKit/StoreKit.h>


@interface UnityStoreKit : NSObject


@end

 .mm代码如下:

//

//  UnityStoreKit.mm

//  test

//

//  Created by HH on 2018/4/13.

//  Copyright © 2018年 HH. All rights reserved.

//


#import "UnityStoreKit.h"



@implementation UnityStoreKit

#if defined(__cplusplus)

extern "C"{

#endif

    void _goComment()

    {

        if([SKStoreReviewController respondsToSelector:@selector(requestReview)]) {// iOS 10.3 以上支持

            [SKStoreReviewController requestReview];

        } else { // iOS 10.3 之前的使用这个

            NSString *appId = @"1234567890"; //项目在苹果后台的appid

            NSString  * nsStringToOpen = [NSString  stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",appId];//替换为对应的APPID

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];

        }

    }

#if defined(__cplusplus)

}

#endif


@end

在软件内部进行星级评价是在IOS10.3之后的新特性。我们将这俩个文件导出到Unity里面的plugins文件夹下。把这个俩个文件所依赖的StoreKit在Unity里面给勾选上(勾上之后Unity打包成XCode文件的时候会自动把这个库给引用上)并且平台选择成IOS平台(这样打包成IOS的时候才会打包这俩个文件)。如下图所示:


二 Untiy里面的调用代码

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class UnityStoreKitMgr : MonoBehaviour {

    private static UnityStoreKitMgr _instance;
    public static UnityStoreKitMgr Instance{
        get{
            if(_instance==null)
            {
                GameObject go=     new GameObject ("UnityStoreKitMgr");
                _instance=go.AddComponent<UnityStoreKitMgr> ();
                DontDestroyOnLoad (go);
            }
            return _instance;
        }
    }


    [DllImport("__Internal")]
    private static extern void _goComment();
    public  void GoToCommnet()
    {    
        #if UNITY_IPHONE
        _goComment();
        #endif
    }

}

[DllImport("__Internal")]  

 这个我也不是很清楚。。反正就是扩展那一类的貌似是调用dl的一些函数(必写)

_goComment()必须和.mm文件里面的函数要一样。

三 调用的运行的截图



提交按钮是灰色的,是因为现在处于调试状态不用在意。

四  IOS回调Untiy

只有一个方式:

  UnitySendMessage("UnityStoreKitMgr","onCancel","params");

第一个参数:调用的Unity函数所在脚本绑定的游戏物体
第二个参数:调用的Unity函数名称
第三个参数:调用的Unity函数参数(只能是字符串类型,和android一样)




猜你喜欢

转载自blog.csdn.net/caohonghong123/article/details/80003287