Skip to content

Instantly share code, notes, and snippets.

@naojitaniguchi
Last active September 15, 2020 10:17
Show Gist options
  • Select an option

  • Save naojitaniguchi/7d71267814ccd0ca719e to your computer and use it in GitHub Desktop.

Select an option

Save naojitaniguchi/7d71267814ccd0ca719e to your computer and use it in GitHub Desktop.
Video Picker for Unity iOS native plugin call sample, Using UIImagePickerController
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class VideoPicker : MonoBehaviour {
public Texture2D shareButtonImage; // Use this for initialization
[DllImport("__Internal")]
private static extern void OpenVideoPicker(string game_object_name, string function_name);
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI ()
{
if (GUILayout.Button (shareButtonImage, GUIStyle.none, GUILayout.Width (128), GUILayout.Height (128))) {
OpenVideoPicker( "GameObject", "VideoPicked" );
}
}
void VideoPicked( string path ){
Debug.Log ("---->VideoPicked");
Debug.Log( path );
}
}
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#if UNITY_VERSION <= 434
#import "iPhone_View.h"
#endif
char video_url_path[1024];
@interface NonRotatingUIImagePickerController : UIImagePickerController
@end
@implementation NonRotatingUIImagePickerController
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
@end
//-----------------------------------------------------------------
@interface APLViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
UIImagePickerController *imagePickerController;
@public
const char *callback_game_object_name ;
const char *callback_function_name ;
}
@end
@implementation APLViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,nil];
imagePickerController.delegate = self;
[self.view addSubview:imagePickerController.view];
}
#pragma mark - UIImagePickerControllerDelegate
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//NSLog(@"type=%@",type);
if ([type isEqualToString:(NSString *)kUTTypeVideo] ||
[type isEqualToString:(NSString *)kUTTypeMovie])
{// movie != video
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"%@", urlvideo);
NSString *urlString = [urlvideo absoluteString];
const char* cp = [urlString UTF8String];
strcpy(video_url_path, cp);
}
[self dismissViewControllerAnimated:YES completion:NULL];
// UnitySendMessage("GameObject", "VideoPicked", video_url_path);
UnitySendMessage(callback_game_object_name, callback_function_name, video_url_path);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
@end
extern "C" {
void OpenVideoPicker(const char *game_object_name, const char *function_name) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
// APLViewController
UIViewController* parent = UnityGetGLViewController();
APLViewController *uvc = [[APLViewController alloc] init];
uvc->callback_game_object_name = strdup(game_object_name) ;
uvc->callback_function_name = strdup(function_name) ;
[parent presentViewController:uvc animated:YES completion:nil];
}
}
}
@dlazares
Copy link
Copy Markdown

What does the header file look like for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment