Unity3d Academy's iOS Advanced interface sends messages and receives UNITY3D messages

Source: Internet
Author: User

Today, we are discussing the interaction between iOS's advanced interface and the unity3d game engine , which is very important in development,Unity3d is a game engine for multi-platform, It's not all about iOS, and there's no use of controls in the engine for iOS's advanced interface.

This example realizes the game background is the Unity3D game World, the front Add 4 iOS Advanced Interface button, and click these buttons can pass the message to the background Unity3d, lets it do some things.
The previous chapter describes the touch of the iOS screen mobile camera location, below a friend asked me that he did not want to move the location of the camera, just want to move the location of the object, I add here, you can bind the script on the box, the reference to choose the main camera, this kind of update box script is OK. For today's example, I'll bind the script to the box, as shown in, and bind the move script to the cube.


First put the code of the move script, which I wrote 4 ways to handle the rotation of the box, the 4 methods are sent by the code on iOS to Unity message after the call, I will describe the method of the specific operation.


Create a full-screen window _window = [[UIWindow alloc] initwithframe:rect];    eaglview* view = [[Eaglview alloc] initwithframe:rect];      [_window Addsubview:view];    MyView * MyView = [[MyView alloc] init];   [_window AddSubview:myView.view]; put out MyView code, write the discovery forgot to release memory, oh, too lazy to change, this chapter is not the main introduction of this OH. myview.m//unity-iphone////Created by rain pine Momo on 11-11-1.//Copyright __mycompanyname__. All rights reserved.//#import "MyView.h" @implementation MyView//Implement Viewdidload to doing additional setup after    Loading the view, typically from a nib.-(void) viewdidload {[Super viewdidload];    Create a label view UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, 320, 40)];    Set the display content Label.text = @ "The program world of the Momo of rain and pine";    Set background Color Label.backgroundcolor = [Uicolor Bluecolor];    Set text color Label.textcolor = [Uicolor Whitecolor];    Set display position centered label.textalignment = Uitextalignmentcenter; Set Font Size Label.font = [Uifont fontwithname:[[uifont familynames] objECTATINDEX:10] size:20];    Create button UIButton *button0 = [UIButton buttonwithtype:1];    Set the button range Button0.frame = CGRectMake (0, 40, 100, 30);    Set button display [Button0 settitle:@ "Rectangle left rotation" forstate:uicontrolstatenormal]; Set the button to bind the response method after changing [Button0 addtarget:self action: @selector (leftbuttonpressed) forControlEvents:        UIControlEventTouchUpInside];    Create button UIButton *button1 = [UIButton buttonwithtype:1];    Set the button range Button1.frame = CGRectMake (0, 100, 100, 30);    Set button display [button1 settitle:@ "Rectangle right rotation" forstate:uicontrolstatenormal]; Set the button to bind the response method after changing [Button1 addtarget:self Action: @selector (rightbuttonpressed) forControlEvents:        UIControlEventTouchUpInside];    Create button UIButton *button2 = [UIButton buttonwithtype:1];    Set the button range Button2.frame = CGRectMake (0, 160, 100, 30);    Set button display [button2 settitle:@] Rotate "forstate:uicontrolstatenormal" on the rectangle; Set the button to bind the response method after the change [Button2 addtarget:self action: @selector (upbuttonpressed) Forcontrolevents:uicontrolevenTtouchupinside];    Create button UIButton *button3 = [UIButton buttonwithtype:1];    Set the button range Button3.frame = CGRectMake (0, 220, 100, 30);    Set button display [Button3 settitle:@] rotate "forstate:uicontrolstatenormal"; Set the button to bind the response method after changing [Button3 addtarget:self action: @selector (downbuttonpressed) forControlEvents:        UIControlEventTouchUpInside];    Add [Self.view Addsubview:label] to view;    [Self.view Addsubview:button0];    [Self.view Addsubview:button1];    [Self.view Addsubview:button2];  [Self.view Addsubview:button3];}  Left button-(void) leftbuttonpressed{unitysendmessage ("Cube", "MoveLeft", "");} Right button-(void) rightbuttonpressed{unitysendmessage ("Cube", "MoveRight", "");}  Up button-(void) upbuttonpressed{unitysendmessage ("Cube", "MoveUp", "");}  Down button-(void) downbuttonpressed{unitysendmessage ("Cube", "MoveDown", "" ");}    -(void) didreceivememorywarning {//Releases the view if it doesn ' t has a superview.      [Super didreceivememorywarning]; Release any cached daTA, images, etc. that aren ' t in use.}  -(void) viewdidunload {[Super viewdidunload];} -(void) Dealloc {[Super Dealloc];}

