虚拟形象编辑器服务可让您在体验中访问并对用户的虚拟形象进行更改。虚拟形象编辑器服务还可以访问用户的库存和市场,为用户保存装扮并购买虚拟形象物品到用户的帐户。
我们建议使用游戏内的虚拟形象编辑器实现虚拟形象编辑器服务,以获得完整的角色定制体验。查看 简单的虚拟形象编辑器示范 参考地点,获取这个精选能的示例。
要开始使用虚拟形象编辑器服务,您必须先请求访问用户的道具。获得访问权限后,您可以执行以下操作:
请求访1) 使用权 2)通行证 3)访问权限
要开始访问用户的道具,您需要提示用户通过 PromptAllowInventoryReadAccess() 允许访问。您需要在每个会话中执行此请求一次。
使用以下代码示例启动访问提示并倾听用户响应:
local AvatarEditorService = game:GetService("AvatarEditorService")AvatarEditorService:PromptAllowInventoryReadAccess()local result = AvatarEditorService.PromptAllowInventoryReadAccessCompleted:Wait()if result == Enum.AvatarPromptResult.Success then-- 访问已授权!end
用户收到以下提示:

一旦用户接受提示,AvatarEditorService 可以开始访问用户的道具。
阅读用户道具
一旦用户获得访问权限,你可以使用 函数阅读他们的库存,提供一个阵列来进行过滤。该函数返回包含用户拥有物品的 InventoryPages 对象。
使用以下代码示例打印用户道具中的特定配件列表:
local AvatarEditorService = game:GetService("AvatarEditorService")AvatarEditorService:PromptAllowInventoryReadAccess()local result = AvatarEditorService.PromptAllowInventoryReadAccessCompleted:Wait()if result == Enum.AvatarPromptResult.Success then-- 访问已授权!local assetTypes = {Enum.AvatarAssetType.BackAccessory,Enum.AvatarAssetType.ShoulderAccessory,Enum.AvatarAssetType.WaistAccessory}local pagesObject = AvatarEditorService:GetInventory(assetTypes)local currentPage = pagesObject:GetCurrentPage()for _, item in currentPage doprint(item)endend
在市场上搜索
AvatarEditorService 包括可让您搜索 Roblox 目录的函数和事件。要搜索,提供您的查询带有一个或多个以下属性的 CatalogSearchParams 对象:
属性 | 描述 |
---|---|
资产类型 | 一个枚列 Enum.AvatarAssetType 如 Enum.AvatarAssetType.BackAccessory。 |
包装类型 | 一个枚列 Enum.BundleType 如 Enum.BundleType.BodyParts。 |
类别过滤器 | 描述各种目录类别,例如“精选”或“社区创作”的 Enum.CatalogCategoryFilter 。默认值为 Enum.CatalogCategoryFilter.None |
最高价格 | 一个整数描述要过过滤器的最大价格。 |
最低价格 | 描述要过过滤器的最低价格的整数。默认情况下,MinPrice 是 0。 |
搜索关键字 | 用于查询目录中项目描述的字符串。 |
排序类型 | 描述如何排序结果的 Enum.CatalogSortType 。默认值为 Enum.CatalogSortType.Relevance 。 |
包含关闭销售 | 描述搜索结果是否包含特价物品的 boolean 值。默认值为 false。 |
创建者ID | 一个整数来指定给定的创建者。您可以使用用户ID或组ID。 |
创建者名称 | 用于搜索特定创建创作者创建的项目的字符串。你可以使用用户名或组名。 |
以下代码示例构建了一个 CatalogSearchParams 对象为 返回 和 肩部 资产类型,并通过一个 SearchCatalog() 调用传递:
local AvatarEditorService = game:GetService("AvatarEditorService")local catalogSearchParams = CatalogSearchParams.new()local assetTypes = {Enum.AvatarAssetType.BackAccessory,Enum.AvatarAssetType.ShoulderAccessory}catalogSearchParams.AssetTypes = assetTypeslocal pagesObject =--该函数返回包含结果的目录页对象。AvatarEditorService:SearchCatalog(catalogSearchParams)local currentPage = pagesObject:GetCurrentPage()for _, item in currentPage doprint(item)end
保存虚拟形象和装扮
当与游戏内的虚拟形象编辑器一起使用时,AvatarEditorService 可以保存并更新虚拟形象物品和服装到 Roblox 平台。用户在保存虚拟形象或服装时不会收到未拥有的目录物品。
任何 HumanoidDescription 都可以保存到用户的当前虚拟形象中,使用 PromptSaveAvatar() 。这可能包括:
- 你使用现有目录项目建造的预定义虚拟形象配置。
- 用户通过游戏虚拟形象编辑器选择的任何配置。

由于 AvatarEditorService:PromptSaveAvatar() 不产生结果,因此您可以通过收听 AvatarEditorService.PromptSaveAvatarCompleted 事件来获得结果。
以下代码将使用 HumanoidDescription 保存当前 PromptSaveAvatar() 并检查是否成功 AvatarEditorService.PromptSaveAvatarCompleted 事件:
local AvatarEditorService = game:GetService("AvatarEditorService")local Players = game:GetService("Players")local player = Players.LocalPlayerlocal character = player.Character or player.CharacterAdded:Wait()local humanoid = character:WaitForChild("Humanoid")local currentDescription = humanoid:GetAppliedDescription()AvatarEditorService:PromptSaveAvatar(currentDescription, humanoid.RigType)local result = AvatarEditorService.PromptSaveAvatarCompleted:Wait()if result == Enum.AvatarPromptResult.Success then-- 已保存虚拟形象!end
要将任何 HumanoidDescription 保存为服装(不会覆盖用户当前的虚拟形象),请使用 AvatarEditorService:PromptCreateOutfit() 。

一旦调用,您可以通过收听 AvatarEditorService:PromptCreateOutfit() 事件来获取结果 AvatarEditorService.PromptCreateOutfitCompleted 。
以下代码示例创建了一个装扮,使用 AvatarEditorService:PromptCreateOutfit() 并监听成功的 AvatarEditorService.PromptCreateOutfitCompleted 事件:
local AvatarEditorService = game:GetService("AvatarEditorService")local Players = game:GetService("Players")local player = Players.LocalPlayerlocal character = player.Character or player.CharacterAdded:Wait()local humanoid = character:WaitForChild("Humanoid")local currentDescription = humanoid:GetAppliedDescription()AvatarEditorService:PromptCreateOutfit(currentDescription, humanoid.RigType)local result = AvatarEditorService.PromptCreateOutfitCompleted:Wait()if result == Enum.AvatarPromptResult.Success then-- 装扮已保存!end
购买物品
当保存虚拟形象或使用目录物品的服装时,用户不会收到任何他们未拥有的物品。保存虚拟形象或服装之前,检查用户是否拥有资产 MarketplaceService:PlayerOwnsAsset() ,并提供购买物品的选择 MarketplaceService:PromptPurchase() 。
如果你不想实现物品购买,你可以允许用户通过 AvatarEditorService:PromptSetFavorite() 收藏非拥有物品。