Unity C # and OC call each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.