Made trivia questions work

This commit is contained in:
Pasha Bibko
2026-01-15 11:42:10 +00:00
parent fd61dcb2f4
commit 4eea7f8836
9 changed files with 271 additions and 8 deletions

View File

@@ -0,0 +1,202 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2940310635561786684
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1414490042639617302}
- component: {fileID: 5291019555413885228}
- component: {fileID: 3825336479657745611}
- component: {fileID: 7082475543525880575}
m_Layer: 5
m_Name: TextButtonPrefab
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1414490042639617302
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2940310635561786684}
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: 4463935307105001289}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 160, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &5291019555413885228
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2940310635561786684}
m_CullTransparentMesh: 1
--- !u!114 &3825336479657745611
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2940310635561786684}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, 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_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!114 &7082475543525880575
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2940310635561786684}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 3825336479657745611}
m_OnClick:
m_PersistentCalls:
m_Calls: []
--- !u!1 &8163815796456593488
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4463935307105001289}
- component: {fileID: 7219155264245777851}
- component: {fileID: 7690044542930724813}
m_Layer: 5
m_Name: Text (Legacy)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &4463935307105001289
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8163815796456593488}
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: 1414490042639617302}
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!222 &7219155264245777851
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8163815796456593488}
m_CullTransparentMesh: 1
--- !u!114 &7690044542930724813
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8163815796456593488}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, 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_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 20
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 2
m_MaxSize: 40
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: hello

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: dab7e63431a2b974cab5c2a6117a037c
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -12,4 +12,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7a072103fe40445691e28d3eb4509d1b, type: 3}
m_Name: AdvertRegistry
m_EditorClassIdentifier:
m_Adverts: []
m_Adverts:
- {fileID: 21300000, guid: a16f47cf69d6cac4aa6283e32df807b4, type: 3}
- {fileID: 21300000, guid: cfef516196b2c1d4b9a2ec018cfe6a53, type: 3}
- {fileID: 21300000, guid: e34bfec25f7947f4e936bb8adc01a8f0, type: 3}
- {fileID: 21300000, guid: 76058b5a9bf44a9478ab31804ab9d2df, type: 3}
- {fileID: 21300000, guid: 8841696fbfeb4cb49aa2e65c44c8f2a4, type: 3}
- {fileID: 21300000, guid: 210bdc7c31d9da3408d6c71877e64d7b, type: 3}

View File

@@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: f25c8c9754c2489da8616c79fe173e69, type: 3}
m_Name: ImageRegistry
m_EditorClassIdentifier:
<Registered>k__BackingField:
m_Registered:
- <Images>k__BackingField:
- {fileID: 1904163225, guid: 0a3345be1f4bf77488a64ce0d8ea336c, type: 3}
- {fileID: -1747164198, guid: 0a3345be1f4bf77488a64ce0d8ea336c, type: 3}

View File

@@ -12,4 +12,5 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9631aba76ad0408d8819b9f979caae5e, type: 3}
m_Name: PrefabRegistry
m_EditorClassIdentifier:
<ImagePrefab>k__BackingField: {fileID: 0}
<ImagePrefab>k__BackingField: {fileID: 1810675969122322477, guid: 792c010445685104eac99ebe5c3831ce,
type: 3}

View File

@@ -542,10 +542,6 @@ MonoBehaviour:
m_EditorClassIdentifier:
<CanvasRect>k__BackingField: {fileID: 1539476655}
<GameCanvas>k__BackingField: {fileID: 1539476654}
<ImagePrefab>k__BackingField: {fileID: 1810675969122322477, guid: 792c010445685104eac99ebe5c3831ce,
type: 3}
<Images>k__BackingField: {fileID: 11400000, guid: 157e9466a4474c147bbd98676e5b1d22,
type: 2}
--- !u!1 &1683319378
GameObject:
m_ObjectHideFlags: 0

View File

@@ -6,6 +6,8 @@ namespace InterfaceOff
public class PrefabRegistry : ScriptableObject
{
[field: SerializeField] public GameObject ImagePrefab { get; private set; }
[field: SerializeField] public GameObject TextButtonPrefab { get; private set; }
public static PrefabRegistry Instance { get; private set; }
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]

View File

@@ -1,23 +1,42 @@
using System.IO;
using UnityEngine;
using UnityEngine.UI;
namespace InterfaceOff
{
[System.Serializable] public struct TriviaQuestion
{
/* Disables name warnings because of JSON serialization */
// ReSharper disable once InconsistentNaming
public string question;
// ReSharper disable once InconsistentNaming
public string[] choices;
// ReSharper disable once InconsistentNaming
public string answer;
}
[System.Serializable] public struct TriviaSet
{
/* Disables name warnings because of JSON serialization */
// ReSharper disable once InconsistentNaming
public TriviaQuestion[] questions;
}
public class TriviaWindow : WindowBase
{
private static TriviaSet Trivia;
private static Vector2[] Positions { get; } =
{
new(-110, 30f),
new(110f, 30f),
new(-110, -60),
new(110f, -60)
};
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void LoadTriviaSet()
@@ -30,10 +49,36 @@ namespace InterfaceOff
public override void OnWindowInstantiation()
{
/* Fetches a random question and sets it as the title */
TriviaQuestion question = Trivia.questions[Random.Range(0, Trivia.questions.Length)];
Components.InfoText.text = question.question;
Components.SetWidth(500); // Larger width is needed for this component that others
Components.SetWidth(500);
/* Adds the answer boxes */
for (int i = 0; i < 4; i++)
{
/* Creates the gameobject and fetches the needed components */
GameObject go = Instantiate(PrefabRegistry.Instance.TextButtonPrefab, transform);
Text text = go.GetComponentInChildren<Text>();
Button button = go.GetComponent<Button>();
/* Assigns needed info to the components */
text.text = question.choices[i];
RectTransform buttonTransform = button.GetComponent<RectTransform>();
buttonTransform.anchoredPosition = Positions[i];
buttonTransform.sizeDelta = new Vector2(200, 60);
/* Adds a lambda function to the button press to detect answers */
bool isCorrectButton = string.Equals(question.choices[i], question.answer);
button.onClick.AddListener(() =>
{
if (isCorrectButton)
{
DestroyWindow();
}
});
}
}
}
}

View File

@@ -9,6 +9,10 @@ namespace InterfaceOff
public override void OnWindowInstantiation()
{
/* Adds a random advert to the display */
Components.WindowImage.sprite = AdvertRegistry.GetRandomAdvert();
Components.WindowImage.color = new Color(1, 1, 1, 1);
/* Calculates a random health value */
m_Health = Random.Range(2, 6);
}