unity3d ---IOS--Android 通讯

   跨平台方面 unity3d虽然已经的够好了,但是针对不同的平台还是有一些原生的接口需要用个平台自己的语言去编写的,

   接sdk的时候就会遇到这样的情况。

   今天简单写写 ios 和android 平台的交互方法,注意:  需要真机调试,模拟器什么的必定报错。

 

  •    IOS平台

    首先在xcode中新建两类 U3dPlugin.h  U3dPlugin.m

    U3dPlugin.h  代码如下

//
//  U3dPlugin.h
//  Unity-iPhone
//
//  Created by SHI  In 14-11-14.
//
//

#import <Foundation/Foundation.h>

@interface U3dPlugin : NSObject

@end

    U3dPlugin.m 代码如下:

//
//  U3dPlugin.m
//  Unity-iPhone
//
//  Created by Shi on 14-11-13.
//
//

#import "U3dPlugin.h"

//===================导入unity sendmessage 方法 向u3d发送消息必须===================
#if defined(__cplusplus)
extern "C"{
#endif
    extern void UnitySendMessage(const char *, const char *, const char *);
    extern NSString* _CreateNSString (const char* string);
#if defined(__cplusplus)
}
#endif

@implementation U3dPlugin
//unity 中要调用的方法,
void helloU3d(const char* id,const char* name){
    NSLog(@"xcode________%c", name);
    //回掉u3d中的方法 三个参数 分别是 场景中的物体, 回掉方法名称 ,方法参数
    UnitySendMessage("Cube","helloCallBack","i'm xcode");
}

@end

    接下来在unity 中新建一个工程,向场景中添加一个Cube ,或者别的什么物体 ,更改名称为Cube

    注意:Cube  是 ios向u3d发送消息是接受消息的对象名称。 也就是上面代码中的第一个参数

    UnitySendMessage("Cube","helloCallBack","i'm xcode");

    之后在你的unity工程下创建一个类 hellowXcode.cs  挂载到Cube上。

            写入一下代码:

using UnityEngine;
using System.Collections;
// 导入dll库需要的引用,必须
using System.Runtime.InteropServices;
public class testxcode : MonoBehaviour {
   #if UNITY_IPHONE&&!UNITY_EDITOR
    //dll导入,helloU3doc 中将要调用的方法
    [DllImport("__Internal")]
    private static extern void helloU3d (string id,string name);
    // Use this for initialization
    void Start () {
        helloXcode ();
    }
    /// <summary>
    /// Hellos the xcode.
    /// </summary>
    public static void helloXcode(){
        if (Application.platform==RuntimePlatform.IPhonePlayer) {
            print("u3d++++++++++ helloXcode");
            helloU3d("111","i'm U3d");
        }
    }
    /// <summary>
    /// helloXcode 回调函数
    /// </summary>
    /// <param name="name">Name.</param>
    public void helloCallBack(string name){
        Debug.Log ("helloCallBack"+name);
    }  
#endif }

 

导入dll 添加引用。
using System.Runtime.InteropServices;

在Assets目录下,新建目录Plugins/IOS
   将刚才创建的 U3dPlugin.h  U3dPlugin.m 放到目录下
为啥要放这里呢,这个文件夹下的内容在导出xcode工程后,会编译到Libraries 目录下。
   好了 打开build settings 界面如图


    Bundle Identifier* 输入你id 要与你申请的 Provisioning profile 文件的名称对应。
    sdk version 选择 device SDK
    勾选development build 和 script debugging 可以在xcode 的控制台看到更多的日志信息,方便bug 查找

好了 点击build 导出xcode 工程
    运行正常可以在xcode 的控制台看 log输出:
    1.u3d++++++++++ helloXcode

    2.xcode________i'm U3d
       3.helloCallBacki'm xcode
  •    android平台

 

    android 还是上面的工程 添加文件:helloeclipse.cs  

    

using UnityEngine;
using System.Collections;
using System;

public class helloweclipse : MonoBehaviour
{
    private string info = "11";
    public static helloweclipse instence;
    void Start() {
        instence = this;
    }
#if UNITY_ANDROID 
    private static AndroidJavaClass jc_U3dPlugin;//类名
    const string SDK_JAVA_CLASS = "com.forzen.test.U3dPlugin"; //包名
    public static void AndroidPlugin(string funcName, object[] obj)
    {

        Debug.Log(funcName);
        if (jc_U3dPlugin == null)
        {
            jc_U3dPlugin = new AndroidJavaClass(SDK_JAVA_CLASS);
        }
        jc_U3dPlugin.CallStatic(funcName, obj);

    }
    void OnGUI() {
        GUI.Label(new Rect(50, 200, 200, 50), info);
        if (GUI.Button(new Rect(50, 250, 200, 30), "Button1"))
        {
            helloeclipses();
        }


    }

    public static void helloeclipses()
    {

        String[] strArry = new String[2];

        strArry[0] = "id";
        strArry[1] = "name";
        AndroidPlugin("helloeclipses", strArry);
    }
    public void helloeclipsesCallBack(string name)
    {
        info = name;
        UnityEngine.Debug.LogError("helloeclipses" + name);
    }

#endif
}

 

     导出eclipse 工程

   之后在eclipse中导入android 工程  (工程可直接打开运行)

    创建类 com.hello.U3dPlugin.java

         

package com.forzen.test;
import android.util.Log;

import com.unity3d.player.UnityPlayer; //导入 u3d 发送消息的 包
public class U3dPlugin {
    public static void helloeclipses(final String id ,final String name){
        Log.e("U3dPlugin","hello:" +name);
        UnityPlayer.UnitySendMessage("Cube", "helloeclipsesCallBack", "i'm eclipse");
    }
}

 

    运行工程 可在 LogCat 中看到 log输出。

 

    软件版本 :u3d版本 4.5.5f ,xcode5.1,  eclipse: Android Developer Tools 

至此结束。

posted @ 2014-11-14 22:07  冰封之子  阅读(923)  评论(0)    收藏  举报