unity3d 与 iOS 的交互

来源:互联网 发布:mac系统打不开flash 编辑:程序博客网 时间:2024/06/01 09:09

unity3d 通过 编辑器实现代码。资源的整合以后。

编译成ios时候,会把脚本部分翻译成arm指令。资源部分整合进去。

然后通过ios的OPENGL代码实现。


大致看一下ios的代码。unity3d从新实现了arm到C的过程。脚本代码从内存中重定向执行。

但是既然是ios本地执行。调用到ios的window和view。就可以绑定ios代码和界面。



iOS 导出代码到 unity3d使用。

using UnityEngine;using System.Runtime.InteropServices;public class IOSUnityCommon{          //导出按钮以后将在xcode项目中生成这个按钮的注册,     //这样就可以在xocde代码中实现这个按钮点击后的事件。     [DllImport("__Internal")]     private static extern void _PressButton ();          public static void IOSButton ()     {                if (Application.platform != RuntimePlatform.OSXEditor)         {            //点击按钮后调用xcode中的 _PressButton ()方法,            _PressButton ();        }     }}


其他脚本中调用

IOSUnityCommon.IOSButton0();


在iOS中实现

void _PressButton(){    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"IOS导出方法"                                                    message:@"hello unity3d"                                                   delegate:nil                                          cancelButtonTitle:@"确认"                                          otherButtonTitles:nil, nil];    [alert  show];    [alert release];}


iOS 控制 unity3d中的代码。

unity3d保留了1个接口

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

官方给出的指导

This function has three parameters : the name of the target GameObject, the script method to call on that object and the message string to pass to the called method.Known limitations:Only script methods that correspond to the following signature can be called from native code: function MethodName(message:string)Calls to UnitySendMessage are asynchronous and have a delay of one frame.

脚本

参数1:游戏对象

参数2:对象脚本中的方法

参数3:String类型的消息

unity中代码

using UnityEngine;using System.Collections;public class Move2 : MonoBehaviour {public Vector3vrotate;private void MoveFunction( Vector3 vFRotate ) {float rotate = Time.deltaTime * 100;vrotate = vFRotate * rotate;transform.Rotate ( vrotate , Space.World );}public void MoveLeft( string message ){Debug.Log ("ios param :" + message );MoveFunction ( Vector3.up );}public void MoveRight( string message ){Debug.Log ("ios param :" + message );MoveFunction( Vector3.down );}public void MoveUp( string message ) {Debug.Log ("ios param :" + message );MoveFunction( Vector3.right );}public void MoveDown( string message ){Debug.Log ("ios param :" + message );MoveFunction( Vector3.left );}}


绑定到1个测试Cube上

ios中代码

//向左按钮-(void)LeftButtonPressed{    UnitySendMessage("Cube","MoveLeft","hello, unity3d1");}//向右按钮-(void)RightButtonPressed{    UnitySendMessage("Cube","MoveRight","hello, unity3d2");}//向上按钮-(void)UpButtonPressed{    UnitySendMessage("Cube","MoveUp","hello, unity3d3");}//向下按钮-(void)DownButtonPressed{    UnitySendMessage("Cube","MoveDown","hello, unity3d4");}


参考

unity3d 官方 指导 Building Plugins for iOS

系列教程:Unity3D For iPhone游戏引擎之iOS高级界面发送消息与Unity3D消息的接收(九)

系列教程:Unity3D For iPhone游戏引擎之Unity3D回馈iOS高级界面消息(十)