TurboSquidの3DモデルをiPhoneのARで表示するにはUnityで始めるARKit入門(4)(3/3 ページ)

» 2018年02月22日 05時00分 公開
[薬師寺国安PROJECT KySS]
前のページへ 1|2|3       

ロケットが飛ぶスクリプト

 図12でRocket_Parentを選択してInspectorの「Add Component」から新しいスクリプトを追加する。「Name」は「FlyScript」を、「Language」には「C#」を選択する。Inspector内に追加された「FlyScript」をダブルクリックしてコードエディタを起動し、リスト1のコードを記述する。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class FlyScript : MonoBehaviour
  5. {
  6. //bool型の変数flyClickedを宣言する
  7. private bool flyClicked;
  8. void Start()
  9. {
  10. //Start関数内では、flyClicked変数をfalseで初期化しておく
  11. flyClicked = false;
  12. }
  13. void Update()
  14. {
  15. //Update関数内では、変数flyClickedがtrueであれば、「transform.Translate」を使って、ロケットを空に向けて飛ばす
  16. if (flyClicked == true)
  17. {
  18. transform.Translate((Vector3.right * Time.deltaTime * (transform.localScale.x * 0.0002f)));
  19. }
  20. }
  21. //Fly関数内ではClicked変数をtrueで初期化しておく
  22. public void Fly()
  23. {
  24. flyClicked = true;
  25. }
  26. }
リスト1 FlyScript.cs

炎を出すスクリプト

 図12でFramethrowFireを選択して、Inspectorの「Add Component」から新しいスクリプトを追加する。「Name」には「FireScript」を、「Language」には「C#」を選択する。Inspector内に追加された「FireScript」をダブルクリックしてコードエディタを起動し、リスト2のコードを記述する。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class FireScript : MonoBehaviour
  5. {
  6. //ParticleSystem型の変数fireを宣言していおく
  7. private ParticleSystem fire;
  8. //bool型の変数isPlayningを宣言しておく
  9. private bool isPlayning;
  10. void Start()
  11. {
  12. //Start関数内では、GetComponentでPaticleSystemコンポーネントを取得して、変数fireで参照する
  13. //変数isPlayningはfalseで初期化しておく
  14. fire = GetComponent<ParticleSystem>();
  15. isPlayning = false;
  16. }
  17. //Fire関数内の処理
  18. //変数isPlayningがtrueなら、炎の噴出を停止して、変数isPlayningをfalseで初期化する
  19. public void Fire()
  20. {
  21. if (isPlayning)
  22. {
  23. fire.Stop();
  24. isPlayning = false;
  25. }
  26. //そうでなければ、炎を噴出して、変数「isPlayning」をtrueで初期化する
  27. else
  28. {
  29. fire.Play();
  30. isPlayning = true;
  31. }
  32. }
  33. }
リスト2 FireScript.cs

煙を出すスクリプト

 図12でFramethrowSmokeを選択して、Inspectorの「Add Component」から新しいスクリプトを追加する。「Name」には「SmokeScript」を、「Language」には「C#」を選択する。Inspector内に追加された、「SmokeScript」をダブルクリックしてコードエディタを起動して、リスト3のコードを記述する。解説はリスト2の炎を噴出する解説と全く同じ。炎が煙に変わっただけだ

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SmokeScript : MonoBehaviour
  5. {
  6. private ParticleSystem smoke;
  7. private bool isPlayning;
  8. void Start()
  9. {
  10. smoke = GetComponent<ParticleSystem>();
  11. isPlayning = true;
  12. }
  13. public void Smoke()
  14. {
  15. if (isPlayning)
  16. {
  17. smoke.Stop();
  18. isPlayning = false;
  19. }
  20. else
  21. {
  22. smoke.Play();
  23. isPlayning = true;
  24. }
  25. }
  26. }
リスト3 SomkeScript.cs

ボタンとプログラムを関連付ける

 HierarchyのCanvas内の、「Fly」という名前のボタンを選択し、Inspectorを表示する。Button(Script)に「On Click()」というイベントがあるので、この右下にある「+」アイコンをクリックする。すると「On Click()」内が変化する。

 連載第3回の図18で解説しているので、図が見たい方はそちらを参照してほしい。図18の下図の「None(Object)」とあるところに、Hierarchy内のRocket_Parentをドラッグ&ドロップする。グレー表示だった「None Function」が「上下▲アイコン」で選択可能になる。

 「No Function」の「上下▲アイコン」で「FlyScript」→「Fly()」と選択する。

 Fireボタンには、「None(Object)」とある箇所にHierarchy内のFramethrowerFireをドラッグ&ドロップし、「None Function」で「FireScript」→「Fire()」と選択する。

 Smokeボタンには、「None(Object)」とある箇所にHierarchy内のFramethrowerSmokeをドラッグ&ドロップし、「None Function」で「FireSmoke」→「Smoke()」と選択する。

 以上でボタンの関連付けは完了だ。

