unity调用ios的参数传入与返回

发表于2017-09-22
评论0 1.6k浏览

这个比较简单,注意一下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个评论