AgentSkillsCN

unity-component

在游戏对象上添加、移除并配置组件。对于2个及以上对象,可使用*_batch技能。

SKILL.md
--- frontmatter
name: unity-component
description: "Add, remove, and configure components on GameObjects. Use *_batch skills for 2+ objects."

Unity Component Skills

BATCH-FIRST: Use *_batch skills when operating on 2+ objects to reduce API calls from N to 1.

Skills Overview

Single ObjectBatch VersionUse Batch When
component_addcomponent_add_batchAdding to 2+ objects
component_removecomponent_remove_batchRemoving from 2+ objects
component_set_propertycomponent_set_property_batchSetting on 2+ objects

Query Skills (no batch needed):

  • component_list - List all components on an object
  • component_get_properties - Get component property values

Single-Object Skills

component_add

Add a component to a GameObject.

ParameterTypeRequiredDescription
namestringNo*GameObject name
instanceIdintNo*Instance ID (preferred)
pathstringNo*Hierarchy path
componentTypestringYesComponent type name

*At least one identifier required

Returns: {success, gameObject, componentType, added}

component_remove

Remove a component from a GameObject.

ParameterTypeRequiredDescription
namestringNo*GameObject name
instanceIdintNo*Instance ID
componentTypestringYesComponent type to remove

Returns: {success, gameObject, componentType, removed}

component_list

List all components on a GameObject.

ParameterTypeRequiredDescription
namestringNo*GameObject name
instanceIdintNo*Instance ID

Returns: {success, gameObject, instanceId, components: [string]}

component_set_property

Set a component property value.

ParameterTypeRequiredDescription
namestringNo*GameObject name
instanceIdintNo*Instance ID
componentTypestringYesComponent type
propertyNamestringYesProperty to set
valueanyYesNew value

Returns: {success, gameObject, componentType, property, oldValue, newValue}

component_get_properties

Get all properties of a component.

ParameterTypeRequiredDescription
namestringNo*GameObject name
instanceIdintNo*Instance ID
componentTypestringYesComponent type

Returns: {success, gameObject, componentType, properties: {name: value}}


Batch Skills

component_add_batch

Add components to multiple objects.

python
unity_skills.call_skill("component_add_batch", items=[
    {"name": "Enemy1", "componentType": "Rigidbody"},
    {"name": "Enemy2", "componentType": "Rigidbody"},
    {"name": "Enemy3", "componentType": "Rigidbody"}
])

component_remove_batch

Remove components from multiple objects.

python
unity_skills.call_skill("component_remove_batch", items=[
    {"instanceId": 12345, "componentType": "BoxCollider"},
    {"instanceId": 12346, "componentType": "BoxCollider"}
])

component_set_property_batch

Set properties on multiple objects.

python
unity_skills.call_skill("component_set_property_batch", items=[
    {"name": "Enemy1", "componentType": "Rigidbody", "propertyName": "mass", "value": 2.0},
    {"name": "Enemy2", "componentType": "Rigidbody", "propertyName": "mass", "value": 2.0}
])

Common Component Types

Physics

TypeDescription
RigidbodyPhysics simulation
BoxColliderBox collision
SphereColliderSphere collision
CapsuleColliderCapsule collision
MeshColliderMesh-based collision
CharacterControllerCharacter movement

Rendering

TypeDescription
MeshRendererRender meshes
SkinnedMeshRendererAnimated meshes
SpriteRenderer2D sprites
LineRendererDraw lines
TrailRendererMotion trails

Audio

TypeDescription
AudioSourcePlay sounds
AudioListenerReceive audio

UI

TypeDescription
CanvasUI container
ImageUI images
TextUI text (legacy)
ButtonClickable button

Example: Efficient Physics Setup

python
import unity_skills

# BAD: 6 API calls
unity_skills.call_skill("component_add", name="Box1", componentType="Rigidbody")
unity_skills.call_skill("component_add", name="Box2", componentType="Rigidbody")
unity_skills.call_skill("component_add", name="Box3", componentType="Rigidbody")
unity_skills.call_skill("component_set_property", name="Box1", componentType="Rigidbody", propertyName="mass", value=2.0)
unity_skills.call_skill("component_set_property", name="Box2", componentType="Rigidbody", propertyName="mass", value=2.0)
unity_skills.call_skill("component_set_property", name="Box3", componentType="Rigidbody", propertyName="mass", value=2.0)

# GOOD: 2 API calls
unity_skills.call_skill("component_add_batch", items=[
    {"name": "Box1", "componentType": "Rigidbody"},
    {"name": "Box2", "componentType": "Rigidbody"},
    {"name": "Box3", "componentType": "Rigidbody"}
])
unity_skills.call_skill("component_set_property_batch", items=[
    {"name": "Box1", "componentType": "Rigidbody", "propertyName": "mass", "value": 2.0},
    {"name": "Box2", "componentType": "Rigidbody", "propertyName": "mass", "value": 2.0},
    {"name": "Box3", "componentType": "Rigidbody", "propertyName": "mass", "value": 2.0}
])

Best Practices

  1. Add colliders before Rigidbody for physics
  2. Use component_list to verify additions
  3. Check property names with component_get_properties first
  4. Some properties are read-only (will fail to set)
  5. Use full type names for custom scripts (e.g., "MyNamespace.MyScript")