unity调用ios的参数传入与返回
来源:互联网 发布:淘宝卖家开通直播入口 编辑:程序博客网 时间:2024/06/06 17:46
这个比较简单,注意一下string类型的返回就好了。
不说了直接上代码:
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
阅读全文
0 0
- unity调用ios的参数传入与返回
- MyBatis的传入和返回参数类型
- c++函数在传入对象参数和返回对象时候的内存调用情况
- Unity与iOS间的调用
- struts2 include标签传入参数的调用
- python调用dll文件时传入参数是int*,返回值是char*,对传入的char*所指的内容的修改
- .NET多线程 传入参数与获取返回值
- dwr 调用函数参数传入与接收配置
- 如果函数的传入参数和返回值是对象
- js调用函数时传入的参数个数与函数定义时的参数个数不符时的操作
- 编写方法,传入参数为指向Node结构的指针,返回传入数据结构的完整拷贝。
- iOS传入颜色返回图片
- Java调用Oracle存储过程传入数组参数的方法
- 函数调用时参数传入的几种情况
- C++ 调用 python 传入参数的编码问题
- 使用ShellExecuteEx调用控制台程序或命令行,传入多个参数,并等待返回结果
- Flex通过RemoteObject调用java实例:flex传入参数以及接收java返回结果
- mybatis调用orcale存储过程-传入多个参数返回单个字符
- Exception in thread "main" java.io.FileNotFoundException: E:\java\.metadata (拒绝访问。)的理解
- 1050. 螺旋矩阵(25)
- HDU 6121 树的节点计数
- JS-DOM(二)_操作节点
- 解决tomcat 8080端口被占用问题
- unity调用ios的参数传入与返回
- bzoj 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+最大流
- 点击查看大图(长按保存图片)
- 【NOI2016】优秀的拆分
- 解决activity、service间使用sharePreference进行值存取不同步
- HDU 6129 Just do it
- [RK3288][Android7.1.2] Launcher3 源码阅读之step2:详解Launcher的OnCreate方法的A部分
- 编写一个Windows窗口应用程序
- jquery验证表单不为空