Hi all! I know there is a nice tool "enhancement pack
" that can do this, but I really want to make it out myself.
Now I managed to use UIImagePickerController to pick a photo from iPhone, and use a UIImage* pointer pointing it.Then I set up the extern “C” function and let Unity plugin it.
I want my unity script can get the photo, but here is my problem: how can I convert a UIImage into a Texture2D? In other words, how can I pass the pointer from Objective-C to C#? I’m confused.
I have tried several ways, but always got the “SIGBUS” error.
uiimages will not work as textures at all just like that, as they are nowhere near meeting the requirements to be used as textures (pow 2 side lengths, <= 1024x1024, …)
as for the actual transfer itself: send their byte[ ] data over and use them to generate the texture in unity
yep! That’s what i want to know: how to send their byte[ ] data? By using UIImagePNGRepresentation to convert to NSData? And then? I’m new to Objective-C.
I know there is a loadImage function in Texture2D which can load a byte[ ] type data. I tried but failed, I think I’m close to success.
I’m sorry, have to pass that one.
I invested into bliprobs kit, just because no matter how fast I am, it will in any case take far less money (as I would finish it nowhere near 4 hours) to just buy it
As for getting the byte array over from Objective C to Unity, I don’t have iPhone Advanced myself so it’s not something I’ve done. I imagine you would write a C function in XCode that returns Byte* or void* to access it.
I must admit, rather than passing the byte array back from an objective C plugin I use bliprob’s approach of resizing appropriately in objective C, stashing a PNG on the file system and passing back a flag for Unity to then load the image in from file.
I have tried the idea for a whole day only to get the “SIGSEGV” signal during runtime, here is the stacktrace:
at (wrapper managed-to-native) ImagePicker.GetImageByPicker () <0xffffffff>
at ImagePicker.GetImageByPicker () <0x0004c>
at ImagePicker.FixedUpdate () <0x00083>
at (wrapper runtime-invoke) System.Object.runtime_invoke_void__this_ (object,intptr,intptr,intptr) <0xffffffff>
Program received signal: “SIGABRT”.
warning: unrecognized length (0x0) for sigtramp context
I have tried a lot types to pass the data but always get the runtime error. Here is my current code(by passing string to C#, failed again):
Now I have get the UIImage* pointing to the photo.
Here is C# code in Unity:
[DllImport ("__Internal")]
private static extern string _GetImageByPicker ();
public string GetImageByPicker()
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
thisText.text = "call GetImage";
return _GetImageByPicker();
}
return null;
}
public Texture2D pickedTex;
public string str;
public GUIText thisText;
public byte[] data1;
void Start()
{
thisText = guiText;
pickedTex = new Texture2D(480,320);
}
void FixedUpdate () {
if (iPhoneInput.touchCount > 0 iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Began)
{
str = GetImageByPicker();
if(str!=null)
{
data1 = System.Text.Encoding.Unicode.GetBytes(str);
pickedTex.LoadImage(data1);
renderer.material.mainTexture = pickedTex;
}
}
}
}
I just comment that line, and my current code does pass string data to Unity. I convert the Byte data to UTF8String type which is the same as string in C#.
And then convert back to byte[ ] in C# using
System.Text.Encoding.Unicode.GetBytes(str);