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

告别手动配置,非常方便

 

posted @   三页菌  阅读(595)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2017-11-09 ClassFoo-IT知识花园
点击右上角即可分享
微信分享提示