Skip to content

Commit

Permalink
Added an example of using action markup processing to make inline events
Browse files Browse the repository at this point in the history
  • Loading branch information
McJones committed Jan 30, 2025
1 parent 7cd768b commit b6c465b
Show file tree
Hide file tree
Showing 12 changed files with 1,490 additions and 21 deletions.
8 changes: 8 additions & 0 deletions Samples~/3D Samples/InlineEvents.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions Samples~/3D Samples/InlineEvents/EmotionEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Threading;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Yarn.Markup;
using Yarn.Unity;

public class EmotionEvent : ActionMarkupHandler
{
private Yarn.Unity.Samples.SimpleCharacterAnimation target;
Dictionary<int, string> emotions;

public override void OnLineDisplayComplete()
{
return;
}

public override void OnLineDisplayBegin(MarkupParseResult line, TMP_Text text)
{
return;
}

public override void OnPrepareForLine(MarkupParseResult line, TMP_Text text)
{
// grab the character attribute
// grab the SimpleCharacterAnimation that matches that name
if (!line.TryGetAttributeWithName("character", out var character))
{
Debug.LogWarning("line has no character");
return;
}
// we need the name of the character so we can find them in the scene
if (!character.Properties.TryGetValue("name", out var name))
{
Debug.LogWarning("character has no name");
return;
}

var emoter = GameObject.Find(name.StringValue);
if (emoter == null)
{
Debug.LogWarning($"scene has no one called {name.StringValue}");
return;
}

target = emoter.GetComponent<Yarn.Unity.Samples.SimpleCharacterAnimation>();
if (target == null)
{
Debug.Log($"{name.StringValue} is not a SimpleCharacterAnimation");
return;
}

Debug.Log($"found a valid target, {name.StringValue}");

emotions = new();

foreach (var attribute in line.Attributes)
{
if (attribute.Name != "emotion")
{
continue;
}

if (!attribute.TryGetProperty("emotion", out var emotionKey))
{
continue;
}
emotions[attribute.Position] = emotionKey.StringValue;
}
}

public override YarnTask OnCharacterWillAppear(int currentCharacterIndex, MarkupParseResult line, CancellationToken cancellationToken)
{
if (target == null)
{
return YarnTask.CompletedTask;
}

if (emotions.TryGetValue(currentCharacterIndex, out var emotion))
{
target.SetFacialExpression(emotion);
}

return YarnTask.CompletedTask;
}
}
2 changes: 2 additions & 0 deletions Samples~/3D Samples/InlineEvents/EmotionEvent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b6c465b

Please sign in to comment.