> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/modifier-areas.md).

# 头像修饰区域

在场景中的某个区域内隐藏头像、隐藏姓名标签，或禁用玩家护照

化身在 Decentraland 中穿越各个场景时，其行为和外观始终保持一致。不过，你可以在场景中的某个区域添加一个 `AvatarModifierArea` ，以影响玩家化身进入该区域时的行为。

{% hint style="danger" %}
**❗警告**\
请将场景中使用的 `AvatarModifierArea` 数量限制在几个以内。使用过多可能会对性能造成显著影响。
{% endhint %}

### 放置化身修改区域

添加一个包含 `AvatarModifierArea` 组件的实体，并使用 `Transform` 组件设置该实体的位置。

```ts
const entity = engine.addEntity()

AvatarModifierArea.create(entity, {
	area: Vector3.create(4, 3, 4),
	modifiers: [AvatarModifierType.AMT_HIDE_AVATARS],
	excludeIds: []
})

Transform.create(entity, {
	position: Vector3.create(8, 0, 8),
})
```

创建 `AvatarModifierArea` 组件时，必须提供以下内容：

* `area`：模式修改区域的大小
* `modifiers`：列出要在该区域应用的修改器的数组。此属性使用 `AvatarModifierType` 枚举中的值。

支持的修改器包括：

* `AvatarModifierType.AMT_HIDE_AVATARS`
* `AvatarModifierType.AMT_DISABLE_PASSPORTS`
* `AvatarModifierType.AMT_HIDE_NAMETAGS`

AvatarModifierArea `的所有效果` 仅在其区域范围内生效。玩家走出该区域后，行为会恢复正常。

一个 `AvatarModifierArea` 只会影响区域内的玩家。玩家进入该区域不会影响区域外其他玩家所看到的内容。

一个 `AvatarModifierArea` 的效果会针对每位玩家在本地计算。你可以让某个 `AvatarModifierArea` 只存在于部分玩家的场景中，而不出现在其他玩家的场景中。例如，你可以制作一个“马可·波罗”游戏：场景中只有一名玩家拥有一个能隐藏其他所有玩家的修改区域。其他玩家的本地场景版本中没有此修改区域，因此他们仍能正常看到彼此。

如果该区域会隐藏化身，那么本地场景版本中没有此区域的玩家仍会正常看到所有化身，即使那些玩家自己看到的是隐藏状态。拥有此区域的玩家进入区域后，会发现自己和所有其他化身都受到该区域的影响。

{% hint style="warning" %}
**📔 注意**：化身修改区域会受其所属实体的 Transform 组件的 *位置* 和 *旋转* 影响，但不受 *缩放*.
{% endhint %}

{% hint style="warning" %}
**📔 注意**： `AvatarModifierArea`组件必须通过以下方式导入

> `import { AvatarModifierArea } from "@dcl/sdk/ecs"`

