Unity Two methods are generally used in combination
1.[dllimport ("__internal")] C # call OC2.UnitySendMessage OC calls C # 1 C # calls OC in C # script using Unityengine;Using System.Collections;
Using system.runtime.interopservices;//Introduction
public class Newbehaviourscript:monobehaviour {
[DllImport ("__internal")]
private static extern void CallOC (); This method is the mm file method name in OC
Use this for initialization
void Start () {
CallOC (); Call
}
Update is called once per frame
void Update () {
}
}
in the mm file
#import
function implementation
#ifdef __cplusplus
extern "C" {
#endif
void CallOC ()
{
NSLog (@ "call to OC");
}
#ifdef __cplusplus
}
#endif
2 OC Call unity in code unity helps me encapsulate it.Unitysendmessage in Java to notify Unity can also usefirst in the mm file//This is the addition of a button to trigger a method
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];
Trigger method
-(void) buttoncall{
Unitysendmessage ("Cube", "Buttoncall", "" "); First parameter simultaneous model name 2 the model hangs the script method name 3 parameter
}
In C #, the script hangs on a cube Using Unityengine;
Using System.Collections;
public class Newbehaviourscript:monobehaviour {
void Buttoncall () {
Debug.Log ("OC buttoncall")
}
}
Unity C # and OC call each other