原地址:http://www.cnblogs.com/qingjoin/p/3638915.html
Unity 3D 简单工程的创建。与Xcode 导出到iOS 平台请看这
Unity3D 学习 创建简单的按钮、相应事件
Unity C# 代码
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices; public class testFeil : MonoBehaviour { [DllImport("__Internal")]
private static extern void c_ctest(); // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } void OnGUI()
{
//开始按钮 创建了一个按钮。
if(GUI.Button(new Rect(20,100,200,50),"qingyun "))
{
//System.Console.WriteLine("hello world");
print("hello qingyun !");
c_ctest();
// Debug.Log("up.up");
} } void testResult (string msg)
{
Debug.Log("testResult:" +msg);
} //这个是C#里的一个回调。用来接收数据是否传送成功。----这里的作用就是OC里的回调。
void testBtnResult (string msg)
{
Debug.Log ("btnPressSuccessssssssssss:"+msg);
}
}
.mm 文件里的OC 与 C 接口
//
// MeiyuPushSdkC.m
// MeiYuPushDemo
//
// Created by qingyun on 3/31/14.
// Copyright (c) 2014 qingyun. All rights reserved.
// #import "MeiyuPushSdkC.h"
#import "MeiyuPushSdk.h" @interface MeiyuPushSdkC : NSObject<MeiyuPushSdkDelegate> @end @implementation MeiyuPushSdkC - (id)init
{
id object = [super init]; [[MeiyuPushSdk my_initPush]setDelegate:self];
//sdk.delegate = self;
NSLog(@"initMeiyuPushSdkC");
return object;
} -(void)cTestResult
{
UnitySendMessage("Main Camera", "testBtnResult", "成功啦.哇哈哈哈");
} @end MeiyuPushSdkC *m_pushSdk = NULL; #if defined (__cplusplus)
extern "C"
{
#endif void c_ctest()
{
if(m_pushSdk == NULL)
{
m_pushSdk = [[MeiyuPushSdkC alloc]init];
}
MeiyuPushSdk *m = [MeiyuPushSdk my_initPush]; //objc_msgSend(obj, @selector(setName:),@"balabala");
[m cTest];
NSLog(@"testSuccess---------");
UnitySendMessage("Main Camera", "testResult", "-10862904$_^_$接口调用失败");
} #if defined (__cplusplus)
}
#endif
这样就把Unity3d 与OC 连到了一起。
具体可参考 unityFile.zip