Search Unity

[Solved] How to move dialogs around for interactive tutorial?

Discussion in 'UGUI & TextMesh Pro' started by asperatology, Aug 12, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    This is what I'm trying to do for an interactive tutorial:



    The first thing the tutorial will show is a dialog message in the center of the game area (top). When the player clicks on the button, the button activates a button action I registered with the script that moves the dialog to the next position (bottom), along with new dialog text messages.

    This is the first step I would like to do before I expand with more dialogs and text messages.

    This is my current incomplete code (note the comments):

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. namespace Tutorial {
    7.     public class TutorialDialog : MonoBehaviour {
    8.         public RectTransform dialogTransform;
    9.         public GameObject textComponent;
    10.         public GameObject mainDialog;
    11.         public List<string> dialogueTexts;
    12.         public List<RectTransform> dialogPositions;
    13.         public int stringIterator;
    14.  
    15.         void Start() {
    16.             if (this.textComponent == null) {
    17.                 Debug.LogError("Text: Something is wrong.");
    18.             }
    19.             if (this.mainDialog == null) {
    20.                 Debug.LogError("Dialog: Something is wrong.");
    21.             }
    22.             this.dialogTransform = this.mainDialog.GetComponent<RectTransform>();
    23.             this.dialogueTexts = new List<string>();
    24.             this.dialogPositions = new List<RectTransform>();
    25.             StringInitialize();
    26.             DialogPositionInitialize();
    27.             this.stringIterator = 0;
    28.             SetText();
    29.             SetPosition();
    30.         }
    31.  
    32.         void OnGUI() {
    33.  
    34.         }
    35.  
    36.         public void OnTutorialButtonClick() {
    37.             if (this.stringIterator < this.dialogueTexts.Count) {
    38.                 this.stringIterator++;
    39.                 SetText();
    40.                 SetPosition();
    41.             }
    42.         }
    43.  
    44.         private void StringInitialize() {
    45.             this.dialogueTexts.Add("Hello world.");
    46.             this.dialogueTexts.Add("Goodbye world.");
    47.             this.dialogueTexts.Add("Test.");
    48.             this.dialogueTexts.Add("Test 1.");
    49.             this.dialogueTexts.Add("Complete.");
    50.         }
    51.  
    52.         private void DialogPositionInitialize() {
    53.             //Should be easy enough to do. Correct me if I'm wrong:
    54.             /*
    55.            
    56.             1. Copy the values in the Rect Transform of the dialog placed at where I wanted it to be in the Editor.
    57.             2. Paste the values here.
    58.             3. Add new Rect Transforms to the list.
    59.             4. Repeat.
    60.            
    61.             */
    62.         }
    63.  
    64.         private void SetText() {
    65.             if (this.stringIterator < this.dialogueTexts.Count) {
    66.                 Text text = this.textComponent.GetComponent<Text>();
    67.                 text.text = this.dialogueTexts[this.stringIterator];
    68.             }
    69.         }
    70.  
    71.         private void SetPosition() {
    72.             //Completely not sure how to "set" dialog position from Rect Transform (list) to Rect Transform (dialog).
    73.             if (this.stringIterator < this.dialogPositions.Count) {
    74.                 this.dialogTransform = this.dialogPositions[this.stringIterator];
    75.             }
    76.         }
    77.     }
    78. }
    79.  
    Because I'm so used to using GameObject.transform.position, the first time using the UI system is like a whole new Unity feature I was never exposed to before.
    • Does anyone know how to set the dialog positions?
    • Do I need to use OnGUI() for this? I included it in the code, but I'm not sure if I need to.
    Thanks in advance.
     
  2. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I was told about "anchoredPosition" and "anchoredPosition3D" via PM, which is used similarly to Transform.position. It now works as intended. Here's the code:

    Code (CSharp):
    1.         private void SetPosition() {
    2.             //Completely not sure how to "set" dialog position from Rect Transform (list) to Rect Transform (dialog).
    3.             if (this.stringIterator < this.dialogPositions.Count) {
    4.                 this.mainDialog.GetComponent<RectTransform>().anchoredPosition = this.dialogPositions[this.stringIterator];
    5.             }
    6.         }
    7.  
    8.         private void DialogPositionInitialize() {
    9.             //Should be easy enough to do:
    10.             /*
    11.          
    12.             1. Copy the values in the Rect Transform of the dialog placed at where I wanted it to be.
    13.             2. Paste the values here.
    14.             3. Add new Rect Transforms to the list.
    15.             4. Repeat.
    16.          
    17.             */
    18.             this.dialogPositions.Add(new Vector2(461.5f, -267));
    19.             this.dialogPositions.Add(new Vector2(700.8f, -115.5f));
    20.         }
    Therefore, this is solved.
     
    Senshi likes this.
  3. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    To answer your second question, OnGUI() is the legacy GUI system and has nothing to do with uGUI. You don't need the method at all if it's empty.
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Okay, thanks for pointing it out. I'll go ahead and remove the OnGUI().