Unity3d ios plug-in development
Unity3D is a very powerful cross-platform game engine, but it still inevitably needs to access some features of the platform. Unity3D does not create APIs for all aspects of the Platform, especially new functions. At this time, we need to write our own local plug-ins to solve the problem. This article mainly introduces how to develop the Unity3D iOS local album plug-in.GlobalBrowser(You can automatically scan the Documents directory and use the photo wall for display. The display function uses an open-source control of Objective-C.MWPhotoBrowser).
Preparations
This article uses Unity 5 and Xcode 6.2 for development. Currently, only Unity 4.6 and Unity 5 support arm64, and only Unity 5 supports using subdirectories in the plug-in. There are three ways to use Objective-C code: source code, static library (. a), and framework (iOS 8). This time we chose pure source code.
Write local code
1. Create an iOS ProjectPhotoBrowserCreate a Library folder in the project directory.
2. Copy MWPhotoBrowser and other open source code used to the Library and add it to the Xcode project.
3. CreateGlobalBrowserDirectory, and then createDVIGlobalBrowserClass. We implement the local code of the Image Browsing plug-in this class. For the sake of simplicity, we only implement several Class methods, and then use a static variable to save the object.
# Import
@ Interface DVIGlobalBrowser: NSObject + (void) show; + (void) dismiss; @ end // implementation code /////////////////////// /# import DVIGlobalBrowser. h # import
# Import MWPhotoBrowser. h # import MWPhoto. hstatic DVIGlobalBrowser * sharedInstance = nil; @ interface DVIGlobalBrowser ()
{NSArray * _ photosArray; NSString * _ photoDir;} @ property (nonatomic, strong) MWPhotoBrowser * photoBrowser; @ end @ implementation DVIGlobalBrowser + (void) initialize {sharedInstance = [[DVIGlobalBrowser alloc] init]; sharedInstance. photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate: sharedInstance]; sharedInstance. photoBrowser. displayActionButton = YES; sharedInstance. photoBrowser. displayNavArrows = YES; sharedInstance. photoBrowser. displaySelectionButtons = NO; sharedInstance. photoBrowser. alwaysShowControls = NO; sharedInstance. photoBrowser. zoomPhotosToFill = YES; # if _ IPHONE_ OS _VERSION_MIN_REQUIRED <_ IPHONE_7_0 sharedInstance. photoBrowser. wantsFullScreenLayout = YES; # endif sharedInstance. photoBrowser. enableGrid = YES; sharedInstance. photoBrowser. startOnGrid = YES; sharedInstance. photoBrowser. enableSwipeToDismiss = YES; // [sharedInstance. photoBrowser setCurrentPhotoIndex: 0];} + (void) show {[sharedInstance loadPhotos]; UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController: sharedInstance. photoBrowser]; nc. modalTransitionStyle = UIModalTransitionStyleCrossDissolve; UIWindow * appWin = [UIApplication sharedApplication]. keyWindow; [appWin. rootViewController presentViewController: nc animated: YES completion: nil]; [sharedInstance. photoBrowser reloadData];} + (void) dismiss {[sharedInstance. photoBrowser dismissViewControllerAnimated: YES completion: nil];}-(void) loadPhotos {if (_ photoDir = nil) {_ photoDir = [NSHomeDirectory () stringByAppendingPathComponent: @ Documents];} NSArray * array = [[NSFileManager ultultmanager] subpathsAtPath: _ photoDir]; _ photosArray = array;}-(NSUInteger) numberOfPhotosInPhotoBrowser :( MWPhotoBrowser *) photoBrowser {return _ photosArray. count;}-(id
) PhotoBrowser :( MWPhotoBrowser *) photoBrowser photoAtIndex :( NSUInteger) index {NSString * filename = _ photosArray [index]; NSString * path = [_ photoDir stringByAppendingPathComponent: filename]; MWPhoto * photo = [MWPhoto photoWithURL: [NSURL fileURLWithPath: path]; return photo;}-(id
) PhotoBrowser :( MWPhotoBrowser *) photoBrowser thumbPhotoAtIndex :( NSUInteger) index {NSString * filename = _ photosArray [index]; NSString * path = [_ photoDir stringByAppendingPathComponent: filename]; MWPhoto * photo = [MWPhoto photoWithURL: [NSURL fileURLWithPath: path]; return photo ;}@ end
4. Since Unity3D only supports C/C ++ functions, we need to further encapsulate the above Code and useextern C
Export necessary functions.
//. Extern C void showPhotoBrowser (void) in the H file );//. void showPhotoBrowser (void) {[DVIGlobalBrowser show];} void dismissPhotoBrowser (void) {[DVIGlobalBrowser dismiss];}
5. Test in iOS ProjectDVIGlobalBrowser
.
Interface code writing
Calling Objective-C code in Unity3d requires compiling the C # interface. Create the C # interface file PhotoBrowser. cs in the GlobalBrowser folder.
Public class PhotoBrowser {// introduce the function [DllImport (_ Internal)] private static extern void showPhotoBrowser (); // The public static void showPhotoBrowserEx () function exposed to C () {// platform determines if (Application. platform = RuntimePlatform. IPhonePlayer) {showPhotoBrowser ();}}}
Test Item
Add a button to an Unity3d scenario. If the screenshot function is not used, pay attention to the storage path.
Using UnityEngine; using System. collections; public class MyFile: MonoBehaviour {// Use this for initialization void Start () {// print (Start ...);} // Update is called once per frame void Update () {// print (Update ...);} void OnGUI () {// print (onGUI ...); // create button to display the album if (GUI. button (new Rect (100,100,100,100), Button) {PhotoBrowser. showPhotoBrowserEx ();} // create screenshot button if (GUI. button (new Rect (100,220,100,100), Save) {var savePath = Application. persistentDataPath ++ (Random. value * 100) + image.png; print (savePath + <---> Capture); // screenshot and save the image to the Documents Directory Application. captureScreenshot (Random. value * 100) + image.png );}}}