码迷,mamicode.com
首页 > 编程语言 > 详细

Unity C#和OC互相调用

时间:2016-01-22 14:23:36      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

 Unity  两种方式 一般都是组合使用

1.[DllImport("__Internal")]  C#调用oc
2.UnitySendMessage        oc调用C#
 
 1 C#调用oc 在C#脚本中
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;//引入

public class NewBehaviourScript : MonoBehaviour {
    [DllImport("__Internal")] 
    private static extern void CallOC();   //该方法为oc 中mm文件方法名称
    // Use this for initialization
    void Start () {
        CallOC ();                                     //调用
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}
 
在MM文件中

#import

// 函数实现

 #ifdef __cplusplus

 extern "C" {

     #endif

    

         void CallOC()

         {

             NSLog(@"调用到了OC");

             

         }

     #ifdef __cplusplus

     }

 #endif

 
 
2 oc调用unity中代码  unity 帮我封装好的
UnitySendMessage 在java通知unity 同样可以使用 
首先在MM文件中
//这段就是加了一个按钮 触发一个方法 

 UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [but setImage:[UIImage imageNamed:@"button1.png"] forState:UIControlStateNormal];

    [but setImage:[UIImage imageNamed:@"button2.png"] forState:UIControlStateHighlighted];

    

    but.frame=CGRectMake(20, 20, 50, 60);

    [self.view addSubview:but];

   [but addTarget:self action:@selector(buttonCall) forControlEvents:UIControlEventEditingDidEnd];

//触发方法

      -(void)buttonCall{

                 UnitySendMessage("Cube", "buttonCall", ""); //第一个参数 同时模型名称 2 该模型挂的脚本方法名称  3参数

        }

 
 
在C#中  该脚本 挂在一个Cube上
 
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
    void buttonCall () {
        Debug.Log("OC buttonCall")
    }

}

Unity C#和OC互相调用

标签:

原文地址:http://www.cnblogs.com/sytfyf/p/5150786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!