Skip to content
AccountService

服务 / AccountService

AccountService Class

用户账号信息管理服务

Table of contents

Methods

addFriend(resp: MGSResponse, userId: string, reason: string): void client
发起添加好友请求
applySharedId(character: Character, id: string, callback: BoolResponse): void client
应用分享Id的角色数据
checkVIP(userId: string, gameId: string, callback: (result: string) => void): void client
发起checkVIP并获得回调,查询玩家的vip信息
createSharedId(character: Character, callback: StringResponse): void client
生成分享Id
dataShowToOther(index: number, isOpen: boolean, callback?: BoolResponse): void client
设置数据是否公开给其他用户
downloadData(character: Character, callback?: BoolResponse VoidResponse, index?: number): void client
下载角色形象并应用到当前角色身上
fillAvatar(img: Image): void client
将头像赋值到Image变量上
getNickName(): string client
获取玩家昵称
getUserData(userId: string, index: number, callback: StringResponse): void client
获取用户存储在服务器上的角色形象数据
getUserId(): string client
获取平台的用户Id,可以用于getUserData接口
getUserInfo(userId: string, gameId: string, callback: (nickname: string, gender: number) => void): void client
发起getUserInfo并获得回调,查询玩家的昵称、性别
isFriend(resp: MGSResponse, userId: string): void client
若需要检测玩家是否好友关系,可通过调用isFriend接口进行查看
setUserData(character: Character, dataString: string, callback?: BoolResponse): void client
将角色形象数据应用至角色
uploadData(character: Character, callback?: BoolResponse VoidResponse, index?: number, openStatus?: number): void client
上传角色形象资源到服务器 Character, Hair, UpperCloth, LowerCloth, Gloves, Shoe

Methods

addFriend

Static addFriend(resp, userId, reason): void client

发起添加好友请求

Parameters

resp MGSResponseGameService的回调
userId string要加的玩家UserId range: 无
reason string申请理由 range: 无

Precautions

  1. 只在移动端生效
  2. 查询的目标玩家需要和当前玩家在同一房间。否则返回的数据会是"参数格式不正确"。

applySharedId

Static applySharedId(character, id, callback): void client

应用分享Id的角色数据

Parameters

character Character分享换装数据的角色
id string分享Id
callback BoolResponse回调参数,true:应用成功;false:应用失败

checkVIP

Static checkVIP(userId, gameId, callback): void client

发起checkVIP并获得回调,查询玩家的vip信息

Parameters

userId string玩家UserId range: 无
gameId stringGameId range: 无
callback (result: string) => void返回 result,玩家的vip是否正常

Precautions

  1. 只在移动端生效
  2. 查询的目标玩家需要和当前玩家在同一房间。否则返回的数据会是"参数格式不正确"。

createSharedId

Static createSharedId(character, callback): void client

生成分享Id

Parameters

character Character分享换装数据的角色
callback StringResponse回调参数,返回生成的Id

dataShowToOther

Static dataShowToOther(index, isOpen, callback?): void client

设置数据是否公开给其他用户

Parameters

index number资源位(0-5) default:0,主角资源位
isOpen boolean是否公开
callback? BoolResponse设置是否成功的回调 default:无回调

downloadData

Static downloadData(character, callback?, index?): void client

下载角色形象并应用到当前角色身上

Parameters

character Character要应用换装数据的角色
callback? BoolResponse VoidResponse设置是否成功的回调 default:默认没有回调
index? number角色位(0-5) default:0,主角资源位

fillAvatar

Static fillAvatar(img): void client

将头像赋值到Image变量上

Parameters

img Image需要赋值获得头像的Image变量

Precautions

只在移动端生效

使用示例:创建一个名为AccountExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,保存并运行游戏,会在屏幕右上方显示用户的头像,PC环境下为白图
ts
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let imgUI = new ImageUI();
        AccountService.fillAvatar(imgUI.image);
    }

}

class ImageUI {
    public image: mw.Image;

    constructor() {
        this.creatUI();
    }

    private creatUI() {
        // 创建一个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.image = mw.Image.newObject(rootCanvas);
        this.image.position = new Vector2(1700, 310);
        this.image.size = new Vector2(150, 50);
        this.image.visibility = SlateVisibility.Visible;
    }
}
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let imgUI = new ImageUI();
        AccountService.fillAvatar(imgUI.image);
    }

}

class ImageUI {
    public image: mw.Image;

    constructor() {
        this.creatUI();
    }

    private creatUI() {
        // 创建一个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.image = mw.Image.newObject(rootCanvas);
        this.image.position = new Vector2(1700, 310);
        this.image.size = new Vector2(150, 50);
        this.image.visibility = SlateVisibility.Visible;
    }
}

getNickName

Static getNickName(): string client

获取玩家昵称

Returns

string昵称

Precautions

只在移动端生效

