1

I want to make a common alert window to show information about some info maybe warning, tutorial etc,

I think it will be good if there is a way that the alert widget(mostly just a box border, a close button, the info text and some hint sign stuff) could pop up when there is event received, so in the game I could just shoot the msg through a static method, and the widget could just pop up.

I have no problem to do the event part, but I couldn't find a way to active the gameobject of the widget since it will be deactivated as the game started, and seems couldn't assign itself the event pool.

Is there a way to do that? Or how you deal with the problem?

enter image description here

And here is the event assignment so far I have complete:

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

public class WarningTextUGUIController : MonoBehaviour {

    private Text text;
    // Use this for initialization
    void Start () {
        text = GetComponent<Text>();
        text.text = ApplicationModel.CurrentErrMsg;
        ApplicationModel.OnErrMsg += ChangeErrMsg;
    }

    private void ChangeErrMsg(string msg, bool isNgui)
    {
        if (!isNgui)
        {
            text.text = msg;
            if (!gameObject.transform.parent.parent.gameObject.activeSelf)
            {
                gameObject.transform.parent.parent.localScale = new Vector3(0f, 0f, 0f);
                gameObject.transform.parent.parent.gameObject.SetActive(true);
                LeanTween.scale(gameObject.transform.parent.parent.gameObject, new Vector3(1f, 1f, 1f), 0.2f).setEase(LeanTweenType.easeInBounce);
            }
        }
    }

    private void OnDestroy()
    {
        ApplicationModel.OnErrMsg -= ChangeErrMsg;
    }

}

I must note that things will work if I made the widget active at the beginning of the game, but I have to disable it because it's shouldn't be seen under most circumstances.

armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • Create an EventManager you can subscribe to. When something happens that EventManager is responsible for alerting scripts that subscribed to it. See an example [here](https://stackoverflow.com/questions/42034245/unity-eventmanager-with-delegate-instead-of-unityevent/42034899#42034899) – Programmer Jul 04 '17 at 10:37
  • thanks,I'll try that – armnotstrong Jul 04 '17 at 10:40
  • @Programmer : The problem is that `WarningTextUGUIController` won't be able to subscribe to the EventManager since it is disabled when the app starts. – Hellium Jul 04 '17 at 10:52
  • Hey, @Hellium do you have other solution for this? – armnotstrong Jul 04 '17 at 10:58
  • 1
    The only way I can think about is to leave the gameobject enabled in the SceneView and disable it in the Awake/Start method after you have subscribed to the event. – Hellium Jul 04 '17 at 11:02
  • Put that pop up message in a empty game object and attach event listener to it, so it can enable or disable pop up as needed. – Woltus Jul 04 '17 at 11:02
  • Or instead of enabling and disabling objects just toggle renderers. – Woltus Jul 04 '17 at 11:03
  • @Hellium That's not a problem at-all. This is why there is an `Awake` function. The `Awake` function is called **even** when the script is disabled. OP can just register the event in the `Awake` function. – Programmer Jul 04 '17 at 11:04
  • @Programmer I found the solution you give in other question couldn't meet my needs since I couldn't easily pass parameters to the listener, I found a bypass way to do that: first, make the scale to vector3.zero at `Start()` and then change the scale to make it pop up. – armnotstrong Jul 04 '17 at 11:05
  • 1
    `Awake` is not called if the **gameobject** is disabled. In edit mode, I understand that the "error pop-up" is on the way and may be disabled and it's tedious to disable/enable the gameobject when you edit your game. – Hellium Jul 04 '17 at 11:05
  • 1
    @Programmer, I have tried to subscribe to the event in `Awak()` but It seems don't work, don't know whether have done something wrong :-( frankly I don't know if the script will be ever executed if the gameobject will not be active during the game. – armnotstrong Jul 04 '17 at 11:10
  • @Hellium In your first comment you said if `WarningTextUGUIController` is disable. `WarningTextUGUIController` is a script. I am talking about the script. If the script is disabled, the `Awake` will be called. I am replying to your first comment. Also you can't disable/enable a GameObject, you can deactivate and activate it. You enable and disable scripts. Just saying that so that you won't confuse people in the future. – Programmer Jul 04 '17 at 11:11
  • @armnotstrong I've answered that question before and I am looking for it to link it here but can't find it. You need a way to subscribe to the event. You have **2** options: **1**.Make the Object enabled by default then disable it in the `Awake` function after subscribing to the event. **2**.Modify the `EventManager` script and make manually make it subscribe to the function in the `WarningTextUGUIController` script by searching for it with `GameObject.Find` the `GetComponent. It's really easy. – Programmer Jul 04 '17 at 11:17
  • @Programmer thanks, enable and disable it in the `Awake()` is a good inspire, maybe I should throw it far away and make it back too, so it doesn't block the scene when editing stuff :D – armnotstrong Jul 04 '17 at 11:20
  • I can see that being really annoying when it's blocking the screen. I have another idea. See [this](https://stackoverflow.com/a/44456334/3785314) answer I left, you will see functions I wrote that can be used to find inactive GameObjects by tag, name, or layer. Deactivate the GameObject from the Editor. From the `EventManager` script, use `FindInActiveObjectByName` to find the disabled GameObject then use `GetComponent` to get `WarningTextUGUIController` script from it. Have a public function in the `WarningTextUGUIController` script that you will call to subscribe to the event. – Programmer Jul 04 '17 at 11:35

0 Answers0