-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example of using action markup processing to make inline events
- Loading branch information
Showing
12 changed files
with
1,490 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.