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];
}