AgentSkillsCN

unity-prefab

创建、实例化并管理预制体。使用prefab_instantiate_batch来生成2个及以上实例。

SKILL.md
--- frontmatter
name: unity-prefab
description: "Create, instantiate, and manage prefabs. Use prefab_instantiate_batch for spawning 2+ instances."

Unity Prefab Skills

BATCH-FIRST: Use prefab_instantiate_batch when spawning 2+ prefab instances.

Skills Overview

Single ObjectBatch VersionUse Batch When
prefab_instantiateprefab_instantiate_batchSpawning 2+ instances

No batch needed:

  • prefab_create - Create prefab from scene object
  • prefab_apply - Apply instance changes to prefab
  • prefab_unpack - Unpack prefab instance

Skills

prefab_create

Create a prefab from a scene GameObject.

ParameterTypeRequiredDescription
gameObjectNamestringNo*Source object name
instanceIdintNo*Instance ID
savePathstringYesPrefab save path

Returns: {success, prefabPath, sourceObject}

prefab_instantiate

Instantiate a prefab into the scene.

ParameterTypeRequiredDefaultDescription
prefabPathstringYes-Prefab asset path
namestringNoprefab nameInstance name
x, y, zfloatNo0Position
parentNamestringNonullParent object

Returns: {success, name, instanceId, prefabPath, position}

prefab_instantiate_batch

Instantiate multiple prefabs in one call.

ParameterTypeRequiredDescription
itemsarrayYesArray of instantiation configs

Item properties: prefabPath, name, x, y, z, rotX, rotY, rotZ, scaleX, scaleY, scaleZ, parentName

python
unity_skills.call_skill("prefab_instantiate_batch", items=[
    {"prefabPath": "Assets/Prefabs/Enemy.prefab", "x": 0, "z": 0, "name": "Enemy_01"},
    {"prefabPath": "Assets/Prefabs/Enemy.prefab", "x": 2, "z": 0, "name": "Enemy_02"},
    {"prefabPath": "Assets/Prefabs/Enemy.prefab", "x": 4, "z": 0, "name": "Enemy_03"}
])

prefab_apply

Apply instance changes back to the prefab asset.

ParameterTypeRequiredDescription
gameObjectNamestringNo*Prefab instance name
instanceIdintNo*Instance ID

Returns: {success, gameObject, prefabPath}

prefab_unpack

Unpack a prefab instance (break prefab connection).

ParameterTypeRequiredDefaultDescription
gameObjectNamestringNo*-Prefab instance name
instanceIdintNo*-Instance ID
completelyboolNofalseUnpack all nested prefabs

Returns: {success, gameObject, mode}


Example: Efficient Enemy Spawning

python
import unity_skills

# BAD: 10 API calls for 10 enemies
for i in range(10):
    unity_skills.call_skill("prefab_instantiate",
        prefabPath="Assets/Prefabs/Enemy.prefab",
        name=f"Enemy_{i}",
        x=i * 2
    )

# GOOD: 1 API call for 10 enemies
unity_skills.call_skill("prefab_instantiate_batch", items=[
    {"prefabPath": "Assets/Prefabs/Enemy.prefab", "name": f"Enemy_{i}", "x": i * 2}
    for i in range(10)
])

Best Practices

  1. Organize prefabs in dedicated folders
  2. Use prefabs for repeated objects
  3. Apply changes to update all instances
  4. Unpack only when unique modifications needed
  5. Use batch instantiation for level generation