fix: Question and answers working correctly

This commit is contained in:
Gerard Gascón 2024-01-27 13:53:18 +01:00
parent 11243f1474
commit a3415767cd
4 changed files with 497 additions and 100 deletions

View file

@ -25,12 +25,12 @@ RectTransform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1294452579022797697} m_GameObject: {fileID: 1294452579022797697}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 5034134452723103632} m_Father: {fileID: 709282506909990290}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0}
@ -65,7 +65,141 @@ MonoBehaviour:
m_OnCullStateChanged: m_OnCullStateChanged:
m_PersistentCalls: m_PersistentCalls:
m_Calls: [] m_Calls: []
m_text: New Textadsdfeqasdfeqpweur m_text:
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4278190080
m_fontColor: {r: 0, g: 0, b: 0, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 16
m_fontSizeBase: 16
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_HorizontalAlignment: 1
m_VerticalAlignment: 256
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!1 &1385370767609568079
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5946925061173963520}
- component: {fileID: 2043221702941464055}
- component: {fileID: 6241851731353898668}
m_Layer: 5
m_Name: EmojiText
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &5946925061173963520
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1385370767609568079}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 6867251083966527665}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 16}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &2043221702941464055
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1385370767609568079}
m_CullTransparentMesh: 1
--- !u!114 &6241851731353898668
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1385370767609568079}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text:
m_isRightToLeft: 0 m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
@ -159,12 +293,12 @@ RectTransform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2275331047474166448} m_GameObject: {fileID: 2275331047474166448}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 5034134452723103632} m_Father: {fileID: 709282506909990290}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0}
@ -199,7 +333,7 @@ MonoBehaviour:
m_OnCullStateChanged: m_OnCullStateChanged:
m_PersistentCalls: m_PersistentCalls:
m_Calls: [] m_Calls: []
m_text: Juan m_text:
m_isRightToLeft: 0 m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
@ -268,6 +402,70 @@ MonoBehaviour:
m_hasFontAssetChanged: 0 m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0} m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0} m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!1 &3086284164549867040
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6867251083966527665}
- component: {fileID: 9104390994953202978}
m_Layer: 5
m_Name: Answer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &6867251083966527665
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3086284164549867040}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 7119683377321649236}
- {fileID: 5946925061173963520}
m_Father: {fileID: 5034134452723103632}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &9104390994953202978
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3086284164549867040}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 30649d3a9faa99c48a7b1166b86bf2a0, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Padding:
m_Left: 0
m_Right: 0
m_Top: 0
m_Bottom: 0
m_ChildAlignment: 2
m_Spacing: 0
m_ChildForceExpandWidth: 0
m_ChildForceExpandHeight: 1
m_ChildControlWidth: 1
m_ChildControlHeight: 0
m_ChildScaleWidth: 0
m_ChildScaleHeight: 0
m_ReverseArrangement: 0
--- !u!1 &3730343923350390943 --- !u!1 &3730343923350390943
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -280,7 +478,6 @@ GameObject:
- component: {fileID: 5443779664744389533} - component: {fileID: 5443779664744389533}
- component: {fileID: 7160386019866200041} - component: {fileID: 7160386019866200041}
- component: {fileID: -6056302822753658535} - component: {fileID: -6056302822753658535}
- component: {fileID: 7445523924295813971}
m_Layer: 5 m_Layer: 5
m_Name: Message m_Name: Message
m_TagString: Untagged m_TagString: Untagged
@ -300,8 +497,8 @@ RectTransform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
- {fileID: 158050521503001043} - {fileID: 709282506909990290}
- {fileID: 2778276168877196149} - {fileID: 6867251083966527665}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1} m_AnchorMin: {x: 0, y: 1}
@ -359,15 +556,59 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d8cc3fce6c7f415fa982510d4778f914, type: 3} m_Script: {fileID: 11500000, guid: d8cc3fce6c7f415fa982510d4778f914, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
messageText: {fileID: 5123271222652756937} questionContainer:
realMessageText: {fileID: 6299244945418012692} container: {fileID: 3943447704424611815}
--- !u!114 &7445523924295813971 messageText: {fileID: 5123271222652756937}
realMessageText: {fileID: 6299244945418012692}
answerContainer:
container: {fileID: 3086284164549867040}
messageText: {fileID: 6241851731353898668}
realMessageText: {fileID: 3566725148210905056}
--- !u!1 &3943447704424611815
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 709282506909990290}
- component: {fileID: 5803872089394284702}
m_Layer: 5
m_Name: Question
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &709282506909990290
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3943447704424611815}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 158050521503001043}
- {fileID: 2778276168877196149}
m_Father: {fileID: 5034134452723103632}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &5803872089394284702
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3730343923350390943} m_GameObject: {fileID: 3943447704424611815}
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 30649d3a9faa99c48a7b1166b86bf2a0, type: 3} m_Script: {fileID: 11500000, guid: 30649d3a9faa99c48a7b1166b86bf2a0, type: 3}
@ -387,3 +628,137 @@ MonoBehaviour:
m_ChildScaleWidth: 0 m_ChildScaleWidth: 0
m_ChildScaleHeight: 0 m_ChildScaleHeight: 0
m_ReverseArrangement: 0 m_ReverseArrangement: 0
--- !u!1 &9172270066209890164
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7119683377321649236}
- component: {fileID: 8573787476193218524}
- component: {fileID: 3566725148210905056}
m_Layer: 5
m_Name: RealText
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &7119683377321649236
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9172270066209890164}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 6867251083966527665}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 16}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &8573787476193218524
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9172270066209890164}
m_CullTransparentMesh: 1
--- !u!114 &3566725148210905056
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9172270066209890164}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text:
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4278190080
m_fontColor: {r: 0, g: 0, b: 0, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 16
m_fontSizeBase: 16
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_HorizontalAlignment: 1
m_VerticalAlignment: 256
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 16, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}

