服务 / ChatService
ChatService Class  
聊天服务
语音聊天功能需要在mobile端才可生效,pie没有效果。可以使用手机端测试。
Table of contents 
Accessors  
onVoiceStatusChange(): MulticastDelegate<(userId: string, lastVolume: number, currentVolume: number, isOpenAudio: boolean) => void> client | 
|---|
| 语音聊天委托,每次语音的回调 | 
voiceChatEnabled(): boolean client | 
| 获取是否既不能听也不能说功能,false为否既不能听也不能说;true为既可以听也可以说。 | 
Methods  
asyncBroadcastMessage(type: MessageType, content: string): Promise<BroadcastMessageResult> server | 
|---|
| 发送富文本消息到聊天窗口中,禁止回调中调用该接口。 | 
asyncCloseMic(): Promise<boolean> client | 
| 关闭自己的语音。 | 
asyncCollapseChatWindow(isCollapsed: boolean): Promise<boolean> client | 
| 是否打开或者折叠游戏中的聊天框。true是打开;false是折叠。 | 
asyncEnableChatWindow(isEnabled: boolean): Promise<boolean> client | 
| 打开或关闭某个客户端的聊天功能。true是打开;false是关闭。 | 
asyncMuteAll(): Promise<boolean> client | 
| 一键屏蔽所有玩家的语音。 | 
asyncMutePlayer(userId: string): Promise<boolean> other | 
| 屏蔽指定玩家的声音 | 
asyncOpenMic(): Promise<boolean> client | 
| 打开自己的语音。 | 
asyncSendMessage(content: string): Promise<BroadcastMessageResult> client | 
| 发送快捷语消息,自动携带发送者名称。与聊天框中输入语言一致。不支持富文本。 | 
asyncUnmuteAll(): Promise<boolean> client | 
| 一键打开所有玩家的声音,取消屏蔽 | 
asyncUnmutePlayer(userId: string): Promise<boolean> client | 
| 打开指定玩家的声音,取消屏蔽 | 
asyncsetUserCanChat(userId: string, canChat: boolean): Promise<boolean> client | 
| 设置指定用户聊天权限 | 
getUserCanChat(userId: string): Promise<boolean> client | 
| 获取指定用户聊天权限 | 
Accessors 
onVoiceStatusChange  
•   | ||
|---|---|---|
语音聊天委托,每次语音的回调 Returns 
  | 
voiceChatEnabled  
•   | •   | ||||
|---|---|---|---|---|---|
获取是否既不能听也不能说功能,false为否既不能听也不能说;true为既可以听也可以说。 note: 开启语音功能,接口才可生效 Returns 
  | 设置既不能听也不能说功能,false为否既不能听也不能说;true为既可以听也可以说。 note: 开启语音功能,接口才可生效 Parameters 
  | 
玩家语音是否设置成功。
Methods 
asyncBroadcastMessage  
• Static asyncBroadcastMessage(type, content): Promise<BroadcastMessageResult> server
发送富文本消息到聊天窗口中,禁止回调中调用该接口。
Parameters 
type MessageType | 发送消息类型 | 
|---|---|
content string | 消息内容 range:小于 1200 个字符串长度 | 
Returns 
Promise<BroadcastMessageResult> | 发送消息的结果 | 
|---|
限制每个消息发送从第一个消息发送开始计时60秒内最多发送60条消息。定时器清空计数后下一次发送消息再次开始计数。
note: 开启聊天框聊天功能,接口才可生效
asyncCloseMic  
• Static asyncCloseMic(): Promise<boolean> client
关闭自己的语音。
Returns 
Promise<boolean> | 语音是否设置成功。 | 
|---|
note: 开启语音功能,接口才可生效
使用示例:创建一个名为"NewScript"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,可屏蔽所有玩家语音,无法在editor模式下调试。代码如下:ts
@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncCloseMic().then(()=>{console.log("asyncCloseMic")});
     }
 }@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncCloseMic().then(()=>{console.log("asyncCloseMic")});
     }
 }asyncCollapseChatWindow  
