Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Created July 16, 2015 05:11
Show Gist options
  • Save grimmdev/988d07b100001440e47d to your computer and use it in GitHub Desktop.
Save grimmdev/988d07b100001440e47d to your computer and use it in GitHub Desktop.
Custom PowerUI Live HTML with a small mod/edit to add support for NodeCanvas's Dialogue Trees.
using UnityEngine;
using System.Collections;
using PowerUI;
using NodeCanvas.DialogueTrees;
public class DialoguePowerUI : UnityEngine.MonoBehaviour {
public TextAsset HtmlFile;
#if UNITY_EDITOR
private LiveHtml Reloader;
#endif
private IDialogueActor currentActor;
private MultipleChoiceRequestInfo currentOptions;
private Element Dialogue;
private Element Choices;
private static System.Action<int> Choice;
// OnEnable is called when the game starts, or when the manager script component is enabled.
void OnEnable () {
print ("UI Enabled.");
// Optional. This allows PowerUI to easily work with screens of varying resolution.
UI.Resolution=new ResolutionInfo(1f);
if(HtmlFile==null){
Debug.Log("Please provide a HTML file for your UI.");
#if UNITY_EDITOR
Reloader.Stop();
Reloader=null;
#endif
}else{
// Write the html:
UI.Html=HtmlFile.text;
#if UNITY_EDITOR
// Get the full asset path:
string fullPath=UnityEditor.AssetDatabase.GetAssetPath(HtmlFile);
// Create the loader:
Reloader=new LiveHtml(fullPath,OnHtmlChanged);
#endif
}
DialogueTree.OnDialogueStarted += OnDialogueStarted;
DialogueTree.OnDialoguePaused += OnDialoguePaused;
DialogueTree.OnDialogueFinished += OnDialogueFinished;
DialogueTree.OnSubtitlesRequest += OnSubtitlesRequest;
DialogueTree.OnMultipleChoiceRequest += OnMultipleChoiceRequest;
Dialogue = UI.document.getElementByAttribute ("class", "Dialogue");
Choices = UI.document.getElementByAttribute("class","Choices");
}
/// <summary>Called when the .html file is saved.</summary>
void OnHtmlChanged(string path,string html){
print ("UI Changed.");
UI.Html=html;
}
// OnDisable is called when the manager script component is disabled. You don't need this.
void OnDisable () {
print ("UI Disabled.");
UI.Destroy();
#if UNITY_EDITOR
Reloader.Stop();
Reloader=null;
#endif
}
void OnDialogueStarted(DialogueTree dialogue){
print ("On Dialogue Started.");
//We could do something here...
}
void OnDialoguePaused(DialogueTree dialogue){
print ("On Dialogue Paused.");
OnDialogueFinished(dialogue);
}
void OnDialogueFinished(DialogueTree dialogue){
print ("On Dialogue Over.");
if (currentActor != null)
currentActor.speech = null;
currentOptions = null;
Dialogue.innerHTML = "";
}
//Function with same name as the event is called when the event is dispatched by the Dialogue Tree
void OnSubtitlesRequest(SubtitlesRequestInfo info){
print ("Subtitles Requested.");
currentActor = info.actor;
StatementProcessor.ProcessStatement(info.statement, info.actor, info.Continue);
print ("Subtitles Requested.");
Dialogue.innerHTML = info.actor.name + ":" + info.statement;
}
//A function with the same name as the subscribed Event is called when the event is dispatched
void OnMultipleChoiceRequest(MultipleChoiceRequestInfo optionsInfo){
print ("Multiple Choice Request.");
currentOptions = optionsInfo;
if (currentOptions.options.Count > 0) {
Choices.innerHTML = "";
foreach (var option in currentOptions.options) {
Element ele=UI.document.createElement("<input type='button' onclick='DialoguePowerUI.ClickedOption' id='" + option.Value + "' value=''/>");
ele["value"] = option.Key.ToString();
Choices.AppendNewChild(ele);
}
Choice = currentOptions.SelectOption;
currentOptions = null;
}
}
public static void ClickedOption(UIEvent e){
print ("Button Clicked.");
int c = int.Parse (e.target ["id"]);
Choice (c);
UI.document.getElementByAttribute ("class", "Choices").innerHTML = "";
return;
}
}
<!-- Time for a little CSS! -->
<style type='text/css'>
.Dialogue {
text-align: center;
height: 100%;
width: 100%;
v-align: middle;
}
.Choices {
position: absolute;
bottom: 0px;
left: 0px;
width: 100%;
height: 100px;
}
</style>
<div class='Dialogue'>
</div>
<div class='Choices'>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment