使用示例
实际场景示例,展示情感系统的运行:危险响应、脚本化问候、自适应分支、分析和创作工作流。
最后更新于
这有帮助吗?
这有帮助吗?
using Convai.Modules.Emotion.Components;
using UnityEngine;
public sealed class HazardZoneTrigger : MonoBehaviour
{
[SerializeField] private ConvaiEmotionController instructorEmotion;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Trainee"))
instructorEmotion.SetEmotionOverride("fear", 0.9f);
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Trainee"))
instructorEmotion.ClearEmotionOverride();
}
}using Convai.Modules.Emotion.Components;
using UnityEngine;
public sealed class WelcomeSequenceController : MonoBehaviour
{
[SerializeField] private ConvaiEmotionController greeterEmotion;
private void Start()
{
// 从第一帧起保持温和、欢迎的表情
greeterEmotion.LockEmotion("joy", 0.65f);
}
public void OnWelcomeSequenceComplete()
{
// 释放锁定,让角色自然地对对话作出反应
greeterEmotion.UnlockEmotion();
}
}using Convai.Domain.Embodiment.Readings;
using Convai.Modules.Emotion.Components;
using UnityEngine;
using UnityEngine.Events;
public sealed class EmotionBranchDirector : MonoBehaviour
{
[SerializeField] private ConvaiEmotionController patientEmotion;
[SerializeField] private float distressThreshold = 0.6f;
[SerializeField] private float sustainedDistressSeconds = 4f;
[SerializeField] private UnityEvent onDistressBranchTriggered;
private bool _branchTriggered;
private void Update()
{
if (_branchTriggered) return;
EmotionReading reading = patientEmotion.Current;
bool isSadOrFearful = reading.DominantLabel is "sadness" or "fear"
&& reading.DominantScore >= distressThreshold;
if (isSadOrFearful && reading.DominantHoldSeconds >= sustainedDistressSeconds)
{
_branchTriggered = true;
onDistressBranchTriggered.Invoke();
}
}
}using Convai.Domain.DomainEvents.Runtime;
using Convai.Runtime.Components;
using System.Collections.Generic;
using UnityEngine;
public sealed class EmotionSessionLogger : MonoBehaviour
{
[SerializeField] private ConvaiManager convaiManager;
private readonly List<string> _emotionLog = new();
private void OnEnable()
{
convaiManager.Events.OnCharacterEmotionChanged += HandleEmotionChanged;
}
private void OnDisable()
{
convaiManager.Events.OnCharacterEmotionChanged -= HandleEmotionChanged;
}
private void HandleEmotionChanged(CharacterEmotionChanged e)
{
string entry = $"[{e.Timestamp:HH:mm:ss.fff}] {e.CharacterId}: {e.Emotion} (scale {e.Intensity})";
_emotionLog.Add(entry);
Debug.Log(entry);
}
public IReadOnlyList<string> GetLog() => _emotionLog;
}