• Static asyncCollapseChatWindow(isCollapsed): Promise<boolean> client
是否打开或者折叠游戏中的聊天框。true是打开;false是折叠。
Parameters 
isCollapsed boolean | 折叠框打开关闭布尔参数 | 
|---|
Returns 
Promise<boolean> | 返回打开折叠是否成功。 | 
|---|
ts
@Component
 export default class ChatExample extends Script {
     protected onStart(): void {
          ChatService.asyncCollapseChatWindow(true);
     }
 }@Component
 export default class ChatExample extends Script {
     protected onStart(): void {
          ChatService.asyncCollapseChatWindow(true);
     }
 }asyncEnableChatWindow  
• Static asyncEnableChatWindow(isEnabled): Promise<boolean> client
打开或关闭某个客户端的聊天功能。true是打开;false是关闭。
Parameters 
isEnabled boolean | 聊天功能打开关闭布尔参数 | 
|---|
Returns 
Promise<boolean> | 聊天功能打开或者关闭是否成功。 | 
|---|
ts
@Component
 export default class ChatExample extends Script {
     protected onStart(): void {
          ChatService.asyncEnableChatWindow(true);
     }
 }@Component
 export default class ChatExample extends Script {
     protected onStart(): void {
          ChatService.asyncEnableChatWindow(true);
     }
 }asyncMuteAll  
• Static asyncMuteAll(): Promise<boolean> client
一键屏蔽所有玩家的语音。
Returns 
Promise<boolean> | 玩家语音是否设置成功。 | 
|---|
note: 开启语音功能,接口才可生效
使用示例:创建一个名为"NewScript"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,可屏蔽所有玩家语音,无法在editor模式下调试。代码如下:ts
@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncMuteAll().then(()=>{console.log("asyncMuteAll")});
     }
 }@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncMuteAll().then(()=>{console.log("asyncMuteAll")});
     }
 }asyncMutePlayer  
• Static asyncMutePlayer(userId): Promise<boolean> other
屏蔽指定玩家的声音
Parameters 
userId string | 玩家的 userid  default:null range: 依据 userId 长度而定  | 
|---|
Returns 
Promise<boolean> | 玩家语音是否设置成功。 | 
|---|
note: 开启语音功能,接口才可生效
使用示例:创建一个名为"NewScript"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,可屏蔽所有玩家语音,无法在editor模式下调试。代码如下:ts
@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncMutePlayer(Player.localPlayer.userId).then(()=>{console.log("asyncMutePlayer")});
     }
 }@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncMutePlayer(Player.localPlayer.userId).then(()=>{console.log("asyncMutePlayer")});
     }
 }asyncOpenMic  
• Static asyncOpenMic(): Promise<boolean> client
打开自己的语音。
Returns 
Promise<boolean> | 语音是否设置成功。 | 
|---|
note: 开启语音功能,接口才可生效
使用示例:创建一个名为"NewScript"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,可屏蔽所有玩家语音,无法在editor模式下调试。代码如下:ts
@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncOpenMic().then(()=>{console.log("asyncOpenMic")});
     }
 }@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncOpenMic().then(()=>{console.log("asyncOpenMic")});
     }
 }asyncSendMessage  
