2014年4月15日火曜日

外部ブラウザからURLを開くサンプル

Unityにて外部ブラウザからURLを開くサンプル。言語はC#。

使う関数は、Application.OpenURL("URL")
※URLはきちんと生成しないとAndroid機でうまく開けないので注意。

-----
/*
 * @file Sample_OpenURL.cs
 * @attention 
 * @note Sample for Open URL
 */
using UnityEngine;

public class Sample_OpenURL : MonoBehaviour {
 //string TestURL = "www.google.co.jp";    //Win OK / Android NG
 //string TestURL = "http://www.google.co.jp"; // Win OK / Android OK
 //string TestURL = "https://www.google.co.jp"; // Win OK / Android OK
 string TestURL = "http://www.google.co.jp/"; // Win OK / Android OK ***** URL ******
 void OnGUI(){
  if(ScrnHor!=Screen.width){ ReCalcScrn(); }
  
  GUI.Box(rectBg, "Menu");
  
  if(GUI.Button(rectURL, "Open URL: "+TestURL)) {
   Application.OpenURL(TestURL); // ***** Open URL ******
  }
 }
 //============================================================
 float ScrnHor, ScrnVert;
 Rect rectBg, rectURL;
 void ReCalcScrn(){
  ScrnHor = (float)Screen.width;
  ScrnVert = (float)Screen.height;
  rectBg = new Rect(ScrnHor*0.1f,ScrnVert*0.1f,ScrnHor*0.8f,ScrnVert*0.8f);
  rectURL = new Rect(ScrnHor*0.12f,ScrnVert*0.16f,ScrnHor*0.76f,ScrnVert*0.1f);
 }
 void Start () {
  //Screen.orientation = ScreenOrientation.Portrait;
  Screen.orientation = ScreenOrientation.AutoRotation;
  ReCalcScrn();
 }
 void Update () {
  //「戻るキー」でアプリ終了 (Android) ※機種判定を外すとESCでアプリ終了(Win)
  if(Application.platform == RuntimePlatform.Android && Input.GetKey(KeyCode.Escape)){//戻るボタン対応
   Application.Quit();
  }
 }
 //============================================================
}
-----

0 件のコメント:

コメントを投稿