Before writing this blog post, I also worked for a long while, for those unfamiliar with OC of children's shoes, very useful, here will mainly talk about the common communication mechanism. And some code tutorials on iOS.
The main explanation is to use the Unity3d call iOS interface to browse a picture.
1.unity3d Basic communication mechanism with IOS.
In a C # script, you define a class that writes something like this
public static class Platformnative{[dllimport ( " __internal ")] private static extern void extern_lookimage (String tempfilepath); /// <SUMMARY> /// Unity3d using the /// </SUMMARY> public static void Lookimage (String tempfilepath) {extern_lookimage (TempFilePath);
Reference using System.Runtime.InteropServices;
The Lookimage method is called directly in the Unity3d.
Write the appropriate method in Xcode, and here I'm about to describe the design module I'm testing.
Defines a helper class PlatformHelper.h, which is mainly used to handle the static method of the OC system interface, because many system interfaces can be obtained directly through the interface.
Class Platformhelper {public:static void lookimage (const char* TempFilePath); static const char* getsystemversion (int& len); };
Implement PLATFORMHELPER.MM:
voidPlatformhelper::lookimage (Const Char* filepath) {NSString * _str = [NSString Stringwithutf8string:filepath]; NSLog (@"lookimage =%@", _STR);//Create the Rootviewcontroller from a XIB file. Imageviewcontroller *viewcontroller=[[[imageviewcontroller alloc] init] autorelease]; Viewcontroller.imagepath = _str; //ADD The Rootviewcontroller view to the main window. [[Albumios shareinstance] push:viewcontroller]; }Const Char* Platformhelper::getsystemversion (int& Len) {nsstring* version = Nil; Version = [[Uidevice currentdevice] systemversion]; len = version.length;return[Version utf8string]; }
Note that this notation is written in C + + and OC mixes. The implementation file. mm is the suffix.
Then, define the following C definition in a class file as follows:
#if defined (__cplusplus) extern "C" {#endif void extern_lookimage (const char* str) { Platformhelper:: Lookimage (str); } Const char* extern_getsystemversion (int& len) { return platformhelper::getsystemversion (len); #if defined (__cplusplus)} #endif
This step is completed Unity3d and iOS communication interface, Extern_editfaceimage here and write in C # inside the same name, as for the parameter type conversion can be viewed on the Internet, not explained here.
After we've done this, Unity3d will brake the [DllImport ("__internal") function defined in C # into the project file when the iOS project is packaged, and how the associated unity is done. Note that, although it is finished, it is not directly invoked on the simulator and is placed on the real machine.
Well, here's how to focus on how iOS pops up its definition of view.
Unity Game has a window on iOS. Together, this window adds a unityviewcontroller, not a nav (full name uinavigationcontrollerdelegate). The General iOS project is to add nav to the window's child view, and the defined Viewcontroller is added to the nav view. Unity is mainly written in Unityappcontroller, this mode is not a nav wizard, and the status bar, is a full screen interface. If we want to add our own definition of viewcontroller under this window, but there is no nav, that is, not to achieve the iOS Viewcontroler jump, (note, I did not learn OC system, but more I understand). Unity provides a number of functions that can be directly taken to the class single-column variables it defines, such as Window,view,rootview.
If we want to be able to pop up our view and use it like a third-party plug-in, we should do this and try not to move the Unity3d automatically generated code.
It is also very simple to recreate a window. This window appears on the Unity window and then adds our custom view to this window as root or child view. To implement another view's jump, you can add a child view to the window as an instantiated nav, and then add the custom view to Nav to manage.
Here I use the Create window as a public encapsulation class as a single-column pattern, the view of the different functions to be implemented, to open the view of the different functions as long as the first instantiation of the Viewcontroller, and then added to a single column window up.
The structure code is as follows:
#import @interface Albumios:nsobject < uinavigationcontrollerdelegate> { Uiviewcontroller *viewcon; Uinavigationcontroller *nav; }; @property (Strong, nonatomic) UIWindow *window; + (ID) shareinstance; -(void) Push: (Uiviewcontroller *) Viewcontroller; -(void@end
Implementation class:
#import "AlbumIOS.h"Albumios *pinstance = nil;@implementationAlbumios@synthesizewindow = _window; +(ID) Shareinstance {if(!pinstance) {[[Albumios alloc] init]; }returnPinstance; } - (ID) Init {if(self = [super init]) {pinstance = self; Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; nav = [[Uinavigationcontroller alloc] initwithrootviewcontroller:nil]; Nav.toolbarhidden = YES; Nav.navigationBar.hidden = YES; [Self.window Setrootviewcontroller:nav]; [Self.window makekeyandvisible];returnSelf }returnNil } -(void) Dealloc {[Nav release]; nav = nil; [Self.window release]; Self.window = nil; Pinstance = nil; [Super Dealloc]; } -(void) Push: (uiviewcontroller*) view{if(View==nil)return; Viewcon =view; [Viewcon setmodaltransitionstyle:uimodaltransitionstylecoververtical]; [Nav Pushviewcontroller:view Animated:yes]; }-(void) Pop: (uiviewcontroller*) view{if(View==nil)return;if(View==viewcon) {[Self release]; } }@end
Note that this type of writing is different from the needs of the case, here is just a simple way to write, and at the same time only a functional module open can be so written (as a plug-in in the game is generally a one-time pop-up function module can be written in this way).
And we're doing our own custom feature view individually: Here's how to browse a picture
Definition: ImageViewController.h
#import // View Picture @interface Imageviewcontroller:uiviewcontroller { UIImage *image;} @property (Copy, nonatomic) NSString *i Magepath; @end
Implement file imageviewcontroller.mm:
#import "ImageViewController.h" #import "AlbumIOS.h"#pragmaMark-imageviewcontroller @implementation Imageviewcontroller @synthesize ImagePath; -(void) viewdidload {[Super viewdidload]; CGRect rect_screen = [[UIScreen mainscreen] bounds]; Cgsize size_screen = rect_screen.size;//Initialize display imageView uiimageview *imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 0, Size_screen . Height, size_screen.width)]; Imageview.image = [UIImage Imagewithcontentsoffile:imagepath]; Self.view.backgroundColor = [Uicolor Whitecolor]; [Self.view Addsubview:imageview]; }-(void) Dealloc {[Super Dealloc]; [ImagePath release]; ImagePath = nil; }-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event {[[Albumios shareinstance] pop:self];}-(BOOL) Pre Fersstatusbarhidden {return YES;} @end
This allows you to call the Lookimage method in C # by passing in a picture of the file directory and opening it in iOS with a new window. The Extern_getsystemversion method calls are defined in C # as well.
See the documentation for the differences between JS, Boo, and C # definitions. The truth is similar.
About ruling Unity script in iOS C # and so on is much simpler, calling directly
Unitysendmessage ("Gameobject name", "function method Name", "string parameter");
Notifies the specified gamobject of the specified function within the script.
Well, we feel useful to see it, what do not understand can also leave a message.