Unity发送参数给iOSNative并响应

unity想要给iOS客户端发送通知并相应。语言太苍白直接上代码。

unity端创建两个C#文件

1.触发cs这个不用多说,大家估计都懂。

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    //声明两个Texture变量,图片资源在外面连线赋值
    public Texture Button0;// Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    //这个方法用于绘制
    void OnGUI() {
        //绘制两个按钮
        if(GUI.Button(new Rect(0,44,120,120),Button0))
        {
            //返回值为ture说明这个按钮被点击
            SDK.ActivateButton0("sss");
        }    
    }
}

2.真正的肉戏,SDK.cs两个文件名字都是随便起的。小伙伴可以根据需求替换

using UnityEngine;
using System.Runtime.InteropServices;

public class SDK
{
    [DllImport("__Internal")]
    private static extern void _PressButton0 (string buy_id);

    public static void ActivateButton (string buy_id)
    {
        if (Application.platform != RuntimePlatform.OSXEditor) 
        {
            _PressButton0 (buy_id);
        }
    }
}

2.iOS端需要做的,创建两个文件。直接拖进去就好了,什么都不用做。

//
//  MyView.h
//  Unity-iPhone
//
//  Created by Aaron on 16/12/27.
//
//

#import <Foundation/Foundation.h>

@interface MyView : NSObject

@end
//
//  MyView.m
//  Unity-iPhone
//
//  Created by Aaron on 16/12/27.
//
//

#import "MyView.h"

@implementation MyView

//接收Unity3D 传递过来的信息
void _PressButton (char *buy_id)
{
    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"1"];
    [alert setMessage:[NSString stringWithUTF8String:buy_id]];
    [alert addButtonWithTitle:@"确定"];
    [alert  show];
}


@end

至此,unity端给移动端的传值已经结束。

ps:导入后报两个错误,意思说预编译的时候出错。注意不要把添加的.h和.m文件。画蛇添足将.m改成.mm。

原文地址:https://www.cnblogs.com/fuunnyy/p/6226478.html