Interaction between unity3d and IOs (1)

Source: Internet
Author: User

Interaction between unity3d and IOs (1)



Today, we will introduce the first part of interaction between unity3d and IOs: transmitting messages from IOS to unity3d. Let's start:



1.

First, use unity3d to create a plain, and adjust the camera angle and the position of the light source, as shown below:





2.

Then we create a cube, and we will use the objective-C code in IOS to control its rotation:






3.

Then we create a rotate. js script and associate it with the Cube:

var vrotate : Vector3;    //Rotate Leftfunction RotateLeft()  {print("Rotate Left");transform.Rotate(Vector3.up * Time.deltaTime * 100, Space.World);}    //Rotate Rightfunction RotateRight()  {print("Rotate Right");transform.Rotate(Vector3.down * Time.deltaTime * 100, Space.World);    }    //Rotate Upfunction RotateUp(){print("Rotate Up");transform.Rotate(Vector3.right * Time.deltaTime * 100, Space.World);    }    //Rotate Downfunction RotateDown(){print("Rotate Down");transform.Rotate(Vector3.left * Time.deltaTime * 100, Space.World);    }

The preceding four functions select cube from different angles. We will call these unity3d functions in the IOS program.




4.

Build the xcode project in unity3d and openThis project.First, create a nsobject-based class named myviewinit. we will put all our interface initialization code in it. Modify myviewinit. h as follows:

////  MyViewInit.h//  Unity-iPhone////  Created by tao hu on 9/15/12.//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import <UIKit/UIKit.h>@interface MyViewInit : NSObject+(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller;+(void)CreateTitle:(UIViewController *)controller;+(void)LeftButtonPressed;+(void)RightButtonPressed;  +(void)UpButtonPressed;+(void)DownButtonPressed;+(void)Init:(UIViewController *)controller;@end

The methods are class methods. In the init function, we have drawn all our custom interfaces (four uibutton and one uilabel are added ).




5.

Modify myviewinit. m as follows:

/// Myviewinit. M // unity-iPhone /// created by Tao Hu on 9/15/12. // copyright (c) 2012 _ mycompanyname __. all rights reserved. // # import "myviewinit. H "@ interface myviewinit () @ end @ implementation myviewinit // create button + (void) createbutton: Title rect :( cgrect) rect action :( SEL) Action controller :( uiviewcontroller *) controller {uibutton * BTN = [uibutton buttonwithtype: uibuttontyperoundedrect]; // set the button range to BTN. frame = rect; // The content displayed by the set button [BTN settitle: Title forstate: uicontrolstatenormal]; // The response method bound to the changed setting button [BTN addtarget: Self action: action forcontrolevents: uicontroleventtouchupinside]; // Add [controller. view addsubview: BTN];} // create a tag + (void) createtitle :( uiviewcontroller *) controller {// create a label view uilabel * label = [[uilabel alloc] initwithframe: cgrectmake (0, 0,320, 40)]; // sets the label of the displayed content. TEXT = @ "rotation control"; // set the background color label. backgroundcolor = [uicolor bluecolor]; // set the text color label. textcolor = [uicolor whitecolor]; // sets the center label of the display position. textalignment = uitextalignmentcenter; // set the font size label. font = [uifont fontwithname: [[uifont familynames] objectatindex: 10] size: 20]; [controller. view addsubview: Label];} // left button + (void) leftbuttonpressed {unitysendmessage ("cube", "moveleft ","");} // right button + (void) rightbuttonpressed {unitysendmessage ("cube", "moveright", "") ;}// up button + (void) upbuttonpressed {unitysendmessage ("cube", "moveup", "");} // push down button + (void) downbuttonpressed {unitysendmessage ("cube", "movedown ", "") ;}+ (void) Init :( uiviewcontroller *) controller {[self createtitle: controller]; [self createbutton: @ "Left rotate" rect: cgrectmake (0, 50,100, 40) Action: @ selector (leftbuttonpressed) controller: controller]; [self createbutton: @ "right rotation" rect: cgrectmake (0,100,100, 40) Action: @ selector (rightbuttonpressed) controller: controller]; [self createbutton: @ "rotate on" rect: cgrectmake (0,150,100, 40) Action: @ selector (upbuttonpressed) controller: controller]; [self createbutton: @ "rotate down" rect: cgrectmake (0,200,100, 40) Action: @ selector (downbuttonpressed) controller: controller] ;}@ end

Where:

The unitysendmessage function has three parameters:

The first is the model name in scene, the second is a function bound to the model, and the third is a char * parameter, which is used to pass the parameter to this function.


We can use this method to call any function on any model in unity3d.

6.

The last thing we need to do is call the above init function when the program starts. Find the appcontroller. MM file:

int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight,  int* openglesVersion)

The function is called when the unity3d program is started. The eaglview is the view in the background of unity3d. We call our init function in this function. Modify openeagl_unitycallback as follows:

int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight,  int* openglesVersion){CGRect rect = [[UIScreen mainScreen] bounds];// Create a full-screen window_window = [[UIWindow alloc] initWithFrame:rect];        EAGLView* view = [[EAGLView alloc] initWithFrame:rect];UnityViewController *controller = [[UnityViewController alloc] init];        sGLViewController = controller;#if defined(__IPHONE_3_0)if( _ios30orNewer )controller.wantsFullScreenLayout = TRUE;#endifcontroller.view = view;[_window addSubview:view];        [MyViewInit init:controller];if( !UnityUseOSAutorotation() ){_autorotEnableHandling = true;[[NSNotificationCenter defaultCenter] postNotificationName: UIDeviceOrientationDidChangeNotification object: [UIDevice currentDevice]];}......

In fact, we only added one sentence:

[MyViewInit init:controller];

Add the following content to the file header:

#import "MyViewInit.h"



7.

Now, we have to run it on the real machine:

Initial interface:





Click the rotate button below:




Rotate the Cube:




We found that the pirnt statement in unity3d is output in the xcode Console window. Therefore, the print function in unity3d is output to the xcode console when running on a real machine.





The final code is too big to be uploaded. If you need it, contact me or leave a message below.





I have referred to Yu Song Momo's article and would like to express my gratitude for the following:

Http://blog.csdn.net/xys289187120/article/details/6926746





Done!

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.