Search Unity

[SOLVED] Adding sprite into UI dialogue system

Discussion in 'Scripting' started by Soarino, Nov 22, 2017.

  1. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    Similar question to the one i asked previously but i need help in implementing in actors into a dialogue system e.g. when player talks to character picture of character shows with dialogue box.

    Here's my scripts so far my dialogue manager
    Code (CSharp):
    1. public class DialogueManager : MonoBehaviour {
    2.  
    3.     public Text nameText;
    4.     public Text dialogueText;
    5.     public Sprite actorSprite;
    6.     public GameObject fpsController;
    7.     public Animator animator;
    8.  
    9.     private Queue<string> sentences;
    10.  
    11.     // Use this for initialization
    12.     void Start() {
    13.         sentences = new Queue<string>();
    14.  
    15.     }
    16.  
    17.     public void StartDialogue(Dialogue dialogue)
    18.     {
    19.         fpsController.GetComponent<FirstPersonController>().enabled = false;
    20.         Cursor.visible = true;
    21.         Cursor.lockState = CursorLockMode.None;
    22.  
    23.         animator.SetBool("IsOpen", true);
    24.  
    25.         Debug.Log("Starting conversation with " + dialogue.name);
    26.  
    27.         actorSprite = dialogue.actor;
    28.         nameText.text = dialogue.name;
    29.         sentences.Clear();
    30.  
    31.         foreach (string sentence in dialogue.sentences)
    32.         {
    33.             sentences.Enqueue(sentence);
    34.         }
    35.  
    36.         DisplayNextSentence();
    37.  
    38.     }
    39.  
    40.     public void DisplayNextSentence ()
    41.     {
    42.         if (sentences.Count == 0)
    43.         {
    44.             EndDialogue();
    45.             return;
    46.         }
    47.  
    48.         string sentence = sentences.Dequeue();
    49.         dialogueText.text = sentence;
    50.         Debug.Log(sentence);
    51.     }
    52.  
    53.     void EndDialogue()
    54.     {
    55.         animator.SetBool("IsOpen", false);
    56.         Debug.Log("End of Conversation.");
    57.         fpsController.GetComponent<FirstPersonController>().enabled = true;
    58.         Cursor.visible = false;
    59.     }
    60.  
    61. }
    This is my dialogue script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Dialogue {
    7.  
    8.     public Sprite actor;
    9.  
    10.     public string name;
    11.  
    12.     [TextArea(3, 10)]
    13.     public string[] sentences;
    14.  
    15.  
    16. }
    17.  
    This is my dialogue script referenced by manager and the dialogue trigger, i have assigned UI elements in the canvas but the sprite doesn't seem to load properly when engaging in dialogue with a character, ive applied my sprite to my game object.

    Also sorry im new to this!

    Any help is greatly appreciated!
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    When using the UI system, try using Image instead of Sprite.
     
    Soarino likes this.
  3. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    I've tried that and I get an error if I try something like

    "Public Image actor"
     
  4. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    You'll need to add

    Code (csharp):
    1.  
    2. using UnityEngine.UI
    3.  
     
  5. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23


    No dice still doesn't work, im come into the problem of i cannot add the character image to the trigger now.

    And the second arrow is a the placeholder in the canvas if this helps
     
    Last edited: Nov 23, 2017
  6. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    Are you sure that's an image you're trying to drag in? Sure looks like it could be a sprite.
     
    Soarino likes this.
  7. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    I tried converting it back to a default image still doesn't work
     
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Change the image's texture type to a "Sprite (2D and UI)" type. It looks like it's still set to whatever the defaults are.
     
    Soarino likes this.
  9. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    Ive tried doing that and it wont let me put in anything in
     
  10. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    This is what you'd expect to see when you click on an image in the Project view:

    upload_2017-11-23_16-11-29.png

    Don't forget to hit Apply when done.

    Now you can drag this into an Image property in your dialogue script.
     
    Soarino likes this.
  11. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    Got exactly the same setting and still cannot drag it into the dialogue trigger.

     
  12. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    That's strange. :confused:

    As a sanity check, when you click on the little circle button next to the trigger's Actor field, a pop-up menu should appear showing you a list of available images to use. Does it show any of your own images?

    EDIT: Actually, since you have a placeholder "Character" silhouette image that seems to work, can you try dragging that into your trigger "Actor" Image box just to see if that works?
     
    Last edited: Nov 24, 2017
    Soarino likes this.
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I've been watching this thread periodically, and all I have to say is... I hope you get it working! lol :)
     
    Soarino and laxbrookes like this.
  14. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    The silhouette works shows up even if the trigger doesn't have a image and i tried placing my dialogue container into the trigger to see if that works and it did not replace the silhouette unfortunately.




    also what i see when i press the circle.
     
  15. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Hm, for some reason it's showing you the scene tab when you press the circle. If you switch to the "Assets" tab, it should show you a list of all available image assets you could use.
     
    Soarino likes this.
  16. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Your actor variable is of type Image, not of type Sprite. It's searching for an image component. You can't add a sprite into an image variable, otherwise, your variable type needs to be Sprite.

    The image UI component can take a sprite, but it must be applied to the Sprite part of it.

    Actor.Sprite = someSprite.

    So basically, you either make a prefab image, or you have the image somewhere in your scene and then change the sprite part of that image when you want to display a different talking character. I suggest the second method personally. Making several image prefabs is overkill I think for this.

    Note that it says (Image) in the Actor variable slot and not (Sprite)
     
  17. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Right yes, I had a brain fart there.

    Yes, you want to drag the Actor component into your dialogue trigger actor box. Not the image itself.
     
    Soarino likes this.
  18. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    I've changed my dialogue script to

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. [System.Serializable]
    7. public class Dialogue {
    8.  
    9.     public Sprite actor;
    10.  
    11.     public string name;
    12.  
    13.     [TextArea(3, 10)]
    14.     public string[] sentences;
    15.  
    16.  
    17. }
    18.  
    And my manager to

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityStandardAssets.Characters.FirstPerson;
    7.  
    8. public class DialogueManager : MonoBehaviour {
    9.  
    10.  
    11.     public Text nameText;
    12.     public Text dialogueText;
    13.     public Sprite Actor;
    14.     public GameObject fpsController;
    15.     public Animator animator;
    16.  
    17.     private Queue<string> sentences;
    18.  
    19.     // Use this for initialization
    20.     void Start() {
    21.         sentences = new Queue<string>();
    22.  
    23.     }
    24.  
    25.     public void StartDialogue(Dialogue dialogue)
    26.     {
    27.         fpsController.GetComponent<FirstPersonController>().enabled = false;
    28.         Cursor.visible = true;
    29.         Cursor.lockState = CursorLockMode.None;
    30.  
    31.         animator.SetBool("IsOpen", true);
    32.  
    33.         Debug.Log("Starting conversation with " + dialogue.name);
    34.  
    35.         Actor.Sprite = dialogue.actor
    36.         nameText.text = dialogue.name;
    37.         sentences.Clear();
    38.  
    39.         foreach (string sentence in dialogue.sentences)
    40.         {
    41.             sentences.Enqueue(sentence);
    42.         }
    43.  
    44.         DisplayNextSentence();
    45.  
    46.     }

    I am getting the the error:

    Assets/Scripts/DialogueManager.cs(34,15): error CS1061: Type `UnityEngine.Sprite' does not contain a definition for `Sprite' and no extension method `Sprite' of type `UnityEngine.Sprite' could be found. Are you missing an assembly reference?


    Sorry if i didn't understand what you said what you wrote sounded pretty solid

    Also thanks for helping me thus far
     
    Last edited: Nov 24, 2017
  19. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    The Dialogue class should be fine.

    The DialogueManager still needs the Actor to be an Image type. This is where I gave you bad information: You do NOT drag an image (i.e. texture/sprite) into this. Instead, you drag the "Actor" Image instance into this. I'm referring to the UI Image component, which you currently have named as "Actor" in your hierarchy view.

    Think of this as a container for the image, not the image itself. You want a reference to the container so you're able to swap the image inside the container.

    EDIT: See blue arrow

    upload_2017-11-24_15-59-39.png
     
    Soarino likes this.
  20. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    The container is already referenced along with all my other ui elements here in my dialogue manager:




    Any the red arrow in your is point towards the name of the character, each character is assigned a dialogue script.



    I want this place holder silhouette to be replace by this image via the trigger "red arrow"

    These references work for the text of the dialogue because both of the characters have unique names and sentences.

    Also i tried adding a image you can see in the hierarchy called Luka image and applying that to the trigger but the placeholder didnt change
     
    Last edited: Nov 25, 2017
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not going to go over every piece, but when you have this:
    Code (csharp):
    1.  
    2. public Sprite Actor;
    3. // you need an image , too
    4. public Image imageSpot;
    5.  
    6. // later on, when you want to show the sprite (in the image script)
    7. imageSpot.sprite = Actor; // looks like this.
    8.  
     
    Soarino likes this.
  22. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    OK I got confused by the name "Actor" being shared between the two classes.That'll teach me to drink more coffee before posting :D

    Let's take a step back, and start from the beginning.

    You have a DialogueManager. This is where you map in the UI objects. It looks like you have this working for the text. This is also where you map in the Actor Image container. This is what I was (incorrectly) trying to point at with the blue arrow.

    It looks like you have this set up properly in your first screenshot.

    Now, in the second screenshot, for the Dialogue class, you want Actor to be a sprite type. Not the Image type. Change that back to a Sprite, and make sure that shows up in the inspector as (Sprite) not (Image). You should be able to drag an image in.
     
    Soarino likes this.
  23. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    This worked when i implemented this into may code thank you!

    I needed these lines in my dialogue manager

    Code (CSharp):
    1. public Image imageSpot;
    2.  
    3. imageSpot.sprite = dialogue.Actor;
    and then i need this in my dialogue script

    Code (CSharp):
    1.  
    2. public Sprite Actor;
    I just didn't define well in the manager

    I think i didn't explain my problem right in the first place or i didn't explain my system right but ether way thank you so much every who help me with this!
    Im still learning to use C sharp and unity so thank you for your patience with me.
     
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey, glad you finally got it resolved & working :) Very cool.
     
    Soarino likes this.