Example #1
0
    /// <summary>
    /// Create a tip in the game
    /// </summary>
    /// <param name="name">tip uniquie name</param>
    /// <param name="forseShow">is it should be shown without the "can show" param?</param>
    public static void CreateTip(string name, bool forseShow = false)
    {
        if (m_sCanShow == false && forseShow == false)
        {
            return;
        }

        LearningTipData data;
        MainScript      ms = Camera.main.GetComponent <MainScript>();

        try
        {
            data = _sAllTips[name];
        }
        catch (KeyNotFoundException)
        {
            Debug.LogError("Tip " + name + " set as the next, but was not found");
            return;
        }

        AbstractObject depend     = null;
        GameObject     dependency = null;

        if (data.m_dependency != null && data.m_dependency.Length > 0)
        {
            depend = AbstractObject.GetByName(data.m_dependency);
            if (depend == null)
            {
                dependency = GameObject.Find(data.m_dependency);
            }
        }

        Vector3 coord = data.m_position;

        if (depend != null)
        {
            coord += depend.m_thisObject.transform.position;
            depend.m_thisObject.IconClick();
        }

        coord.z = -3; //learning tips should be higher then icons;

        var LearningTipObj = Instantiate(Camera.main.GetComponent <MainScript>().m_LearingTipPrefab);

        LearningTipObj.name = "Learning Tip: " + data.m_name;
        LearningTipObj.transform.position = coord;

        coord.z = Camera.main.transform.position.z;
        Camera.main.transform.position = coord;

        LearningTip thisObj = LearningTipObj.GetComponent <LearningTip>();

        thisObj._data = data;

        var Canvas = LearningTipObj.transform.Find("Canvas tips");

        var txtObj = Canvas.Find("TipText").gameObject;
        var txt    = txtObj.GetComponent <TextMeshProUGUI>();

        txt.text = data.m_text;


        SetButton(Canvas, "PrevButton", thisObj.OnPrevious, data.m_previous != null);
        SetButton(Canvas, "NextButton", thisObj.OnNext, data.m_next != null && data.m_next.Length > 0);
        SetButton(Canvas, "OkButton", thisObj.OnClose, !(data.m_next != null && data.m_next.Length > 0));

        var toggle = thisObj.m_showTips.GetComponent <Toggle>();

        toggle.isOn = m_sCanShow;
        toggle.onValueChanged.AddListener(delegate { thisObj.OnShowTip(); });


        if (depend != null || dependency != null)
        {
            bool hasCanvas = false;
            //TODO:yellow outline
            if (depend != null)
            {
                hasCanvas = SetTargetObject(thisObj, data.m_targetObject, depend.m_thisObject.transform);
            }
            else
            {
                thisObj._targetObject = dependency;
            }

            Rect rectB = new Rect();

            if (data.m_yellowBox != null && data.m_yellowBox.width > 0)
            {
                rectB         = data.m_yellowBox;
                rectB.center += (Vector2)thisObj._targetObject.transform.position;
            }
            else
            {
                if (data.m_isItUI || hasCanvas)
                {
                    var rect = thisObj._targetObject.transform as RectTransform;
                    rectB        = rect.rect;
                    rectB.center = thisObj._targetObject.transform.position;
                }
                else
                {
                    var render = thisObj._targetObject.GetComponent <Renderer>();
                    rectB.size   = render.bounds.size;
                    rectB.center = render.bounds.center;
                }
            }

            data.m_outline = Instantiate(ms.m_OutlinePrefab).GetComponent <Outline>();
            data.m_outline.transform.position = new Vector3();
            data.m_outline.m_OutlineRect      = rectB;
            data.m_outline.mb_IsItCanvas      = thisObj._data.m_isItUI;
        }
        //var outline = thisObj.m_targetObject.AddComponent<Outline>();
        //outline.OutlineMode = Outline.Mode.OutlineAll;
        //outline.OutlineColor = Color.yellow;
        //outline.OutlineWidth = 5f;
    }