UnityはIOS関連インタフェースを呼び出して携帯電話の型番(CSharp)を取得する

2743 ワード

簡単な効果を実現:Buttonをクリックし、IOS AlertViewを呼び出し、ハードウェアモデルを表示
インプリメンテーション:スクリプトに2つの外部メソッドを定義します.1つはAlertViewをポップアップし、もう1つは文字列を返す
GUIでButtonを作成し、クリックしたときに外部関数を呼び出すと、バウンド効果が得られます.
C-Sharpで外部メソッドを定義しました
DllImport("_Internal")とexternがキーです
以下はC-Sharpスクリプトコードです
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Test : MonoBehaviour {
	
	private static string _buttonTitle = "press!!!!";

	[DllImport ("__Internal")]
	private static extern string _getDeviceName();

	[DllImport ("__Internal")]
	private static extern void _showAlertView(string str);
	// Use this for initialization
	void Start () {
	
		if(Application.platform==RuntimePlatform.IPhonePlayer)
		{

			print("Unity:"+_getDeviceName());


		}
	}


	void OnGUI ()
	{
		if (GUI.Button(new Rect (15, 10, 450, 100),_buttonTitle))
		{
			_showAlertView(_getDeviceName());
		}

		GUIStyle labelFont = new GUIStyle();    
		labelFont.normal.textColor = Color.white;
		labelFont.alignment = TextAnchor.MiddleCenter;  
		labelFont.fontSize = 30; 
		GUI.Label(new Rect(15, 150, 100, 100), _getDeviceName(), labelFont);
		
		
	}
	
	
	// Update is called once per frame
	void Update () {
	

	}
}

上記のスクリプトをMain Cameraにバインドし、Build&Runをクリックします(PS:IOSを覚えています)
この時UnityはXCODEを開けてくれました.私たちがしなければならないのは中に新しいクラスを追加することです.私はここで使います.mm
そしてmmでextern“C”でインタフェースをマークする
#import 

@interface CustomMethods : NSObject

@end
//
//  CustomMethods.mm
//  Unity-iPhone
//
//  Created by Dale_Hui on 13-12-13.
//
//

#import "CustomMethods.h"
#import 

static struct utsname systemInfo;

extern "C"
{
    void _showAlertView(const char* str);
    char* _getDeviceName();
}
void _showAlertView(const char* str)
{
    
    UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@"Unity " message:[NSString stringWithUTF8String:str] delegate:nil cancelButtonTitle:@" " otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];
    
    
}

char* _getDeviceName()

{
    
    uname(&systemInfo);
    
    char* deviceName=(char*)malloc(sizeof(char)*255);
    
    strcpy(deviceName, systemInfo.machine);
    
    return deviceName;
    
}


@implementation CustomMethods

@end

IOSにはUnityにコールバックを送信する方法(非同期方法)があります
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");

3つのパラメータは、オブジェクト名、関数名、伝達された情報です.
UnityがIOS IAPを呼び出す(購入):http://blog.chukong-inc.com/index.php/2012/01/05/unity3d-のiap/
extern「C」の説明:http://blog.chinaunix.net/uid-21411227-id-1826909.html