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

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

   //获取数据转为字节流数组返回unity
    int _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中方法

/// <summary>
    /// 获取初始化logo字节流数据  Android
    /// </summary>
    /// <returns></returns>
    public byte[] GetLogo()
    {
#if UNITY_ANDROID
       AndroidJavaClass   javaClass = new     AndroidJavaClass("com.unity3d.player.UnityPlayer");
             AndroidJavaObject   javaObject = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
            javaObject.Call("GetLogoByte");
#elif UNITY_IPHONE
        GetSDKByteData("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];
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值