unity调用ios的参数传入与返回
2017-08-16 10:49
411 查看
这个比较简单,注意一下string类型的返回就好了。
不说了直接上代码:
1、oc
2、c#
3、c#测试
注:unity版本 5.6
不说了直接上代码:
1、oc
#if defined (__cplusplus) extern "C" { #endif BOOL u3d_getBool1(BOOL _input) { NSLog(@"_input value:%@",_input?@"Yes":@"No"); return true; } BOOL u3d_getBool2(BOOL _input) { NSLog(@"_input value:%@",_input?@"Yes":@"No"); return false; } int u3d_getInt(int _input) { return _input + 100; } long u3d_getLong(long _input) { return _input + 123l; } float u3d_getFloat(float _input) { return _input + 0.123f; } double u3d_getDouble(double _input) { return _input + 0.123456l; } char * u3d_getString(const char * _input) { NSString *str = [NSString stringWithUTF8String:_input]; NSString *newstr = [NSString stringWithFormat:@"%@--%@", str, @"_fromOC"]; const char * _output = [newstr UTF8String]; NSLog(@"%@, %@, %s",str,newstr,_output); // 是里如果直接返回_output是会报错的,因为它是是const,不能被返回。 char* res = (char*)malloc(strlen(_output)+1); strcpy(res, _output); return res; } // 封装json void u3d_packageJosn() { // 构造一个nsdictionary NSDictionary *dict = @{ @"name":@"小海", @"age":@102, @"weight":@56.5f, @"height":@165.555f, @"nice":@YES }; // 判断dict是否可转成json BOOL isYes = [NSJSONSerialization isValidJSONObject:dict]; NSLog(@"u3d_packageJosn isYes : %@",isYes?@"Yes":@"No"); // nsdictionary --> nsdata NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil]; NSLog(@"u3d_packageJosn data: %@", data); // nsdata -> nsstring NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"u3d_packageJosn jsonString: %@", jsonString); // nsstring -> const char* const char* constStr = [jsonString UTF8String]; // 调用unity3d中的方法 UnitySendMessage("Main Camera", "CallbackFormIOS_1", constStr); } // 解析json void u3d_parseJson(const char * jsonStr) { // const char* --> nnstring NSString *jsonString = [NSString stringWithUTF8String:jsonStr]; NSLog(@"u3d_parseJson jsonString: %@", jsonString); // nsstring -> nsdata NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"u3d_parseJson data: %@", data); // nsdata -> nsdictionary NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSLog(@"u3d_parseJson dict: %@",dict); NSLog(@"dict - name: %@",dict[@"name"]); NSLog(@"dict - age: %@",dict[@"age"]); NSLog(@"dict - weight: %@",dict[@"weight"]); NSLog(@"dict - height: %@",dict[@"height"]); NSLog(@"dict - nice: %@",dict[@"nice"]); } #if defined (__cplusplus) } #endif
2、c#
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.InteropServices; public class IOSLibraryBridge { #if UNITY_IOS || UNITY_IPHONE [DllImport("__Internal")] private static extern bool u3d_getBool1 (bool _input); [DllImport("__Internal")] private static extern bool u3d_getBool2 (bool _input); [DllImport("__Internal")] private static extern int u3d_getInt (int _input); [DllImport("__Internal")] private static extern long u3d_getLong (long _input); [DllImport("__Internal")] private static extern float u3d_getFloat (float _input); [DllImport("__Internal")] private static extern double u3d_getDouble (double _input); [DllImport("__Internal")] private static extern string u3d_getString (string _input); [DllImport("__Internal")] private static extern void u3d_packageJosn (); [DllImport("__Internal")] private static extern void u3d_parseJson (string _input); #endif public static bool getBool1 (bool _input) { #if UNITY_IOS || UNITY_IPHONE return u3d_getBool1(_input); #else return false; #endif } public static bool getBool2 (bool _input) { #if UNITY_IOS || UNITY_IPHONE return u3d_getBool2(_input); #else return false; #endif } public static int getInt (int _input) { #if UNITY_IOS || UNITY_IPHONE return u3d_getInt(_input); #else return 0; #endif } public static long getLong (long _input) { #if UNITY_IOS || UNITY_IPHONE return u3d_getLong(_input); #else return 0L; #endif } public static float getFloat (float _input) { #if UNITY_IOS || UNITY_IPHONE return u3d_getFloat(_input); #else return 0f; #endif } public static double getDouble (double _input) { #if UNITY_IOS || UNITY_IPHONE return u3d_getDouble(_input); #else return 0d; #endif } public static string getString (string _input) { #if UNITY_IOS || UNITY_IPHONE return u3d_getString(_input); #else return "--empty--"; #endif } public static void packageJson () { #if UNITY_IOS || UNITY_IPHONE u3d_packageJosn(); #endif } public static void parseJson (string _input) { #if UNITY_IOS || UNITY_IPHONE u3d_parseJson(_input); #endif } }
3、c#测试
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test : MonoBehaviour { // Use this for initialization void Start () { bool b1 = IOSLibraryBridge.getBool1 (true); Debug.Log ("b1:" + b1); bool b2 = IOSLibraryBridge.getBool2 (false); Debug.Log ("b2:" + b2); int int1 = IOSLibraryBridge.getInt (10); Debug.Log ("int1:" + int1); long long1 = IOSLibraryBridge.getLong (10L); Debug.Log ("long1:" + long1); float float1 = IOSLibraryBridge.getFloat (10f); Debug.Log ("float1:" + float1); double double1 = IOSLibraryBridge.getDouble (10d); Debug.Log ("double1:" + double1); string string1 = IOSLibraryBridge.getString ("hello"); IOSLibraryBridge.packageJson (); string json = "{\"age\":102,\"weight\":65.5,\"name\":\"阿海\",\"height\":175.555,\"nice\":true}"; IOSLibraryBridge.parseJson (json); } void CallbackFormIOS_1(string jsonStr) { Debug.Log("CallbackFormIOS_1: " + jsonStr); LitJson.JsonData jd = LitJson.JsonMapper.ToObject (jsonStr); if (jd != null) { string name = jd ["name"].ToString (); int age = int.Parse(jd ["age"].ToString()); float weight = float.Parse(jd ["weight"].ToString()); float height = float.Parse(jd ["height"].ToString()); bool nice = bool.Parse(jd ["nice"].ToString()); Debug.Log(string.Format("name={0},age={1},weight={2},height={3},nice={4},",name,age,weight,height,nice)); } } }
注:unity版本 5.6
相关文章推荐
- Flex通过RemoteObject调用java实例:flex传入参数以及接收java返回结果
- mybatis调用orcale存储过程-传入多个参数返回单个字符
- mybatis调用orcale存储过程-传入多个参数返回结果集
- c++函数在传入对象参数和返回对象时候的内存调用情况
- POST形式 soapUI调用WebService的restful接口,传入json参数,并且返回json
- 使用ShellExecuteEx调用控制台程序或命令行,传入多个参数,并等待返回结果
- Unity使用C#调用C++dll传递指针参数、接收指针返回值
- unity调用系统相册无法横屏解决方法(ios)
- myBatis 传入的参数是对的,但是总是没有返回结果
- Unity在Android和iOS中如何调用Native API
- 请问Delphi调用VC写的dll,有传入和传出参数都是unsigned char*运行中有有关问题
- JS 定义函数的参数的个数,传入参数的个数对调用的影响
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- 显示调用的程序的返回参数和执行时间 cb_console_runner.exe
- 函数调用时参数传入的几种情况
- 通过双击ALV中的字段名判断调用的事物代码和传入的参数
- java调用oracle存储过程传入参数为数组的处理
- 封装的一个可以传入URL参数字典返回完整版URL
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- MyCat1.6带返回参数存储过程调用示例