• Static asyncSendMessage(content): Promise<BroadcastMessageResult> client
发送快捷语消息,自动携带发送者名称。与聊天框中输入语言一致。不支持富文本。
Parameters 
content string | 消息内容 range:小于 128 个字符串长度 | 
|---|
Returns 
Promise<BroadcastMessageResult> | 发送消息的结果 | 
|---|
note: 开启聊天框聊天功能,接口才可生效
使用示例:创建一个名为"messageExample"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,你将在场景中看到在聊天框中显示"asyncBroadcastMessage is called"的效果。代码如下:ts
@Component
export default class NewExample extends Script {
    protected onStart(): void {
        if (SystemUtil.isClient()){
            this.test();
        }
        if(SystemUtil.isServer()){
            Event.addClientListener("bro_two",()=>{
                ChatService.asyncBroadcastMessage(MessageType.Room,"bro").then(()=>{
                    console.log("asyncBroadcastMessage is called");
                });
            });
        }
        if(SystemUtil.isClient()){
            Event.addLocalListener("bro",()=>{
                Event.dispatchToServer("bro_two");
            });
        }
    }
    private async test(): Promise<void> {
        let btn = new ButtonUI();
        InputUtil.onKeyDown(Keys.F, async () => {
            let playerPos = Player.localPlayer.character.worldTransform.position;
            let result = InputUtil.projectWorldPositionToWidgetPosition(playerPos);
            if (result) {
                btn.button.position = result.screenPosition;
            }
        })
    }
}
class ButtonUI {
    public button: StaleButton;
    public buttonTwo: StaleButton;
    constructor(fun: Function = null) {
        this.creatUI(fun);
    }
    private creatUI(fun: Function = null) {
        // 创建一个UI对象
        let ui = UserWidget.newObject();
        // 将UI添加到屏幕上
        ui.addToViewport(1);
        // 创建一个画布组件
        let rootCanvas = Canvas.newObject();
        rootCanvas.size = new Vector2(1920, 1080);
        rootCanvas.position = Vector2.zero;
        // 将Ui的根画布设置为rootCanvas
        ui.rootContent = rootCanvas;
        // 创建一个按钮
        this.button = StaleButton.newObject(rootCanvas);
        this.button.position = new Vector2(1000, 310);
        this.button.size = new Vector2(200, 50);
        this.button.text = "SendMes";
        this.button.transitionEnable = true;
        this.button.pressedImagColor = LinearColor.red;
        this.button.visibility = SlateVisibility.Visible;
        this.button.onClicked.add(() => {
            console.log("btn ")
            ChatService.asyncSendMessage("hello").then(()=>{
                console.log("asyncSendMessage is called");
            });
            if (fun) {
                fun();
            }
        });
        this.buttonTwo = StaleButton.newObject(rootCanvas);
        this.buttonTwo.position = new Vector2(1700, 310);
        this.buttonTwo.size = new Vector2(150, 50);
        this.buttonTwo.text = "BroMes";
        this.buttonTwo.transitionEnable = true;
        this.buttonTwo.pressedImagColor = LinearColor.red;
        this.buttonTwo.visibility = SlateVisibility.Visible;
        this.buttonTwo.onClicked.add(() => {
            console.log("btn ")
            Event.dispatchToLocal("bro");
            if (fun) {
                fun();
            }
        });
    }
}@Component
export default class NewExample extends Script {
    protected onStart(): void {
        if (SystemUtil.isClient()){
            this.test();
        }
        if(SystemUtil.isServer()){
            Event.addClientListener("bro_two",()=>{
                ChatService.asyncBroadcastMessage(MessageType.Room,"bro").then(()=>{
                    console.log("asyncBroadcastMessage is called");
                });
            });
        }
        if(SystemUtil.isClient()){
            Event.addLocalListener("bro",()=>{
                Event.dispatchToServer("bro_two");
            });
        }
    }
    private async test(): Promise<void> {
        let btn = new ButtonUI();
        InputUtil.onKeyDown(Keys.F, async () => {
            let playerPos = Player.localPlayer.character.worldTransform.position;
            let result = InputUtil.projectWorldPositionToWidgetPosition(playerPos);
            if (result) {
                btn.button.position = result.screenPosition;
            }
        })
    }
}
class ButtonUI {
    public button: StaleButton;
    public buttonTwo: StaleButton;
    constructor(fun: Function = null) {
        this.creatUI(fun);
    }
    private creatUI(fun: Function = null) {
        // 创建一个UI对象
        let ui = UserWidget.newObject();
        // 将UI添加到屏幕上
        ui.addToViewport(1);
        // 创建一个画布组件
        let rootCanvas = Canvas.newObject();
        rootCanvas.size = new Vector2(1920, 1080);
        rootCanvas.position = Vector2.zero;
        // 将Ui的根画布设置为rootCanvas
        ui.rootContent = rootCanvas;
        // 创建一个按钮
        this.button = StaleButton.newObject(rootCanvas);
        this.button.position = new Vector2(1000, 310);
        this.button.size = new Vector2(200, 50);
        this.button.text = "SendMes";
        this.button.transitionEnable = true;
        this.button.pressedImagColor = LinearColor.red;
        this.button.visibility = SlateVisibility.Visible;
        this.button.onClicked.add(() => {
            console.log("btn ")
            ChatService.asyncSendMessage("hello").then(()=>{
                console.log("asyncSendMessage is called");
            });
            if (fun) {
                fun();
            }
        });
        this.buttonTwo = StaleButton.newObject(rootCanvas);
        this.buttonTwo.position = new Vector2(1700, 310);
        this.buttonTwo.size = new Vector2(150, 50);
        this.buttonTwo.text = "BroMes";
        this.buttonTwo.transitionEnable = true;
        this.buttonTwo.pressedImagColor = LinearColor.red;
        this.buttonTwo.visibility = SlateVisibility.Visible;
        this.buttonTwo.onClicked.add(() => {
            console.log("btn ")
            Event.dispatchToLocal("bro");
            if (fun) {
                fun();
            }
        });
    }
}asyncUnmuteAll  
• Static asyncUnmuteAll(): Promise<boolean> client
一键打开所有玩家的声音,取消屏蔽
Returns 
Promise<boolean> | 语音是否设置成功。 | 
|---|
note: 开启语音功能,接口才可生效
使用示例:创建一个名为"NewScript"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,可屏蔽所有玩家语音,无法在editor模式下调试。代码如下:ts
@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncUnmuteAll().then(()=>{console.log("asyncUnmuteAll")});
     }
 }@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncUnmuteAll().then(()=>{console.log("asyncUnmuteAll")});
     }
 }asyncUnmutePlayer  
