iOS和unity的互動問題

來源:互聯網
上載者:User

標籤:

之前完全沒接觸過unity,因為現在公司需要在unity的代碼上做二次開發,所以我要能夠隨意調用unity的方法,在網上搜了很多種方法都沒什麼用,全是幾年前的版本,我只是在別人原來的方法上更新下,寫一個最新版本的互動例子。   我用的是unity5.3和Xcode7.3

 建立一個按鈕直接跳轉到unity項目的方法網上可以搜到,我就不做描述了

這裡只說怎樣隨意調用unity裡單個的方法

建立unity情境這個可以通過

雨松momo大神的方法來弄 http://www.xuanyusong.com/archives/517

 

 

我的方法就是在他的基礎上更新了一下,主要是他那個版本是很老的,不適用現在的版本

匯出成iOS後我建立了一個類叫MyView  

 MyView.h檔案

#import <UIKit/UIKit.h>@interface MyView : NSObject+(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller;+(void)CreateTitle:(UIViewController *)controller;+(void)LeftButtonPressed;+(void)RightButtonPressed;+(void)UpButtonPressed;+(void)DownButtonPressed;+(void)Init:(UIViewController *)controller;@end

MyView.mm檔案

#import "MyView.h"@interface MyView ()@end@implementation MyView//建立按鈕+(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller{    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];        //設定按鈕範圍    btn.frame = rect;        //設定按鈕顯示內容    [btn setTitle:title forState:UIControlStateNormal];        //設定按鈕改變後綁定的回應程式法    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];        //加入View中    [controller.view addSubview:btn];}//建立標籤+(void)CreateTitle:(UIViewController *)controller{    //建立label視圖    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];    //設定顯示內容    label.text = @"旋轉控制";    //設定背景顏色    label.backgroundColor = [UIColor blueColor];    //設定文字顏色    label.textColor = [UIColor whiteColor];    //設定顯示位置置中    label.textAlignment = NSTextAlignmentCenter;    //設定字型大小    label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];        [controller.view addSubview:label];}//向左按鈕+(void)LeftButtonPressed{    UnitySendMessage("Cube", "MoveLeft","");}//向右按鈕+(void)RightButtonPressed{    UnitySendMessage("Cube", "MoveRight","");}//向上按鈕+(void)UpButtonPressed{    UnitySendMessage("Cube", "MoveUp","");}//向下按鈕+(void)DownButtonPressed{    UnitySendMessage("Cube", "MoveDown","");}+(void)Init:(UIViewController *)controller{    [self CreateTitle:controller];        [self CreateButton:@"左旋轉" rect:CGRectMake(0,  50, 100, 40) action:@selector(LeftButtonPressed) controller:controller];    [self CreateButton:@"右旋轉" rect:CGRectMake(0, 100, 100, 40) action:@selector(RightButtonPressed) controller:controller];    [self CreateButton:@"上旋轉" rect:CGRectMake(0, 150, 100, 40) action:@selector(UpButtonPressed) controller:controller];    [self CreateButton:@"下旋轉" rect:CGRectMake(0, 200, 100, 40) action:@selector(DownButtonPressed) controller:controller];}@end

 

然後進入UnityAppController.mm裡

 

在UnityAppController.mm裡面匯入標頭檔

 

 

找到這一行

 

 

在這個方法裡的return YES 之前加上這段代碼

 

 

然後運行就出現了這個介面

 

 

 

點擊按鈕後這個模型會旋轉

 

好了,就是這些

iOS和unity的互動問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.