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


    废话不多说直接上代码。

    一 Xcode端的OC代码

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

    .h代码如下:

    //
    //  UnityStoreKit.h
    //  UnityStoreKit
    //
    //  Created by mac on 2017/12/14.
    //  Copyright © 2017年 mac. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import <StoreKit/StoreKit.h>
    
    @interface UnityStoreKit : NSObject
    
    @end
    

     .mm代码如下:

    //
    //  UnityStoreKit.m
    //  UnityStoreKit
    //
    //  Created by mac on 2017/12/14.
    //  Copyright © 2017年 mac. 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 = @"1280215473";
                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一样)

  • 相关阅读:
    IBM项目(项目管理)
    MSN附加照片问题解决
    国家图书馆新馆
    『Python Kivy』Kivy and PyDev on Eclipse
    『C#基础』C#导出Excel
    『Python Kivy』什么是Kivy,以及Hello world
    『创建型』简单工厂SimpleFactory、工厂方法FactoryMethod、抽象工厂AbstractFactory
    『C#基础』多线程笔记「三」计时器
    『C#基础』多线程笔记「二」线程同步
    『创建型』单例模式Singleton学习笔记
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/8099214.html
Copyright © 2020-2023  润新知