Unity 兩種方式 一般都是組合使用
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 () {
}
}
#import
// 函數實現
#ifdef __cplusplus
extern "C" {
#endif
void CallOC()
{
NSLog(@"調用到了OC");
}
#ifdef __cplusplus
}
#endif
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參數
}
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
void buttonCall () {
Debug.Log("OC buttonCall")
}
}