zoukankan      html  css  js  c++  java
  • Unity3D与iOS消息交互方法(1)--iOS接收Unity3D发出的消息

    跨平台这种事情不管多NB, 总要有些与原生系统交互的方法, 比如  Unity3D与iOS消息交互方法.

    一: 建立一个空的Unity工程.

      File -->  New Project

    二: 编写脚本文件 (Test.cs)

         在Project选项卡中, create ->C# script, 命名为Test, 双击, 编写此cs文件.

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class Main : MonoBehaviour {
     5 
     6 //声明两个Texture变量,图片资源在外面连线赋值
     7 public Texture Button0;
     8 public Texture Button1;
     9 
    10     // Use this for initialization
    11     void Start () {
    12     
    13     }
    14     
    15     // Update is called once per frame
    16     void Update () {
    17     
    18     }
    19     
    20     //这个方法用于绘制
    21     void OnGUI() {
    22         //绘制两个按钮
    23         if(GUI.Button(new Rect(0,44,120,120),Button0))
    24         {
    25             //返回值为ture说明这个按钮被点击
    26             SDK.ActivateButton0();
    27         }    
    28         
    29         //绘制两个按钮
    30         if(GUI.Button(new Rect(200,44,120,120),Button1))
    31         {
    32             //返回值为ture说明这个按钮被点击
    33             SDK.ActivateButton1();
    34         }    
    35     }
    36 }

    同样的方法, 再生成一个脚本文件. SDK.cs

    using UnityEngine;
    using System.Runtime.InteropServices;
    
    public class SDK
    {
         
         //导出按钮以后将在xcode项目中生成这个按钮的注册,
         //这样就可以在xocde代码中实现这个按钮点击后的事件。
         [DllImport("__Internal")]
         private static extern void _PressButton0 ();
         
         public static void ActivateButton0 ()
         {
             
            if (Application.platform != RuntimePlatform.OSXEditor) 
            {
                //点击按钮后调用xcode中的 _PressButton0 ()方法,
                //方法中的内容须要我们自己来添加
                _PressButton0 ();
            }
         }
         
         //和上面一样
         [DllImport("__Internal")]
         private static extern void _PressButton1 ();
         
         public static void ActivateButton1 ()
         {
             if (Application.platform != RuntimePlatform.OSXEditor) 
            {
                _PressButton1 ();
            }
         }
    
    }

    三:绑定相关脚本到Main Camera

      只拖动Test.cs文件到"Main Camera"上, 成功后, 可以在game view中看到效果图, 或者在Main Camera的Inspector中也能看到如: Test(Script)

    ps: 在"Project"中添加图片文件作用button的图片, 也是用拖动的方法(选中Test.cs, 在Instpector中可以看到 Button0和1处无图片, 这时拖动图片文件到Button0和1上)

    四: 导出iOS工程

      Unity3D中的工作此时已经完成, 可以导出iOS工程了

      File--> Build&Run

    **:Unity3D中界面如下:

     

    五: 在iOS工程中添加代码

    添加两个文件 , 以接收Unity3D发来的消息

     1 //
     2 //  MyView.h
     3 //  Unity-iPhone
     4 //
     5 //  Created by willme on 13-10-15.
     6 //
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface MyView : NSObject
    12 
    13 @end
     1 //
     2 //  MyView.m
     3 //  Unity-iPhone
     4 //
     5 //  Created by willme on 13-10-15.
     6 //
     7 //
     8 
     9 #import "MyView.h"
    10 
    11 @implementation MyView
    12 
    13 //接收Unity3D 传递过来的信息
    14 void _PressButton0()
    15 {
    16     UIAlertView *alert = [[UIAlertView alloc] init];
    17     [alert setTitle:@"雨松MOMO程序世界"];
    18     [alert setMessage:@"点击了第一个按钮"];
    19     [alert addButtonWithTitle:@"确定"];
    20     [alert  show];
    21     [alert release];
    22 }
    23 
    24 void _PressButton1()
    25 {
    26     
    27     UIAlertView *alert = [[UIAlertView alloc] init];
    28     [alert setTitle:@"雨松MOMO程序世界"];
    29     [alert setMessage:@"点击了第二个按钮"];
    30     [alert addButtonWithTitle:@"确定"];
    31     [alert  show];
    32     [alert release];
    33 }
    34 
    35 @end

     **界面如下:

    ***代码来自 http://blog.csdn.net/xys289187120/article/details/6933456

    最后的效果

  • 相关阅读:
    OS程序开发引用的第三方库之间出现冲突的处理方法
    ios的指令集(转)
    查看lib库支持的IOS指令集
    Audio Session Programming Guide
    Swift中文教程
    NSString 与 char * 互转
    id 与void *类型的转换(转)
    versions使用(转)
    superview透明问题
    Python 头部 #!/usr/bin/python 和 #!/usr/bin/env 的区别
  • 原文地址:https://www.cnblogs.com/willbin/p/3370412.html
Copyright © 2011-2022 走看看