Unity和IOS交互

| 发布     | 分类 Unity  | 标签 Unity  IOS 

实例一: Unity调用IOS里的方法

实例二: IOS回调Unity方法

实例三: Unity调用IOS方法并返回结果


  1. C#与C交互
  2. C与ObjectC交互
  1. 新建一个空的Unity项目
  2. 创建目录 Assets/Plugins/iOS

1. 在Assets/Plugins/iOS目录创建两个文件: Test.h, Test.m

2. 在Assets目录创建两个文件: Unity2iOS.cs, TestUnity2iOS.cs



@interface Test
void _receive(char* str1, char* str2);
@end



#import "Test.h"

@interface Test ()

@end

@implementation Test


void _receive(char* s1, char* s2)
{
    NSLog([NSString stringWithUTF8String:s1] );
}

@end



using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public static class Unity2iOS 
{
    [DllImport("__Internal")]
    private static extern void  _receive(string s1, string s2);

    public static void Receive(string s1, string s2)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            _receive(s1, s2);
        }
    }
}

using UnityEngine;
using System.Collections;

public class TestUnity2iOS : MonoBehaviour 
{
    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 200, 50), "Send"))
        {
            Unity2iOS.Receive("Hello", "Ihaiu");
        }
    }
}



void UnitySendMessage(const char* obj, const char* method, const char* msg);

这方法在发布后的xcode项目 Classes/Unity/UnityInterface.h里。用来调用Unity里的方法。

obj: 为场景中GameObject名称

method: 挂载在该GameObject里其中MonoBehaviour的一个方法名称

msg: 传递的参数



@interface Test
void _receive(char* str1, char* str2);
@end



#import "Test.h"

@interface Test ()

@end

@implementation Test


void _receive(char* s1, char* s2)
{
    NSLog([NSString stringWithUTF8String:s1] );
    UnitySendMessage("Main Camera", "OnMsg", s2);
}

@end



using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public static class Unity2iOS 
{
    [DllImport("__Internal")]
    private static extern void  _receive(string s1, string s2);

    public static void Receive(string s1, string s2)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            _receive(s1, s2);
        }
    }
}

using UnityEngine;
using System.Collections;

public class TestUnity2iOS : MonoBehaviour 
{
    private string msg;
    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 200, 50), "Send"))
        {
            Unity2iOS.Receive("Hello", "Ihaiu");
        }


        if (!string.IsNullOrEmpty(msg))
        {
            GUI.Label(new Rect(0, 50, 200, 50), msg);
        }
    }


    public void OnMsg(string msg)
    {
        this.msg = msg;
    }
}


@interface Test
int _add(int a, int b);
char* _callback(char* s);
@end



#import "Test.h"

@interface Test ()

@end

@implementation Test

int _add(int a, int b)
{
    int result = a + b;
    NSLog(@"%d + %d = %d", a, b, result);
    return result;
}

char* _callback(char* s)
{
    return MakeStringCopy(s);
}

/** 拷贝字符串, 为了不内存泄漏 */
char* MakeStringCopy(const char* str)
{
    if (str == NULL) {
        return NULL;
    }
    
    char* res = malloc(strlen(str) + 1);
    strcpy(res, str);
    return res;
}

@end



using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public static class Unity2iOS 
{

    [DllImport("__Internal")]
    private static extern int _add(int a, int b);

    public static int Add(int a, int b)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            return _add(a, b);
        }
        return -1;
    }



    [DllImport("__Internal")]
    private static extern string _callback(string msg);

    public static string Callback(string msg)
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            return _callback(msg);
        }
        return null;
    }


}

using UnityEngine;
using System.Collections;

public class TestUnity2iOS : MonoBehaviour 
{
    private int addResult = -1;
    private string callResult;

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 200, 200, 50), "10 + 2 = "))
        {
            addResult = Unity2iOS.Add(10, 2);
        }

        if (addResult != -1)
        {
            GUI.Label(new Rect(0, 250, 200, 50), "add result=" + addResult);
        }


        if (GUI.Button(new Rect(0, 300, 200, 50), "callback "))
        {
            callResult = Unity2iOS.Callback("http://blog.ihaiu.com");
        }

        if (!string.IsNullOrEmpty(callResult))
        {
            GUI.Label(new Rect(0, 350, 200, 50), "call result=" + callResult);
        }
    }

}

上一篇: Unity 视频相册
下一篇: xcode 配置参数笔记