zoukankan      html  css  js  c++  java
  • Unity C#和OC互相调用

     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")
        }

    }
  • 相关阅读:
    PYTHON压平嵌套列表
    linux下IPTABLES配置详解
    Python面试必须要看的15个问题
    两个实用的Python的装饰器
    Python的16个“坑”
    python实现不可修改的常量
    51nod-1322: 关于树的函数
    51nod-1310: Chandrima and XOR
    51nod-1296: 有限制的排列
    51nod-1277: 字符串中的最大值
  • 原文地址:https://www.cnblogs.com/sytfyf/p/5150786.html
Copyright © 2011-2022 走看看