Advertisement
Powerhoof

DialogWithFont

Aug 28th, 2022 (edited)
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace PowerTools.Quest
  6. {
  7.  
  8. // Add this component to the DialogText prefab and add the fonts you want to use in the 'fonts' list. Then set the id in the character
  9. public class DialogWithFont: MonoBehaviour, ISpeechGui
  10. {
  11.     [System.Serializable]
  12.     public class FontStyle
  13.     {  
  14.         public Font m_font = null;
  15.         public int m_size = 0;
  16.     }
  17.  
  18.     // List of available fonts
  19.     [SerializeField] FontStyle[] m_fonts = null;
  20.    
  21.     TextMesh m_mesh = null;
  22.     QuestText m_questText = null;
  23.  
  24.     void Awake()
  25.     {
  26.         m_mesh = GetComponent<TextMesh>();
  27.         m_questText = GetComponent<QuestText>();
  28.     }
  29.  
  30.     public void StartSay(Character character, string text, int currLineId, bool backgroundSpeech)
  31.     {
  32.         if ( m_fonts == null || m_fonts.IsIndexValid(character.FontId) == false )
  33.             return;
  34.         FontStyle style = m_fonts[character.FontId];
  35.        
  36.         if ( style.m_font != null && style.m_font != m_mesh.font )
  37.         {          
  38.             m_mesh.font = style.m_font;
  39.             m_mesh.GetComponent<MeshRenderer>().materials = new Material[] { style.m_font.material };
  40.             if ( style.m_size > 0 )
  41.                 m_mesh.fontSize = style.m_size;
  42.  
  43.             // have to rebuild outline too, hacky since there's no "reset" function
  44.             TextOutline tmp = m_questText.GetOutline();        
  45.             m_questText.SetOutline(new TextOutline());
  46.             m_questText.SetOutline(tmp);           
  47.         }      
  48.  
  49.         m_questText.SetText(text);
  50.     }
  51.  
  52.     public void EndSay(Character character)
  53.     {
  54.     }
  55. }
  56.  
  57.  
  58. public partial interface ICharacter
  59. {
  60.     // changes the font the character uses for dialog
  61.     int FontId {get;set;}
  62. }
  63.  
  64. public partial class Character
  65. {
  66.     // Having this as an int or string means you can change it and it'll work with the save system.
  67.     int m_fontId = -1;
  68.  
  69.     public int FontId { get=>m_fontId; set {m_fontId = value;}}
  70. }
  71.  
  72. }
  73.  
Tags: PowerQuest
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement