unity调用ios原生代码objective-c和回调
从c#到objective-c学习
https://www.runoob.com/w3cnote/objective-c-tutorial.html
https://www.jianshu.com/p/6328c55ac4b2
http://www.cnblogs.com/wuhuacong/p/3589699.html
进阶 https://www.xuanyusong.com/archives/category/ios/objective-c
网上说的教程太复杂,这里我给个最简单版本的
但是首先你要学会ios打包发布 https://www.cnblogs.com/sanyejun/p/8308873.html
第一步 新建一个NativeBinding.mm文件在上图这个位置,名字随便,别的也行
里面的代码
extern "C" { void FooPluginFunction() { //打log NSLog(@"Hello World!"); //回调unity UnitySendMessage("OSEvent","testBtnResult", "chuang_chuang"); } }
如果这个方法调用成功,那么xcode控制台会打log,并且回调unity方法 testBtnResult
新建物体,挂上脚本
这个脚本
using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.UI; public class CallOS : MonoBehaviour { public Button testButton; public Text text; // Use this for initialization void Start () { int a = 1; testButton.onClick.AddListener(delegate { Debug.Log("click"); text.text = "haha" + a++; if (Application.platform == RuntimePlatform.IPhonePlayer) { //点击按钮触发 FooPluginFunction(); } }); } //ios原生方法接口,方法名和mm文件里面的方法相同 [DllImport("__Internal")] static extern void FooPluginFunction(); //objective-c方法的回调 public void testBtnResult(string msg) { Debug.Log("btnPressSuccessssssssssss:" + msg); } }
XUPorter插件自动配置sdk
http://www.xuanyusong.com/archives/2720
https://fengyu.name/article/469
告别手动配置,非常方便