有关如何轻松处理这些内容，请参阅 [导入](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/ru-men/coding-scenes.md#imports) 。
{% endhint %}

### 隐藏化身

当玩家走进一个 `AvatarModifierArea` 且该区域带有 `AvatarModifierType.AMT_HIDE_AVATARS` 修饰符时，玩家的化身将不再渲染。这既适用于玩家处于第三人称视角时，也适用于其他玩家走进该区域时。

```ts
const entity = engine.addEntity()

AvatarModifierArea.create(entity, {
	area: Vector3.create(4, 3, 4),
	modifiers: [AvatarModifierType.AMT_HIDE_AVATARS],
	excludeIds: []
})

Transform.create(entity, {
	position: Vector3.create(8, 0, 8),
})
```

这样一来，你就可以在场景中用任意自定义化身替换 Decentraland 的默认化身。请参阅 [替换玩家的化身](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/npc-avatars.md#replace-the-players-avatar)。请注意，如果你希望看到其他玩家使用自定义化身，就需要自行处理玩家位置的同步。

### 禁用护照弹窗

当玩家走进一个 `AvatarModifierArea` 且该区域带有 `AvatarModifierType.AMT_DISABLE_PASSPORTS` 修饰符时，点击该玩家不再会打开显示玩家简介、物品栏等信息的护照界面。

```ts
const entity = engine.addEntity()

AvatarModifierArea.create(entity, {
	area: Vector3.create(4, 3, 4),
	modifiers: [AvatarModifierType.AMT_DISABLE_PASSPORTS],
	excludeIds: []
})

Transform.create(entity, {
	position: Vector3.create(8, 0, 8),
})
```

这在某些游戏中特别有用，因为意外打开该界面可能会打断游戏流程，例如多人射击游戏。

### 隐藏名称标签

当玩家走进一个 `AvatarModifierArea` 且该区域带有 `AvatarModifierType.AMT_HIDE_NAMETAGS` 修饰符时，玩家的名称标签会被隐藏，而化身本身仍然可见。

```ts
const entity = engine.addEntity()

AvatarModifierArea.create(entity, {
	area: Vector3.create(4, 3, 4),
	modifiers: [AvatarModifierType.AMT_HIDE_NAMETAGS],
	excludeIds: []
})

Transform.create(entity, {
	position: Vector3.create(8, 0, 8),
})
```

这适用于舞台、演示或脚本化场景：你可以隐藏玩家名称标签，同时保留化身本身。例如，在演出期间，你可能希望画面简洁，让化身保持可见，同时避免悬浮的名称分散观众注意力。

`AMT_HIDE_NAMETAGS` 还会隐藏通过 [`AvatarNametag`](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/avatar-nametags.md) 组件添加的任何自定义铭牌。

{% hint style="info" %}
**💡 提示**: `AMT_HIDE_AVATARS` 会连同化身一起隐藏名称标签，因此使用 `AMT_HIDE_NAMETAGS` 时无需再添加 `AMT_HIDE_AVATARS`。只有在希望隐藏名称标签、但保留化身可见时，才使用 `AMT_HIDE_NAMETAGS` 。
{% endhint %}

你可以在同一个区域中，将 `AMT_HIDE_NAMETAGS` 与其他修饰符（例如 `AMT_DISABLE_PASSPORTS` ）组合使用：

```ts
const entity = engine.addEntity()

AvatarModifierArea.create(entity, {
	area: Vector3.create(4, 3, 4),
	modifiers: [AvatarModifierType.AMT_HIDE_NAMETAGS, AvatarModifierType.AMT_DISABLE_PASSPORTS],
	excludeIds: []
})

Transform.create(entity, {
	position: Vector3.create(8, 0, 8),
})
```

{% hint style="info" %}
**💡 提示**：只有当玩家的头部或躯干位于区域内时，名称标签才会被隐藏。如果区域太矮，玩家双重跳跃越过该区域，名称标签就会短暂重新出现。请确保区域足够高，以覆盖玩家可能的移动范围。
{% endhint %}

### 排除化身

你可以将玩家 ID 添加到修饰符区域的 `excludeIds` 属性中的数组，以排除指定玩家，使其不受该区域修饰符的影响。

此示例会隐藏区域内的所有化身，但具有特定 ID 的玩家除外。例如，在现场活动中，你可以用它只显示舞台上的活动主持人，并隐藏跳上舞台的其他玩家。

```ts
const entity = engine.addEntity()

AvatarModifierArea.create(entity, {
	area: Vector3.create(4, 3, 4),
	modifiers: [AvatarModifierType.AMT_HIDE_AVATARS],
	excludeIds: ['0xx1...', '0xx2...'],
})

Transform.create(entity, {
	position: Vector3.create(8, 0, 8),
})
```

{% hint style="warning" %}
**📔 注意**：确保玩家 ID 全部使用小写字母。必要时使用 `.toLowerCase()` 。
{% endhint %}

修改器区域在每位玩家的实例上本地运行。排除 ID 列表可以因玩家而异。下面的示例中，每位玩家都会在隐藏头像的修改器中排除自己的 ID，因此每个人都能看到自己的头像，而看不到其他人的头像。

```ts
import { getPlayer } from '@dcl/sdk/src/players'

export function main() {
	let userData = getPlayer()
	if (!userData) return

	const entity = engine.addEntity()

	AvatarModifierArea.create(entity, {
		area: Vector3.create(16, 5, 16),
		modifiers: [AvatarModifierType.AMT_HIDE_AVATARS],
		excludeIds: [userData.userId],
	})

	Transform.create(entity, {
		position: Vector3.create(8, 0, 8),
	})
}
```

{% hint style="danger" %}
**❗警告**\
如果排除 ID 列表会定期变化（例如根据玩家进入或离开某个区域进行调整），请确保列表保持有序。对数组执行 `.sort()` ，使列表每次传入时顺序都相同。这样只会计算列表中的变更。否则，这可能会对场景性能造成显著影响。

```ts
AvatarModifierArea.create(entity, {
	area: Vector3.create(16, 5, 16),
	modifiers: [AvatarModifierType.AMT_HIDE_AVATARS],
	excludeIds: myAvatarList.sort(),
})
```

{% endhint %}

### 重叠区域与运行时变更

每个头像对每种效果都只有一个开/关标志，而不会记录它当前身处多少个区域。每个区域都会写入同一个标志，最后一次写入会生效。以下两种情况容易让人措手不及：

* **离开一个区域会让头像重新显示，即使它仍在另一个区域内。** 走出一个隐藏头像的小区域后，头像会重新显示，即使覆盖该位置的场景级隐藏区域仍然生效。
* **如果某个区域的 `excludeIds` 中列出了某位玩家，那么该区域在玩家进入时会将其设为“可见”**，且仅针对该区域自身列出的每种效果。这会覆盖玩家同时身处其中的“隐藏所有人”区域的设置。

尽量让每个头像一次只处于一个区域中。如果区域确实会重叠，请确保它们对每种效果的设置一致。

**就地移除修改器不会撤销其效果。** 更改区域的字段时，客户端会重新应用当前列出的修改器。移除某个修改器后，不会有任何机制恢复该效果，因此清空 `modifiers`，或将 `AMT_HIDE_AVATARS` 替换为 `AMT_HIDE_NAMETAGS`，都会导致头像继续处于隐藏状态。就地添加修改器则有效。

若要将区域切换为另一组修改器，请在同一帧中删除实体并创建替代实体：

```ts
import { AvatarModifierArea, AvatarModifierType, engine, Entity, Transform } from '@dcl/sdk/ecs'
import { Vector3 } from '@dcl/sdk/math'

// 替换区域，而不是就地编辑其修改器
function setModifiers(oldArea: Entity, modifiers: AvatarModifierType[]): Entity {
	engine.removeEntity(oldArea) // 这也会重新显示区域内的所有头像

	const area = engine.addEntity()
	Transform.create(area, { position: Vector3.create(8, 1, 8) })
	AvatarModifierArea.create(area, {
		area: Vector3.create(16, 5, 16),
		modifiers,
		excludeIds: [],
	})
	return area
}
```

### 调试修改器区域

仅凭代码很难准确判断修改器区域覆盖了场景中的哪些部分。视觉反馈有助于确认它们的位置是否正确。

若要检查 `AvatarModifierArea` 或 `CameraModeArea`的位置，请为承载该组件的实体添加 `MeshRenderer` 带有一个 `box` 形状的组件，并将缩放设置为与修饰器区域的 `area` 相同的大小。

{% hint style="warning" %}
**📔 注意**：修饰器区域不受变换的 `scale` 属性影响，其大小由其 `area` 属性决定。
{% endhint %}

```ts
const entity = engine.addEntity()
const areaSize = Vector3.create(8, 3, 8)

AvatarModifierArea.create(entity, {
	area: areaSize,
	modifiers: [AvatarModifierType.AMT_HIDE_AVATARS],
  	excludeIds: []
})

Transform.create(entity, {
	position: Vector3.create(8, 0, 8),
	scale: areaSize,
})

MeshRenderer.setBox(entity)
Material.setPbrMaterial(entity, {
	albedoColor: Color4.create(0.5, 0.5, 0.5, 0.5),
})
```

要激活修饰器区域的效果，玩家的头部或躯干必须进入该区域。仅玩家的脚进入区域不会生效。请确保玩家无法轻易通过跳跃避开该区域。

{% hint style="warning" %}
**📔 注意**：整个区域应位于场景边界之内。
{% endhint %}

{% hint style="info" %}
**💡 提示**：示例请参见 [`9,99-modifier-areas`](https://github.com/decentraland/sdk7-test-scenes/tree/main/scenes/9,99-modifier-areas) 测试场景，该场景将 `AvatarModifierArea` 与运行时动态修改的 `excludeIds`结合使用；以及 [`10,99-avatar-modifier-hide-nametags`](https://github.com/decentraland/sdk7-test-scenes/tree/main/scenes/10,99-avatar-modifier-hide-nametags)，该场景单独演示了 `AMT_HIDE_NAMETAGS` 修饰器。
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/modifier-areas.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
