A profile avatar that remembers its owner

Someone changes their glasses, closes your app, and returns tomorrow. Their profile should open with those same glasses, even while the live 3D face is getting ready.

Read the guide
Noor · Smile

Give each profile its own saved look

Clayfaces stores appearance in a small recipe containing a skin index, shade and the features the person changed. Save that recipe against the stable profile ID your app already uses. A display name can change, and two people on a shared device can choose the same name.

This Swift helper stores separate looks on one device. A missing or unreadable recipe falls back to the default character. If you sync profiles between devices, send the encoded data through your existing profile store as well.

Swift
import Foundation
import Clayfaces

enum ProfileLooks {
    static func load(_ profileID: String) -> AvatarRecipe {
        let key = "profile.avatar." + profileID
        return AvatarRecipe.decode(UserDefaults.standard.data(forKey: key))
            ?? AvatarRecipe()
    }

    static func save(_ look: AvatarRecipe, for profileID: String) throws {
        let key = "profile.avatar." + profileID
        UserDefaults.standard.set(try look.encoded(), forKey: key)
    }
}

Open the editor on the current face

Load the recipe into SwiftUI state when you select a profile. Give ClayfacesView that value and present ClayfacesEditor with a binding to the same state. When an edit is committed, save the updated recipe under the ID of the person being edited.

Use the avatar at a size that lets someone recognize a fringe, freckles or a pair of frames. Keep their name as text beside it. In a long contact list, reserve the live stage for the selected profile so scrolling past a row never has to initialize a 3D player.

Check the return visit

  1. Edit the first account, then open a second account and choose a visibly different look.
  2. Quit and relaunch. Switch between both profiles and check the hairstyle and glasses on each one.
  3. Once a saved recipe is available, call ClayfacesView.preload(appearance:) from the preceding screen. Open the profile and watch its matching saved portrait give way to the live head.

Reference

Related guides

Build a 3D avatar editorShare a character between iOS and web