View file

@ -2,32 +2,66 @@ using System;
using Messaging.Typer; using Messaging.Typer;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization;
using Utilities; using Utilities;
namespace Messaging { namespace Messaging {
public class Message : MonoBehaviour { public class Message : MonoBehaviour {
public RectTransform RectTransform { private set; get; } public RectTransform RectTransform { private set; get; }
public string Text => messageText.text; public string Text { private set; get; }
public string RealText => realMessageText.text; public string RealText { private set; get; }
public bool IsAnswer { private set; get; }
public bool IsReal { private set; get; }
[SerializeField] private TMP_Text messageText; [SerializeField] private MessageContainer questionContainer;
[SerializeField] private TMP_Text realMessageText; [SerializeField] private MessageContainer answerContainer;
private TextTyper _textTyper;
private TextTyper _realTextTyper;
private Coroutine _typingCoroutine;
private Coroutine _realTypingCoroutine;
private void Awake() { private void Awake() {
RectTransform = GetComponent<RectTransform>(); RectTransform = GetComponent<RectTransform>();
_textTyper = new TextTyper(messageText);
_realTextTyper = new TextTyper(realMessageText); questionContainer.InitializeTypers();
answerContainer.InitializeTypers();
} }
public void SetMessageText(string text, bool animate) => public void SetMessageText(string text, bool animate, bool isAnswer) {
SetText(ref _typingCoroutine, messageText, _textTyper, text, animate); Text = text;
public void SetMessageRealText(string text, bool animate) => IsAnswer = isAnswer;
SetText(ref _realTypingCoroutine, realMessageText, _realTextTyper, text, animate); IsReal = false;
if (isAnswer) {
SetText(ref answerContainer.TypingCoroutine, answerContainer.messageText, answerContainer.TextTyper,
text, animate);
answerContainer.container.SetActive(true);
questionContainer.container.SetActive(false);
} else {
SetText(ref questionContainer.TypingCoroutine, questionContainer.messageText,
questionContainer.TextTyper, text, animate);
questionContainer.container.SetActive(true);
answerContainer.container.SetActive(false);
}
}
public void SetMessageRealText(string text, bool animate, bool isAnswer) {
RealText = text;
IsAnswer = isAnswer;
IsReal = true;
if (isAnswer) {
SetText(ref answerContainer.RealTypingCoroutine, answerContainer.realMessageText,
answerContainer.RealTextTyper, text, animate);
answerContainer.container.SetActive(true);
questionContainer.container.SetActive(false);
} else {
SetText(ref questionContainer.RealTypingCoroutine, questionContainer.realMessageText,
questionContainer.RealTextTyper, text, animate);
questionContainer.container.SetActive(true);
answerContainer.container.SetActive(false);
}
}
private void SetText(ref Coroutine routine, TMP_Text tmpText, TextTyper typer, string text, bool animate) { private void SetText(ref Coroutine routine, TMP_Text tmpText, TextTyper typer, string text, bool animate) {
this.EnsureCoroutineStopped(ref routine); this.EnsureCoroutineStopped(ref routine);
@ -37,5 +71,25 @@ namespace Messaging {
else else
tmpText.text = text; tmpText.text = text;
} }
[Serializable]
private class MessageContainer {
public GameObject container;
public TMP_Text messageText;
public TMP_Text realMessageText;
public Coroutine TypingCoroutine;
public Coroutine RealTypingCoroutine;
public TextTyper TextTyper;
public TextTyper RealTextTyper;
public void InitializeTypers() {
TextTyper = new TextTyper(messageText);
RealTextTyper = new TextTyper(realMessageText);
container.SetActive(false);
}
}
} }
} }

View file

@ -19,43 +19,49 @@ namespace Messaging {
string[] fLines = textAsset.text.Split(':'); string[] fLines = textAsset.text.Split(':');
int i = 0; int i = 0;
while (Application.isPlaying) { while (Application.isPlaying) {
Debug.Log(textAsset.text); bool isAnswer = i % 2 != 0;
CreateMessage(MessageParser.SplitMessage(fLines[i % 2]).Key);
++i; CreateMessage(MessageParser.SplitMessage(fLines[i % 2]).Key, isAnswer);
await Task.Delay(2000); await Task.Delay(2000);
CreateRealMessage(MessageParser.SplitMessage(fLines[i % 2]).Value, isAnswer);
await Task.Delay(2000);
++i;
} }
} }
public void CreateMessage(string message) { public void CreateMessage(string message, bool isAnswer) {
ModifyMessageText(message, _messages.Length - 1); ModifyMessageText(message, _messages.Length - 1, isAnswer);
}
public void CreateRealMessage(string message) {
ModifyRealMessageText(message, _messages.Length - 1);
} }
private void ModifyMessageText(string message, int index) { public void CreateRealMessage(string message, bool isAnswer) {
ModifyPreviousMessageText(_messages[index].Text, index - 1); ModifyRealMessageText(message, _messages.Length - 1, isAnswer);
_messages[index].SetMessageText(message, true);
}
private void ModifyRealMessageText(string message, int index) {
ModifyPreviousRealMessageText(_messages[index].Text, index - 1);
_messages[index].SetMessageRealText(message, true);
} }
private void ModifyPreviousMessageText(string message, int index) { private void ModifyMessageText(string message, int index, bool isAnswer) {
ModifyPreviousMessage(index);
_messages[index].SetMessageText(message, true, isAnswer);
}
private void ModifyRealMessageText(string message, int index, bool isAnswer) {
_messages[index].SetMessageRealText(message, true, isAnswer);
}
private void ModifyPreviousMessage(int index) {
ModifyPreviousMessage(_messages[index].Text, _messages[index].RealText, index - 1,
_messages[index].IsAnswer, _messages[index].IsReal);
_messages[index].SetMessageText("", false, _messages[index].IsAnswer);
_messages[index].SetMessageRealText("", false, _messages[index].IsAnswer);
}
private void ModifyPreviousMessage(string message, string realMessage, int index, bool isAnswer, bool isReal) {
if (index < 0) return; if (index < 0) return;
ModifyPreviousMessageText(_messages[index].Text, index - 1); ModifyPreviousMessage(_messages[index].Text, _messages[index].RealText, index - 1,
_messages[index].IsAnswer, _messages[index].IsReal);
_messages[index].SetMessageText(message, false); _messages[index].SetMessageText(message, false, isAnswer);
} _messages[index].SetMessageRealText(realMessage, false, isAnswer);
private void ModifyPreviousRealMessageText(string message, int index) {
if (index < 0) return;
ModifyPreviousRealMessageText(_messages[index].RealText, index - 1);
_messages[index].SetMessageRealText(message, false);
} }
} }
} }