使用示例:创建一个名为AccountExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,保存并运行游戏,控制窗口会输出用户的昵称,PC环境下为空
ts
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let name = AccountService.getNickName();
        console.log("获取玩家昵称", name);
    }

}
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let name = AccountService.getNickName();
        console.log("获取玩家昵称", name);
    }

}

getUserData

Static getUserData(userId, index, callback): void client

获取用户存储在服务器上的角色形象数据

Parameters

userId string用户Id
index number资源位(0-5)
callback StringResponse返回获取的数据string.
使用示例:创建一个名为AccountExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布在真机上运行游戏,会看到一个和玩家形象一致的人形对象
ts
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = await Player.asyncGetLocalPlayer();
        let npc = (await GameObject.asyncSpawn({ guid: "NPC" })) as Character;
        npc.worldTransform.position = new Vector(0, 0, 200);
        AccountService.getUserData(player.userId, 0, async str => {
            await TimeUtil.delaySecond(5);
            AccountService.setUserData(npc, str, isSuccess => {
                player.character.name = isSuccess ? "成功" : "失败";
            })
        });
    }
}
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = await Player.asyncGetLocalPlayer();
        let npc = (await GameObject.asyncSpawn({ guid: "NPC" })) as Character;
        npc.worldTransform.position = new Vector(0, 0, 200);
        AccountService.getUserData(player.userId, 0, async str => {
            await TimeUtil.delaySecond(5);
            AccountService.setUserData(npc, str, isSuccess => {
                player.character.name = isSuccess ? "成功" : "失败";
            })
        });
    }
}

getUserId

Static getUserId(): string client

获取平台的用户Id,可以用于getUserData接口

Returns

string用户Id
使用示例:创建一个名为AccountExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,保存并运行游戏,控制窗口会输出用户的UserId
ts
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let userId = AccountService.getUserId();
        console.log("获取平台的用户Id", userId);
    }
}
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let userId = AccountService.getUserId();
        console.log("获取平台的用户Id", userId);
    }
}

getUserInfo

Static getUserInfo(userId, gameId, callback): void client

发起getUserInfo并获得回调,查询玩家的昵称、性别

Parameters

userId string玩家UserId range: 无
gameId stringGameId range: 无
callback (nickname: string, gender: number) => void返回 nickname(string) 和 gender(number)

Precautions

  1. 只在移动端生效
  2. 查询的目标玩家需要和当前玩家在同一房间。否则返回的数据会是"参数格式不正确"。

isFriend

Static isFriend(resp, userId): void client

若需要检测玩家是否好友关系,可通过调用isFriend接口进行查看

Parameters

resp MGSResponseGameService的回调。在收到结果时会触发该回调,参数含义如下: - isSuccess - 正常获取到了查询结果即为true,并不代表为好友关系。false表示请求出错。 - jsonData - 当userId对应的角色和当前玩家不在同一房间,返回值为"参数格式不正确"; 当userId对应的角色和当前玩家在同一房间,且互为好友,返回值为"true"; 当userId对应的角色和当前玩家在同一房间,不为好友,返回值为"false";
userId string要确定的玩家UserId range: 无

Precautions

  1. 只在移动端生效
  2. 查询的目标玩家需要和当前玩家在同一房间。否则返回的数据会是"参数格式不正确"。

setUserData

Static setUserData(character, dataString, callback?): void client

将角色形象数据应用至角色

Parameters

character Character用于换装的角色
dataString string返回的数据
callback? BoolResponse设置是否成功的回调 default:无回调
使用示例:创建一个名为AccountExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,发布在真机上运行游戏,会看到一个和玩家形象一致的人形对象
ts
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = await Player.asyncGetLocalPlayer();
        let npc = (await GameObject.asyncSpawn({ guid: "NPC" })) as Character;
        npc.worldTransform.position = new Vector(0, 0, 200);
        AccountService.getUserData(player.userId, 0, async str => {
            await TimeUtil.delaySecond(5);
            AccountService.setUserData(npc, str, isSuccess => {
                player.character.name = isSuccess ? "成功" : "失败";
            })
        });
    }
}
@Component
export default class AccountExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        let player = await Player.asyncGetLocalPlayer();
        let npc = (await GameObject.asyncSpawn({ guid: "NPC" })) as Character;
        npc.worldTransform.position = new Vector(0, 0, 200);
        AccountService.getUserData(player.userId, 0, async str => {
            await TimeUtil.delaySecond(5);
            AccountService.setUserData(npc, str, isSuccess => {
                player.character.name = isSuccess ? "成功" : "失败";
            })
        });
    }
}

uploadData

Static uploadData(character, callback?, index?, openStatus?): void client

上传角色形象资源到服务器 Character, Hair, UpperCloth, LowerCloth, Gloves, Shoe

Parameters

character Character要上传换装数据的角色
callback? BoolResponse VoidResponse设置是否成功的回调 default:默认没有回调
index? number角色位(0-5) default:0,主角资源位
openStatus? number开发状态 default:1,默认是开放状态