根据Shader动态生成遮罩
源码地址
圆形遮罩镂空处理脚本:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 圆形遮罩镂空
///
public class CircleGuidance : MonoBehaviour
{
public static CircleGuidance instance;
///
/// 高亮显示目标
///
public Image target;
///
/// 区域范围缓存
///
private Vector3[] corners = new Vector3[4];
///
/// 镂空区域中心
///
private Vector4 center;
///
/// 镂空区域半径
///
private float radius;
///
/// 遮罩材质
///
private Material material;
///
/// 当前高亮区域半径
///
private float currentRadius;
///
/// 高亮区域缩放的动画时间
///
private float shrinkTime = 0.5f;
///
/// 事件渗透组件
///
private GuidanceEventPenetrate eventPenetrate;
private void Awake()
{
instance = this;
}
public void Init(Image target)
{
this.target = target;
eventPenetrate = GetComponent();
if (eventPenetrate != null)
{
eventPenetrate.SetTargetImage(target);
}
Canvas canvas = GameObject.Find("Canvas").GetComponent
矩形遮罩镂空处理脚本:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 矩形遮罩镂空
///
public class RectGuidance : MonoBehaviour
{
public static RectGuidance instance;
///
/// 高亮显示目标
///
public Image target;
///
/// 区域范围缓存
///
private Vector3[] corners = new Vector3[4];
///
/// 镂空区域中心
///
private Vector4 center;
///
/// 最终的偏移x
///
private float targetOffsetX = 0;
///
/// 最终的偏移y
///
private float targetOffsetY = 0;
///
/// 遮罩材质
///
private Material material;
///
/// 当前的偏移x
///
private float currentOffsetX = 0f;
///
/// 当前的偏移y
///
private float currentOffsetY = 0f;
///
/// 高亮区域缩放的动画时间
///
private float shrinkTime = 0.5f;
///
/// 事件渗透组件
///
private GuidanceEventPenetrate eventPenetrate;
private void Awake()
{
instance = this;
}
public void Init(Image target)
{
this.target = target;
eventPenetrate = GetComponent();
if (eventPenetrate != null)
{
eventPenetrate.SetTargetImage(target);
}
Canvas canvas = GameObject.Find("Canvas").GetComponent
新手引导管理脚本,通过此脚本管理遮罩跟引导步骤,动态添加按钮点击事件等:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 新手引导管理
///
public class GuideManagers : MonoBehaviour
{
///
/// 引导步骤数组(如:第一步-》第二步。。。。)
///
public List guideList = new List();
///
/// 当前数组索引
///
private int currentIndex = 0;
///
/// 是否完成所有的新手引导
///
private bool isFinish = false;
///
/// 遮罩对象
///
private GameObject maskPrefabs;
///
///
///
public void Next()
{
if (isFinish || currentIndex > guideList.Count)
{
return;
}
//注销上一步按钮点击事件
if (currentIndex != 0 && guideList[currentIndex - 1].go.GetComponent() != null)
{
EventTriggerListener.GetListener(guideList[currentIndex - 1].go).onClick -= null;
}
if (maskPrefabs == null)
{
maskPrefabs = Instantiate(Resources.Load("RectGuidance_Panel"), this.transform);
}
//初始化遮罩
maskPrefabs.GetComponent().Init(guideList[currentIndex].go.GetComponent()); ;
currentIndex++;
//给当前按钮添加点击事件
if (currentIndex < guideList.Count)
{
EventTriggerListener.GetListener(guideList[currentIndex - 1].go).onClick += (go) =>
{
Next();
};
}
//最后一个按钮点击事件处理
else if (currentIndex == guideList.Count)
{
EventTriggerListener.GetListener(guideList[currentIndex - 1].go).onClick += (go) =>
{
maskPrefabs.gameObject.SetActive(false);
//注销最后一个按钮的点击事件
EventTriggerListener.GetListener(guideList[currentIndex - 1].go).onClick -= null;
};
isFinish = true;
}
}
}
///
/// 引导ui数组
///
[Serializable]
public class GuideUIList
{
///
/// 引导步骤对象
///
public GameObject go;
public GuideUIList(GameObject go)
{
this.go = go;
}
}