Here you can export this unity project to the Xcode project, not the pot friends please look at my previous article ha, Xcode Project export Success, we first add 4 Advanced interface buttons to click on the response to the above Script 4 rotation box method.

Create a class to inherit Uiviewcontroller, to add a view of our advanced interface, I'll name it myview.

Open Unity3d exported appcontroller.mm This class, the header file into our class #import "MyView" Find the following method to add Viewint openeagl_unitycallback (UIWindow * * window, int* screenwidth, int* screenheight, int* openglesversion)



Eaglview is the view of the Unity3d background, below we add a view that we write ourselves to cover it.

Create a full-screen window _window = [[UIWindow alloc] initwithframe:rect];    eaglview* view = [[Eaglview alloc] initwithframe:rect];      [_window Addsubview:view];    MyView * MyView = [[MyView alloc] init];   [_window AddSubview:myView.view]; put out MyView code, write the discovery forgot to release memory, oh, too lazy to change, this chapter is not the main introduction of this OH. myview.m//unity-iphone////Created by rain pine Momo on 11-11-1.//Copyright __mycompanyname__. All rights reserved.//#import "MyView.h" @implementation MyView//Implement Viewdidload to doing additional setup after    Loading the view, typically from a nib.-(void) viewdidload {[Super viewdidload];    Create a label view UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, 320, 40)];    Set the display content Label.text = @ "The program world of the Momo of rain and pine";    Set background Color Label.backgroundcolor = [Uicolor Bluecolor];    Set text color Label.textcolor = [Uicolor Whitecolor];    Set display position centered label.textalignment = Uitextalignmentcenter; Set Font Size Label.font = [Uifont fontwithname:[[uifont familynames] objECTATINDEX:10] size:20];    Create button UIButton *button0 = [UIButton buttonwithtype:1];    Set the button range Button0.frame = CGRectMake (0, 40, 100, 30);    Set button display [Button0 settitle:@ "Rectangle left rotation" forstate:uicontrolstatenormal]; Set the button to bind the response method after changing [Button0 addtarget:self action: @selector (leftbuttonpressed) forControlEvents:        UIControlEventTouchUpInside];    Create button UIButton *button1 = [UIButton buttonwithtype:1];    Set the button range Button1.frame = CGRectMake (0, 100, 100, 30);    Set button display [button1 settitle:@ "Rectangle right rotation" forstate:uicontrolstatenormal]; Set the button to bind the response method after changing [Button1 addtarget:self Action: @selector (rightbuttonpressed) forControlEvents:        UIControlEventTouchUpInside];    Create button UIButton *button2 = [UIButton buttonwithtype:1];    Set the button range Button2.frame = CGRectMake (0, 160, 100, 30);    Set button display [button2 settitle:@] Rotate "forstate:uicontrolstatenormal" on the rectangle; Set the button to bind the response method after the change [Button2 addtarget:self action: @selector (upbuttonpressed) Forcontrolevents:uicontrolevenTtouchupinside];    Create button UIButton *button3 = [UIButton buttonwithtype:1];    Set the button range Button3.frame = CGRectMake (0, 220, 100, 30);    Set button display [Button3 settitle:@] rotate "forstate:uicontrolstatenormal"; Set the button to bind the response method after changing [Button3 addtarget:self action: @selector (downbuttonpressed) forControlEvents:        UIControlEventTouchUpInside];    Add [Self.view Addsubview:label] to view;    [Self.view Addsubview:button0];    [Self.view Addsubview:button1];    [Self.view Addsubview:button2];  [Self.view Addsubview:button3];}  Left button-(void) leftbuttonpressed{unitysendmessage ("Cube", "MoveLeft", "");} Right button-(void) rightbuttonpressed{unitysendmessage ("Cube", "MoveRight", "");}  Up button-(void) upbuttonpressed{unitysendmessage ("Cube", "MoveUp", "");}  Down button-(void) downbuttonpressed{unitysendmessage ("Cube", "MoveDown", "" ");}    -(void) didreceivememorywarning {//Releases the view if it doesn ' t has a superview.      [Super didreceivememorywarning]; Release any cached daTA, images, etc. that aren ' t in use.}  -(void) viewdidunload {[Super viewdidunload];} -(void) Dealloc {[Super Dealloc];}

@end
Here I'm mainly talking about this method, which is a way for us to write the bottom of unity, meaning that the iphone sends a message to unity,



Parameter 1: The model name in the scene, Cube is a box we define.

Parameter 2: Script method Name MoveLeft is the method in the above script,

Parameter 3: For a char * type, you can pass data to unity.

Article derived from: http://www.unitymanual.com/thread-20376-1-1.html


Related Article

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.