unity--新手引导+ugui镂空遮罩

目录

1.引导界面样式

2. 引导内容配置

 3.Ugui镂空遮照

2d样式

3.3d样式



1.引导界面样式

 

2. 引导内容配置

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

public class NoviceContr : Singleton<NoviceContr>
{
    [SerializeField] private HollowOutMask;
    [SerializeField] private RectTransform mask3d;
    [SerializeField] private RectTransform fingerObj;
    //剧情引导
    [SerializeField] private GameObject plotObj;
    [SerializeField] private Text plotText;

    public string[] desc_1 = { "忙碌了一天,我终于回到家了。" };
    public string[] desc_2 = { "噢!我的天哪!" };
    public string[] desc_3 = { "先收拾玄关吧。" };
    public string[] desc_4 = { "在规定的时间内将鞋子拖拽到格子里。" };
    public string[] desc_5 = { "呼,下一个是放包的柜子。" };
    public string[] desc_6 = { "接下来,我该整理客厅了。" };
    public string[] desc_7 = { "呼,客厅整理完毕,然后是厨房。" };
    public string[] desc_8 = { "厨房收拾完毕,让我看看家里的其他区域情况如何。" };
    public string[] desc_9 = { "一楼全部收拾完毕。让我去楼上看看吧。" };
    public string[] desc_10 = { "这里也收拾好了,接下来是哪里呢?" };
    public string[] desc_11 = { "整个家都收拾完毕,终于可以休息了!" };

    public int[] descNode = { 2, 1, 2, 3 };

    [SerializeField] private int plotIndex = 0;
    [SerializeField] private int plotDescCount = -1;
    [SerializeField] private int tempCount = 0;

    private void ShowPlot(int _plotIndex)
    {
        plotIndex = _plotIndex;
        //Debug.Log("引导:" + plotIndex + "..." + tempCount);
        if (PlayerPrefs.GetInt("curPlotIndex", 0) > plotIndex)
        {
            DoSomeThing();
        }
        else
        {
            plotObj.SetActive(true);
            switch (plotIndex)
            {
                case 0:
                    plotText.text = desc_1[tempCount];
                    break;
                case 1:
                    plotText.text = desc_2[tempCount];
                    break;
                case 2:
                    plotText.text = desc_3[tempCount];
                    break;
                case 3:
                    plotText.text = desc_4[tempCount];
                    break;
                case 4:
                    plotText.text = desc_5[tempCount];
                    break;
                case 5:
                    plotText.text = desc_6[tempCount];
                    break;
                case 6:
                    plotText.text = desc_7[tempCount];
                    break;
                case 7:
                    plotText.text = desc_8[tempCount];
                    break;
                case 8:
                    plotText.text = desc_9[tempCount];
                    break;
                case 9:
                    plotText.text = desc_10[tempCount];
                    break;
                case 10:
                    plotText.text = desc_11[tempCount];
                    break;
            }
        }
    }

    public void OnPlotClick()
    {
        tempCount += 1;
        if (tempCount >= plotDescCount)
        {
            DoSomeThing();
            PlayerPrefs.SetInt("curPlotIndex", plotIndex + 1);
        }
        else
        {
            ShowPlot(plotIndex);
        }
    }


    private void DoSomeThing()
    {
        plotObj.SetActive(false);
        plotText.text = "";
        switch (plotIndex)
        {
            case 0:
                break;
            case 1:

                break;
            case 2:

                break;
            case 3:
                break;
            default:
                break;
        }
    }

    public void YinDaoEnd()
    {

    }

}

 3.Ugui镂空遮照

2d样式

3.3d样式

 

using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 实现镂空效果的Mask组件
/// </summary>
public class HollowOutMask : MaskableGraphic, ICanvasRaycastFilter
{
    [SerializeField]
    private RectTransform _target;

    private Vector3 _targetMin = Vector3.zero;
    private Vector3 _targetMax = Vector3.zero;

    private bool _canRefresh = true;
    private Transform _cacheTrans = null;

    /// <summary>
    /// 设置镂空的目标
    /// </summary>
    public void SetTarget(RectTransform target)
    {
        _canRefresh = true;
        _target = target;
        _RefreshView();
    }

    private void _SetTarget(Vector3 tarMin, Vector3 tarMax)
    {
        if (tarMin == _targetMin && tarMax == _targetMax)
            return;
        _targetMin = tarMin;
        _targetMax = tarMax;
        SetAllDirty();
    }

    private void _RefreshView()
    {
        if (!_canRefresh) return;
        _canRefresh = false;

        if (null == _target)
        {
            _SetTarget(Vector3.zero, Vector3.zero);
            SetAllDirty();
        }
        else
        {
            Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(_cacheTrans, _target);
            _SetTarget(bounds.min, bounds.max);
        }
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        if (_targetMin == Vector3.zero && _targetMax == Vector3.zero)
        {
            base.OnPopulateMesh(vh);
            return;
        }

        vh.Clear();

        // 填充顶点
        UIVertex vert = UIVertex.simpleVert;
        vert.color = color;

        Vector2 selfPiovt = rectTransform.pivot;
        Rect selfRect = rectTransform.rect;
        float outerLx = -selfPiovt.x * selfRect.width;
        float outerBy = -selfPiovt.y * selfRect.height;
        float outerRx = (1 - selfPiovt.x) * selfRect.width;
        float outerTy = (1 - selfPiovt.y) * selfRect.height;
        // 0 - Outer:LT
        vert.position = new Vector3(outerLx, outerTy);
        vh.AddVert(vert);
        // 1 - Outer:RT
        vert.position = new Vector3(outerRx, outerTy);
        vh.AddVert(vert);
        // 2 - Outer:RB
        vert.position = new Vector3(outerRx, outerBy);
        vh.AddVert(vert);
        // 3 - Outer:LB
        vert.position = new Vector3(outerLx, outerBy);
        vh.AddVert(vert);

        // 4 - Inner:LT
        vert.position = new Vector3(_targetMin.x, _targetMax.y);
        vh.AddVert(vert);
        // 5 - Inner:RT
        vert.position = new Vector3(_targetMax.x, _targetMax.y);
        vh.AddVert(vert);
        // 6 - Inner:RB
        vert.position = new Vector3(_targetMax.x, _targetMin.y);
        vh.AddVert(vert);
        // 7 - Inner:LB
        vert.position = new Vector3(_targetMin.x, _targetMin.y);
        vh.AddVert(vert);

        // 设定三角形
        vh.AddTriangle(4, 0, 1);
        vh.AddTriangle(4, 1, 5);
        vh.AddTriangle(5, 1, 2);
        vh.AddTriangle(5, 2, 6);
        vh.AddTriangle(6, 2, 3);
        vh.AddTriangle(6, 3, 7);
        vh.AddTriangle(7, 3, 0);
        vh.AddTriangle(7, 0, 4);
    }

    bool ICanvasRaycastFilter.IsRaycastLocationValid(Vector2 screenPos, Camera eventCamera)
    {
        if (null == _target) return true;
        // 将目标对象范围内的事件镂空(使其穿过)
        return !RectTransformUtility.RectangleContainsScreenPoint(_target, screenPos, eventCamera);
    }

    protected override void Awake()
    {
        base.Awake();
        _cacheTrans = GetComponent<RectTransform>();
    }

    void Update()
    {
        _canRefresh = true;
        _RefreshView();
    }
}

猜你喜欢

转载自blog.csdn.net/lalate/article/details/130600713
今日推荐