UnityARHitTestExample.csにコードを追加する

 Hierarchy内のHitCubeParentの子であるRocket_Parentを選択してInspectorを表示する。「Unity AR Hit Test Example(Script)」の中に、UnityARHitTestExampleのスクリプトがあるので、これをクリックしてコードエディタで開いてリスト4のようにコードを編集する。

  1. using UnityEngine.EventSystems;
  2. void Update () {
  3. if ((Input.touchCount > 0 && m_HitTransform != null) && !IsPointerOverUIObject())
  4. {
  5. var touch = Input.GetTouch(0);
  6. if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
  7. {
  8. var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
  9. ARPoint point = new ARPoint {
  10. x = screenPosition.x,
  11. y = screenPosition.y
  12. };
  13. // prioritize reults types
  14. ARHitTestResultType[] resultTypes = {
  15. ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
  16. // if you want to use infinite planes use this:
  17. //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
  18. ARHitTestResultType.ARHitTestResultTypeHorizontalPlane,
  19. ARHitTestResultType.ARHitTestResultTypeFeaturePoint
  20. };
  21. foreach (ARHitTestResultType resultType in resultTypes)
  22. {
  23. if (HitTestWithResultType (point, resultType))
  24. {
  25. return;
  26. }
  27. }
  28. ~中略~
  29. private bool IsPointerOverUIObject()
  30. {
  31. PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
  32. eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  33. List<RaycastResult> results = new List<RaycastResult>();
  34. EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
  35. return results.Count > 0;
  36. }
リスト4 UnityARHitTestExample.cs

 まず、「using UnityEngine.EventSystems;」と記述して、EventSystemsの名前空間を読み込んでおく。これは、Unityのシーンのイベントを処理する役割を持っている。忘れずに追加してほしい。

 4行目で「&& !IsPointerOverUIObject()」を追加している。この関数は32~39行目で定義している。IsPointerOverUIObject()については、前回記事を参照してほしい。

ビルド&実行結果

 ここまでのSceneを上書き保存し、前回同様ビルドして実行しよう。実行結果は図1のようになるはずだ。

動画1 「ARkitRocket」サンプルの実行動画

 次回は、無数の球がランダムにバウンドするサンプルを紹介する。お楽しみに。

著者プロフィール

薬師寺 国安(やくしじ くにやす) / 薬師寺国安事務所

薬師寺国安事務所代表。Visual Basicプログラミングと、マイクロソフト系の技術をテーマとした、書籍や記事の執筆を行う。

1950年生まれ。事務系のサラリーマンだった40歳から趣味でプログラミングを始め、1996年より独学でActiveXに取り組む。

1997年に薬師寺聖とコラボレーション・ユニット「PROJECT KySS」を結成。

2003年よりフリーになり、PROJECT KySSの活動に本格的に参加。.NETやRIAに関する書籍や記事を多数執筆する傍ら、受託案件のプログラミングも手掛ける。

Windows Phoneアプリ開発を経て、現在はWindowsストアアプリを多数公開中。

Microsoft MVP for Development Platforms - Client App Dev (Oct 2003-Sep 2012)。

Microsoft MVP for Development Platforms - Windows Phone Development(Oct 2012-Sep 2013)。

Microsoft MVP for Development Platforms - Client Development(Oct 2013-Sep 2014)。

Microsoft MVP for Development Platforms-Windows Platform Development(Oct 2014-Sep 2015)。

前のページへ 1|2|3       

Copyright © ITmedia, Inc. All Rights Reserved.

スポンサーからのお知らせPR

Smart & Social 記事ランキング

本日月間

注目のテーマ

4AI by @IT - AIを作り、動かし、守り、生かす
Microsoft & Windows最前線2025
AI for エンジニアリング
ローコード/ノーコード セントラル by @IT - ITエンジニアがビジネスの中心で活躍する組織へ
Cloud Native Central by @IT - スケーラブルな能力を組織に
システム開発ノウハウ 【発注ナビ】PR
あなたにおすすめの記事PR

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。