• Static asyncUnmutePlayer(userId): Promise<boolean> client
打开指定玩家的声音,取消屏蔽
Parameters 
userId string | 玩家的 userid  default:null range: 依据 userId 的长度而定  | 
|---|
Returns 
Promise<boolean> | 语音是否设置成功。 | 
|---|
note: 开启语音功能,接口才可生效
使用示例:创建一个名为"NewScript"的脚本,放置在对象管理器中,打开脚本,输入以下代码保存,运行游戏,可屏蔽所有玩家语音,无法在editor模式下调试。代码如下:ts
@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncUnmutePlayer(Player.localPlayer.userId).then(()=>{console.log("asyncUnmutePlayer")});
     }
 }@Component
 export default class NewScript extends Script {
     protected onStart(): void {
         ChatService.asyncUnmutePlayer(Player.localPlayer.userId).then(()=>{console.log("asyncUnmutePlayer")});
     }
 }asyncsetUserCanChat  
• Static asyncsetUserCanChat(userId, canChat): Promise<boolean> client
设置指定用户聊天权限
Parameters 
userId string | 用户 ID range: 根据 ID 长度决定 | 
|---|---|
canChat boolean | 是否开启用户聊天权限。 | 
Returns 
Promise<boolean> | 是否设置成功 | 
|---|
getUserCanChat  
• Static getUserCanChat(userId): Promise<boolean> client
获取指定用户聊天权限
Parameters 
userId string | 用户 ID range: 不限制 | 
|---|
Returns 
Promise<boolean> | 是否获取成功 | 
|---|