0
\$\begingroup\$

Before this, I try to insert an image in a tooltip and it was already done. But right now, I want to try to insert a sound in the tooltip. What I want to do is when I point my cursor to the object it will display the image and also the sound at the same time.

My question:

  1. Can sound insert in the tooltip?

  2. Can I insert the code of sound in the same place as the image function?

-----TooltipManager Script--------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class TooltipManager: MonoBehaviour
{
    public static TooltipManager _instance;

    public Image imageComponent;
   
    // public Sound soundComponent;

    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            _instance = this;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        Cursor.visible = true;
        gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Input.mousePosition;
    }

    public void SetAndShowToolTip(Sprite image)
    {
        gameObject.SetActive(true);
        imageComponent.sprite = image;
    }

    public void HideToolTip()
    {
         gameObject.SetActive(false);
    }

}

So can I insert the sound code inside the SetAndShowToolTip? or do I need to create a new function?

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

The short answer is yes to both 1) and 2).

But if you should do it that simple is another matter for a different reason. It is a matter and ease of control of your sound. Unlike a tooltip that is requested by the user by clicking/ pointing on it, sound might not be. The usual way is to have an option somewhere to turn of sound effects somewhere in your settings. Now if all your toolmanager is doing is soundeffect.play() you will have a harder time to integrate the control over the sound itself like if it should play or how loud it should be.

\$\endgroup\$
3
  • \$\begingroup\$ so I need to do something so that the user can click on the object to turn on the sound so that the sound will not be mixed? like this right \$\endgroup\$ Commented May 29, 2023 at 10:54
  • \$\begingroup\$ Not on the object where the tooltip is located. Basically every game has in one way or another some options where you can turn on or off the sound. As a start you can of course right away play your sound effect but later in the game you might need to rework all those instances of playing sound if it turns out that they still play even if the user/ player didn't want to have sound active \$\endgroup\$
    – Zibelas
    Commented May 29, 2023 at 11:08
  • \$\begingroup\$ ahh I understand, what I want to do is the sound on the specific object only. \$\endgroup\$ Commented May 29, 2023 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .