Xcode 编写 Unity 在Mac 平台的Bundle插件
- Xcode创建工程,选择MacOs 的框架和库,中的bundle
- 创建和 bundle 相同名字的 cpp文件自动生成头hpp文件
- 头文件代码如下
#pragma once
#if UNITY_METRO
#define EXPORT_API __declspec(dllexport) __stdcall
#elif UNITY_WIN
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API
#endif
#include <stdio.h>
实现cpp中如下
extern "C" int EXPORT_API TestAB(int a,int b)
{
return a+b;
}
extern "C"
{
int TestAC(int a);
}
int TestAC(int w )
{
return w * 10;
}
选中 Products 中的bundle 显示在finder中,拷贝复制到Unity 工程的Asset/Plugin 下
然后在c#中调用,调用的代码形式如下
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class StartMain : MonoBehaviour {
[DllImport("NetPlugin")]
public static extern int TestAB(int a, int b);
[DllImport("NetPlugin")]
public static extern int TestAC(int c);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void ShowAdd()
{
int a = TestAB(1, 2);
Debug.Log("AB : "+a);
int c = TestAC(9);
Debug.Log("AC : " + c);
}
}