View file

@ -7,8 +7,7 @@ namespace Messaging.Typer {
public class TextTyper { public class TextTyper {
private readonly TMP_Text _textBox; private readonly TMP_Text _textBox;
private static readonly Color32 Clear = new(0, 0, 0, 0); private const float SecondsPerCharacter = 0.07f;
private const float SecondsPerCharacter = 1f / 60f;
public TextTyper(TMP_Text textBox) { public TextTyper(TMP_Text textBox) {
_textBox = textBox; _textBox = textBox;
@ -18,63 +17,26 @@ namespace Messaging.Typer {
float timeOfLastCharacter = 0; float timeOfLastCharacter = 0;
TMP_TextInfo textInfo = _textBox.textInfo; TMP_TextInfo textInfo = _textBox.textInfo;
foreach (TMP_MeshInfo meshInfer in textInfo.meshInfo) {
if (meshInfer.vertices == null) continue;
for (int j = 0; j < meshInfer.vertices.Length; j++) {
meshInfer.vertices[j] = Vector3.zero;
}
}
_textBox.maxVisibleCharacters = 0;
_textBox.text = processedMessage; _textBox.text = processedMessage;
_textBox.ForceMeshUpdate(); _textBox.ForceMeshUpdate();
Color32[][] originalColors = new Color32[textInfo.meshInfo.Length][];
for (int i = 0; i < originalColors.Length; i++) {
Color32[] theColors = textInfo.meshInfo[i].colors32;
originalColors[i] = new Color32[theColors.Length];
Array.Copy(theColors, originalColors[i], theColors.Length);
}
int charCount = textInfo.characterCount; int charCount = textInfo.characterCount;
int visibleCharacterIndex = 0; int visibleCharacterIndex = 0;
while (true) { while (true) {
if (ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) { if (ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) {
if (visibleCharacterIndex <= charCount) { if (visibleCharacterIndex <= charCount) {
if (visibleCharacterIndex < charCount && visibleCharacterIndex++;
ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) { _textBox.maxVisibleCharacters = visibleCharacterIndex;
visibleCharacterIndex++; timeOfLastCharacter = Time.unscaledTime;
timeOfLastCharacter = Time.unscaledTime; if (visibleCharacterIndex == charCount) {
if (visibleCharacterIndex == charCount) { onFinish?.Invoke();
onFinish?.Invoke(); break;
}
} }
} }
} }
for (int j = 0; j < charCount; j++) {
TMP_CharacterInfo charInfo = textInfo.characterInfo[j];
if (!charInfo.isVisible) continue;
int vertexIndex = charInfo.vertexIndex;
int materialIndex = charInfo.materialReferenceIndex;
Color32[] destinationColors = textInfo.meshInfo[materialIndex].colors32;
Color32 theColor = j < visibleCharacterIndex ? originalColors[materialIndex][vertexIndex] : Clear;
destinationColors[vertexIndex + 0] = theColor;
destinationColors[vertexIndex + 1] = theColor;
destinationColors[vertexIndex + 2] = theColor;
destinationColors[vertexIndex + 3] = theColor;
}
_textBox.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
for (int i = 0; i < textInfo.meshInfo.Length; i++) {
TMP_MeshInfo theInfo = textInfo.meshInfo[i];
theInfo.mesh.vertices = theInfo.vertices;
_textBox.UpdateGeometry(theInfo.mesh, i);
}
if (visibleCharacterIndex == charCount) {
break;
}
yield return null; yield return null;
} }
} }