首先定义提示框的 属性

protected MessageStrings messageName;
[Tooltip("动画时长")] public float tweenDuration;
[Tooltip("停留时长")] public float stayDuration;
[Tooltip("停留的位置")] public ShowPos showPos;
[Tooltip("显示顺序")] public ShowMode showMode;
//偏移量
[PropertyRange(-1000,1000)]
public float offsetX;
[PropertyRange(-1000,1000)]
public float offsetY;
protected Sequence sequence;
public enum ShowMode
{
    Direct,//直接播放
    Wait,    //等上一个播完 
}

//出现位置
public enum ShowPos
{
    MiddleUp,
    MiddleDown,
    LeftUp,
    RightUp,
}

然后根据不同属性设置不同动画

protected virtual void InitTweenData()
{
    sequence = DOTween.Sequence();
    var canvasRect = transform.GetComponentInParent<Canvas>().GetComponent<RectTransform>().rect;
    var rect = GetComponent<RectTransform>().rect;
    transform.SetLocalX(0);
    switch (showPos)
    {
        case ShowPos.MiddleUp:
            transform.SetLocalY(canvasRect.height / 2 + rect.height / 2);
            sequence.Append
                (transform
                 .DOLocalMoveY(canvasRect.height / 2 -rect.height / 2+offsetY, tweenDuration));
            break;
        case ShowPos.MiddleDown:
            transform.SetLocalY(-canvasRect.height / 2 - rect.height / 2);
            sequence.Append(transform
                            .DOLocalMoveY(-canvasRect.height / 2 + rect.height / 2+offsetY, tweenDuration)
                           );
            break;
        case ShowPos.LeftUp:
            transform.SetLocalY(canvasRect.height / 2 - rect.height / 2+offsetY);
            transform.SetLocalX(-canvasRect.width / 2 -rect.width / 2);
            sequence.Append(transform
                            .DOLocalMoveX(-canvasRect.width / 2 + rect.width / 2+offsetX, tweenDuration)
                           );
            break;
        case ShowPos.RightUp:
            transform.SetLocalY(canvasRect.height / 2 - rect.height / 2+offsetY);
            transform.SetLocalX(canvasRect.width / 2 + rect.width / 2);
            sequence.Append(transform
                            .DOLocalMoveX(canvasRect.width / 2- rect.width / 2+offsetX, tweenDuration)
                           );
            break;
    }

    sequence.AppendInterval(stayDuration);
    sequence.SetLoops(2, LoopType.Yoyo);
    sequence.Pause();
    sequence.SetAutoKill(false);
}

显示窗口

public virtual void Show()
{
    switch (showMode)
    {
        case ShowMode.Direct:
            sequence.Restart();
            break;
        case ShowMode.Sequence:
            break;
        case ShowMode.Wait:
            if (!sequence.IsPlaying())
            {
                sequence.Restart();
            }
            break;
    }
}

然后通过事件系统调用https://assetstore.unity.com/packages/tools/integration/easy-event-manager-147887

定义不同消息的 枚举

public static class MessageStrings
{
    public enum WindowMsgStr
    {
        MSG_ShowHintWindow,
    }
}

事件注册与注销

protected virtual void Awake()
{
    InitTweenData();
    EventManager.StartListening(messageName.ToString(),Show);
}
protected virtual void OnDestroy()
{
    EventManager.StopListening(messageName.ToString(),Show);
}

最后在需要的地方发送消息即可

EventManager.EmitEvent(messageName.ToString());

Last modification:July 5th, 2021 at 07:09 pm