0
1

More than 1 year has passed since last update.

PlayCanvasでクリックできるボタンを設置する

Last updated at Posted at 2019-12-27

PlayCanvasでクリックに反応するボタンを追加する方法

スクリーンショット 2019-12-27 16 39 11

今回のプロジェクトの詳細はこちらになります

PlayCanvasでクリックできるボタンを4つ追加するには、このヒエラルキーになります。

  • 2D Screen
    • Button
      • Text
    • Button
      • Text
    • Button
      • Text
    • Button
      • Text

2D Screenとボタンをエディターから追加する

2D Screenを「Add Entity」 → 「User Interface 」 → 「2D Screen」から追加します。

スクリーンショット 2019-12-27 15 54 45

クリックに反応させる

ボタン(Element Component)にクリックを検出するには、 this.entity.element.onでクリックイベントを取得します。

var Popup = pc.createScript('popup');
// エディターから変更できるようにattribteusを追加する
Popup.attributes.add("message", {type: "string", default: "message"})

// 起動時
Popup.prototype.initialize = function() {
    // タッチできる端末だったら
    if(this.app.touch){
         // element componentがタッチされたらattributes: messageアラートとして表示する
        this.entity.element.on(pc.EVENT_TOUCHSTART, () => this.show(this.message))
    }else{
         // element componentがクリックされたらattributes: messageアラートとして表示する
        this.entity.element.on("click", () => this.show(this.message))
    }
    
};

Popup.prototype.show = (message) => alert(message)
PlayCanvas開発で参考になりそうな記事の一覧です。 入門 応用 その他の記事はこちらになります。 その他関連 - [PlayCanvasタグの付いた記事一覧](https://qiita.com/tags/playcanvas)

PlayCanvasのユーザー会のSlackを作りました!

少しでも興味がありましたら、ユーザー同士で解決・PlayCanvasを推進するためのSlackを作りましたので、もしよろしければご参加ください!

日本PlayCanvasユーザー会 - Slack

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1