0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Playcanvasでボタンクリックイベントを作成する

Posted at

Hierarchyビューの下にある➕️ボタンを選択
「UserInterface」→「Button」を選択。
Positionで位置を調整。Scaleで大きさ調整。

「➕️ADD COMPONENT」から「〈〉Script」を選択。
SCRIPTで「➕️Create Script」を選択。

仮で「ButtonClick.js」とする。
「〈〉Script」で「ButtonClick.js」を設定。

ButtonClick.jsを開く。

var ButtonClick = pc.createScript('buttonClick');

// initialize code called once per entity
ButtonClick.prototype.initialize = function() {

};

// update code called every frame
ButtonClick.prototype.update = function(dt) {

};

// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// ButtonClick.prototype.swap = function(old) { };

// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/

クリックしたときにコンソールにメッセージを表示。

var ButtonClick = pc.createScript('ButtonClick');

// initialize code called once per entity
ButtonClick.prototype.initialize = function() {
    if (this.app.touch) {
        // タッチデバイスを監視
        this.entity.element.on(pc.EVENT_TOUCHSTART, this.onClick, this);
    } else {
        // マウスクリックを監視
        this.entity.element.on(pc.EVENT_MOUSEDOWN, this.onClick, this);
    }
};

ButtonClick.prototype.onClick = function(event) {
    console.log('OK');
};

// update code called every frame
ButtonClick.prototype.update = function(dt) {

};

// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// ButtonClick.prototype.swap = function(old) { };

// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/

Launchで確認する。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?