unity3d 与 iOS 的交互

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 Vector3	vrotate;
	
	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高级界面消息(十)


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值