Advertisement
GauHelldragon

Unity Dialog Controller Script

Mar 22nd, 2022
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class DialogControl : MonoBehaviour
  7. {
  8.     public float textXLeft = -235.0f;
  9.     public float textXRight = 235.0f;
  10.     public int state = 0;
  11.     public Text dialogText;
  12.     public GameObject[] dialogObjects;
  13.  
  14.  
  15.  
  16.     public void Next()
  17.     {
  18.         state++;
  19.         switch (state)
  20.         {
  21.             case 1:
  22.                 MoveDialogObjects(textXRight);
  23.                 dialogText.text = "I know, right?!";
  24.                 break;
  25.             case 2:
  26.                 MoveDialogObjects(textXLeft);
  27.                 dialogText.text = "Tottaly.";
  28.                 break;
  29.             case 3:
  30.                 dialogText.text = "Hey did you hear about...";
  31.                 break;
  32.             case 4:
  33.                 MoveDialogObjects(textXRight);
  34.                 dialogText.text = "wow!!";
  35.                 break;
  36.             default:
  37.                 HideAllDialog();
  38.                 break;
  39.         }
  40.  
  41.     }
  42.  
  43.     public void MoveDialogObjects(float newX)
  44.     {
  45.         foreach (GameObject dialogObject in dialogObjects)
  46.         {
  47.  
  48.             // Moves dialogObject to newX and uses the existing Y
  49.             RectTransform rectTransform = dialogObject.GetComponent<RectTransform>();
  50.             if (rectTransform != null)
  51.             {              
  52.                 float y = rectTransform.localPosition.y;
  53.                 rectTransform.localPosition = new Vector3(newX, y);
  54.             }
  55.         }
  56.     }
  57.  
  58.     public void HideAllDialog()
  59.     {
  60.         foreach (GameObject dialogObject in dialogObjects)
  61.         {
  62.             dialogObject.SetActive(false);
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement