素材巴巴 > 程序开发 >

Unity在Android平台和iOS平台获取图片转为字节流数组返回unity

程序开发 2023-09-19 21:44:42

Android中的方法

public class UnityPlayerActivity extends Activity
 {
 @Override protected void onCreate (Bundle savedInstanceState){
 }//获取logo字节流public byte[] GetLogoByte() {return readFileLogo();}public static byte[] readFileLogo() {try {Resources r = UnityPlayer.currentActivity.getResources();InputStream in = r.openRawResource(R.drawable.logo);//获取文件的字节数int lenght = in.available();//创建byte数组byte[]  buffer = new byte[lenght];//将文件中的数据读到byte数组中in.read(buffer);return buffer;} catch (Exception e) {e.printStackTrace();}return new byte[0];}}

logo图片是放在Res/drawable中

OC中方法

//获取图片转为字节流数组返回unity
 -(int)GetLogoByte:(unsigned char**) returnbytes
 {NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"Resources" ofType:@"bundle"];NSString *path = [[NSBundle bundleWithPath:resourceBundle] pathForResource:@"logo"ofType:@"png"inDirectory:@"Images"];NSData *imageData = [NSData dataWithContentsOfFile: path];if (imageData.bytes > 0) {int picturesize = (int)[imageData length];*returnbytes = (unsigned char*)[imageData bytes];return picturesize;}return 0;
 }#if defined(__cplusplus)
 extern "C"{
 #endif//获取数据转为字节流数组返回unityint _iOS_GetSDKByteData(const char* key,unsigned char** returnbytes){NSString* strFunc = [NSString stringWithUTF8String: key];NSLog(@"%@", [NSString stringWithFormat:@"ios sdk call _iOS_GetSDKByteDataFzl funcName : %@",strFunc]);if([strFunc isEqualToString:@"GetLogoByte"]){return  [GetLogoByte: returnbytes];}return 0;}#if defined(__cplusplus)
 }
 #endif
 

 

Unity中方法

/// /// 获取初始化logo字节流数据  Android/// /// public byte[] GetLogo(){
 #if UNITY_ANDROIDAndroidJavaClass   javaClass = new     AndroidJavaClass("com.unity3d.player.UnityPlayer");AndroidJavaObject   javaObject = javaClass.GetStatic("currentActivity");javaObject.Call("GetLogoByte");
 #elif UNITY_IPHONEGetSDKByteData("GetLogoByte")
 #endif}[DllImport("__Internal")]private static extern int _iOS_GetSDKByteData(string key, ref IntPtr thebytes);
 public byte[] GetSDKByteData(string key){try{IntPtr unmanagedPtr = IntPtr.Zero;int size = _iOS_GetSDKByteData(key, ref unmanagedPtr);byte[] mbyte = new byte[size];Marshal.Copy(unmanagedPtr, mbyte, 0, size);if (mbyte != null && mbyte.Length > 0){return mbyte;}else{return new byte[0];}}catch(Exception e){Debug.Log("GetSDKByteData==errr==" + e.Message.ToString());}return new byte[0];}
 

 


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。