あなたの世界を作りなさい
チャット
チャットメッセージの送受信をカスタマイズする
11 分
送受信するチャットメッセージをzepeto chatパッケージをインポートすることでカスタマイズできます。 ❗️ 注意 この機能はマルチプレイヤー要素が実装されたワールドで利用可能です。 👍 参考ポイント エディタ環境でもチャットを送受信できます。 ハンドラーを通じてユーザーが作成したチャットメッセージのみが送受信できます(クイックチャット、システムメッセージは受信できません) 関数定義 class zepetochat extends system object { public static get instance() zepetochatimplementation; public static get onreceivedmessage() unityengine events unityevent$1\<imessage>; public constructor(); public static send($message string) void; } zepetochat。send()を使用して特定のメッセージをチャットウィンドウに送信できます。 zepetochat onreceivedmessage addlistener()を使用して、どのイベントでメッセージを受信するかを決定できます。 使用例 ボタン1と2を押して指定されたメッセージを送信します。 メッセージを送信するためにinputfieldにカスタムメッセージを作成します。 import { zepetoscriptbehaviour } from 'zepeto script'; import { button, inputfield, text } from 'unityengine ui'; import { zepetochat, messagetype, usermessage } from 'zepeto chat'; import { color } from 'unityengine'; export default class customchat extends zepetoscriptbehaviour { public custom1chatbtn button; public custom2chatbtn button; public sendchatbtn button; public resulttext text; public inputchatbox inputfield; start() { // ボタンがクリックされたとき this custom1chatbtn onclick addlistener(() => { // テキストの色を変更 this resulttext color = color magenta; // メッセージを送信 zepetochat send("1"); }); this custom2chatbtn onclick addlistener(() => { this resulttext color = color blue; zepetochat send("2"); }); this sendchatbtn onclick addlistener(() => { this resulttext color = color black; const inputmsg = this inputchatbox text; zepetochat send(inputmsg); }); // メッセージを受信 zepetochat onreceivedmessage addlistener(msg => { const usermsg = msg as usermessage; this resulttext text = `\[user ${usermsg username}] ${usermsg message}`; }); } } チャットボタンの有効化/無効化 マルチプレイヤー要素が実装された世界で利用可能な機能。 モバイルテストで確認できます。 宣言 public static setactivechatui($value boolean) void; サンプルコード 特定のボタンを押してチャットボタンを有効/無効に切り替えます。 import { zepetoscriptbehaviour } from 'zepeto script'; import { button } from 'unityengine ui'; import { zepetochat, messagetype, usermessage } from 'zepeto chat'; export default class chatcontroller extends zepetoscriptbehaviour { public chatuibtn button; private ischatuiactive boolean = true; start() { // ボタンがクリックされたとき this chatuibtn onclick addlistener(() => { // メッセージを送信 if (this ischatuiactive) { zepetochat setactivechatui(false); this ischatuiactive = false; console log("チャットui無効化"); } else { zepetochat setactivechatui(true); this ischatuiactive = true; console log("チャットui有効化"); } }); } } フィルタリングされたメッセージの決定 マルチプレイヤー要素が実装された世界で利用可能な機能。 モバイルテストで確認できます。 宣言 public isfiltered boolean; サンプルコード 受信したチャットメッセージがフィルタリングされたメッセージかどうかを判断し、画面に表示します。 import { zepetoscriptbehaviour } from 'zepeto script'; import { text } from 'unityengine ui'; import { zepetochat, messagetype, usermessage } from 'zepeto chat'; export default class chatcontroller extends zepetoscriptbehaviour { public filterresulttext text; start() { // メッセージを受信 zepetochat onreceivedmessage addlistener(msg => { const usermsg = msg as usermessage; if (usermsg isfiltered) { this filterresulttext text = `このメッセージはフィルタリングされました ${usermsg message}`; } else { this filterresulttext text = `このメッセージはフィルタリングされていません ${usermsg message}`; } }); } }