Files
Master-Arbeit-Tom-Hempel/Unity-Master/Assets/Scripts/README_AvatarSync.md
2025-09-21 22:42:26 +02:00

3.4 KiB

Avatar Synchronization Scripts

This system allows you to synchronize Ready Player Me avatars in Unity by reading all transforms, rotations, and blendshapes from one avatar and applying them to another.

Files Created

  • AvatarDataWriter.cs - Reads avatar data and saves to JSON
  • AvatarDataReader.cs - Reads JSON data and applies to target avatar

Prerequisites

  • Newtonsoft.Json package must be installed in Unity
  • Install via Package Manager: Window → Package Manager → "+" → Add package by name → com.unity.nuget.newtonsoft-json

Setup Instructions

1. Writer Setup (Source Avatar)

  1. Add the AvatarDataWriter component to your source Ready Player Me avatar GameObject
  2. In the inspector:
    • Avatar Root: Assign the root transform of your source avatar (if empty, uses the GameObject this script is on)
    • File Name: Keep default avatar_sync_data.json or change as needed
    • Write Every Frame: Check for real-time sync, uncheck for rate-limited updates
    • Update Rate: If not writing every frame, set desired updates per second (default: 60)

2. Reader Setup (Target Avatar)

  1. Add the AvatarDataReader component to your target Ready Player Me avatar GameObject
  2. In the inspector:
    • Target Avatar Root: Assign the root transform of your target avatar
    • File Name: Must match the writer's file name
    • Read Every Frame: Check for real-time sync, uncheck for rate-limited updates
    • Update Rate: If not reading every frame, set desired updates per second (default: 60)
    • Smooth Transitions: Check for smooth interpolation, uncheck for instant updates
    • Transition Speed: Speed of smooth transitions (default: 10)
    • Show Debug Info: Check to see debug messages in console

How It Works

AvatarDataWriter

  • Captures all transform data (position, rotation, scale) from the entire avatar hierarchy
  • Captures all blendshape weights from SkinnedMeshRenderer components
  • Saves this data as JSON to Assets/Sync-Files/[filename].json
  • Updates every frame or at a specified rate

AvatarDataReader

  • Reads the JSON file created by the writer
  • Maps transform paths to find corresponding bones on the target avatar
  • Applies transform data and blendshape weights to the target avatar
  • Supports smooth transitions or instant updates

Important Notes

  1. File Path: Data is saved to Assets/Sync-Files/ directory (created automatically)
  2. Avatar Structure: Both avatars should have similar bone hierarchies for best results
  3. Performance: Writing/reading every frame is intensive - consider rate limiting for better performance
  4. Blendshapes: Only works if both avatars have compatible blendshape setups
  5. Transform Paths: The system uses transform hierarchy paths to match bones between avatars

Troubleshooting

  • Missing transforms: Check debug info to see which transforms couldn't be found
  • Poor performance: Reduce update rate or disable "Every Frame" options
  • Blendshapes not working: Ensure both avatars have SkinnedMeshRenderer components with blendshapes
  • File not found: Check that the Sync-Files directory exists and the file names match

Example Usage

  1. Put AvatarDataWriter on your player avatar
  2. Put AvatarDataReader on your sync avatar
  3. Play the scene - the sync avatar should now copy all movements and expressions from the player avatar in real-time!