UGUI新手引导系统

相关链接:http://www.cnblogs.com/ybgame/p/3844315.html


效果图:


对于新手引导,主要分两种做法:需要使用shader的和不需要shader的,这里介绍的是后者

1.屏蔽点击

假如ImageA在ImageB前面,且ImageA完全覆盖ImageB,点击两者的重叠部分,ImageA会收到点击事件,而ImageB不会。对于UGUI来说,就是将一张灰色半透明图放到最前面了


2.目标UI高亮

这里有三种做法:

a.调整目标UI的Hierarchy层级
b.克隆目标UI,调整克隆UI的Hierarchy层级
c.使用Canvas + Graphic Raycaster 

这里本人选择的是第三种,因为第一种会破坏原有的的层级,第二种的话,如果目标UI本身没有绑定Mono脚本,则需要复制事件,不太好

需要注意的是,上面三种做法都是会增加drawcall的


这里给出核心脚本:

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEngine.UI;  
  4. using System;  
  5.   
  6. public class GuideManager : MonoSingletion<GuideManager> {  
  7.   
  8.     private Transform maskTra;  
  9.   
  10.     private string fileDir = "GuideFile/";  
  11.     private string nowCsvFile;  
  12.     private int nowIndex;  
  13.     private bool isFinish = false;//是否完成所有的新手引导  
  14.     private string[] nameArray;      
  15.   
  16.     public void Init()  
  17.     {  
  18.         //读取进度  
  19.         string content = Resources.Load<TextAsset>(fileDir + "GuideProgress").ToString();  
  20.         string[] temp = content.Split(',');  
  21.         nowCsvFile = temp[0];  
  22.         nowIndex = int.Parse(temp[1]);  
  23.         isFinish = bool.Parse(temp[2]);  
  24.   
  25.         //读取需要高亮的组件的Hierarchy路径  
  26.         if (!isFinish)  
  27.         {  
  28.             string s = Resources.Load<TextAsset>(fileDir + nowCsvFile).ToString();  
  29.             nameArray = s.Split(new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries);     
  30.         }   
  31.     }  
  32.   
  33.     void OnDestroy()  
  34.     {  
  35.         //退出游戏后的处理  
  36.         Debug.Log("OnDestroy");  
  37.     }  
  38.   
  39.     public void Next()  
  40.     {  
  41.         if (nowIndex < nameArray.Length)  
  42.         {  
  43.             ShowHightLight(nameArray[nowIndex]);  
  44.             nowIndex++;  
  45.         }  
  46.         else//加载下一个文件  
  47.         {  
  48.             maskTra.gameObject.SetActive(false);  
  49.        
  50.             int index = int.Parse(nowCsvFile.Substring(nowCsvFile.Length - 1));  
  51.             index++;  
  52.             nowCsvFile = nowCsvFile.Substring(0, nowCsvFile.Length - 1) + index.ToString();  
  53.             string path = fileDir + nowCsvFile;  
  54.   
  55.             string content = null;  
  56.             try  
  57.             {  
  58.                 content = Resources.Load<TextAsset>(path).ToString();  
  59.             }  
  60.             catch (Exception e)   
  61.             {  
  62.                 isFinish = true;  
  63.                 Debug.Log("finish");  
  64.                 return;  
  65.             }  
  66.             nowIndex = 0;  
  67.             nameArray = content.Split(new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries);    
  68.         }  
  69.     }  
  70.   
  71.     void ShowHightLight(string name, bool checkIsClone = true)  
  72.     {  
  73.         if(checkIsClone && name.Contains("/"))  
  74.         {  
  75.             name = name.Insert(name.IndexOf('/'), "(Clone)");  
  76.         }  
  77.         StartCoroutine(FindUI(name));  
  78.     }  
  79.   
  80.     void CancelHightLight(GameObject go)  
  81.     {  
  82.         Destroy(go.GetComponent<GraphicRaycaster>());  
  83.         Destroy(go.GetComponent<Canvas>());  
  84.         Next();  
  85.         EventTriggerListener.GetListener(go).onPointerClick -= CancelHightLight;  
  86.     }  
  87.   
  88.     IEnumerator FindUI(string name)  
  89.     {  
  90.         //寻找目标  
  91.         GameObject go = UIManager.Instance.Find(name);  
  92.         while(go == null)  
  93.         {  
  94.             yield return new WaitForSeconds(0.1f);  
  95.             Debug.Log("wait");  
  96.             go = UIManager.Instance.Find(name);  
  97.         }  
  98.          
  99.         //高亮  
  100.         maskTra = UIManager.Instance.Show("Mask").transform;  
  101.         maskTra.SetAsLastSibling();  
  102.         go.AddComponent<Canvas>().overrideSorting = true;  
  103.         go.AddComponent<GraphicRaycaster>();  
  104.   
  105.         //设置监听  
  106.         EventTriggerListener.GetListener(go).onPointerClick += CancelHightLight;  
  107.     }  
  108.   
  109. }  
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class GuideManager : MonoSingletion<GuideManager> {

    private Transform maskTra;

    private string fileDir = "GuideFile/";
    private string nowCsvFile;
    private int nowIndex;
    private bool isFinish = false;//是否完成所有的新手引导
    private string[] nameArray;    

    public void Init()
    {
        //读取进度
        string content = Resources.Load<TextAsset>(fileDir + "GuideProgress").ToString();
        string[] temp = content.Split(',');
        nowCsvFile = temp[0];
        nowIndex = int.Parse(temp[1]);
        isFinish = bool.Parse(temp[2]);

        //读取需要高亮的组件的Hierarchy路径
        if (!isFinish)
        {
            string s = Resources.Load<TextAsset>(fileDir + nowCsvFile).ToString();
            nameArray = s.Split(new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries);   
        } 
    }

    void OnDestroy()
    {
        //退出游戏后的处理
        Debug.Log("OnDestroy");
    }

    public void Next()
    {
        if (nowIndex < nameArray.Length)
        {
            ShowHightLight(nameArray[nowIndex]);
            nowIndex++;
        }
        else//加载下一个文件
        {
            maskTra.gameObject.SetActive(false);
     
            int index = int.Parse(nowCsvFile.Substring(nowCsvFile.Length - 1));
            index++;
            nowCsvFile = nowCsvFile.Substring(0, nowCsvFile.Length - 1) + index.ToString();
            string path = fileDir + nowCsvFile;

            string content = null;
            try
            {
                content = Resources.Load<TextAsset>(path).ToString();
            }
            catch (Exception e) 
            {
                isFinish = true;
                Debug.Log("finish");
                return;
            }
            nowIndex = 0;
            nameArray = content.Split(new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries);  
        }
    }

    void ShowHightLight(string name, bool checkIsClone = true)
    {
        if(checkIsClone && name.Contains("/"))
        {
            name = name.Insert(name.IndexOf('/'), "(Clone)");
        }
        StartCoroutine(FindUI(name));
    }

    void CancelHightLight(GameObject go)
    {
        Destroy(go.GetComponent<GraphicRaycaster>());
        Destroy(go.GetComponent<Canvas>());
        Next();
        EventTriggerListener.GetListener(go).onPointerClick -= CancelHightLight;
    }

    IEnumerator FindUI(string name)
    {
        //寻找目标
        GameObject go = UIManager.Instance.Find(name);
        while(go == null)
        {
            yield return new WaitForSeconds(0.1f);
            Debug.Log("wait");
            go = UIManager.Instance.Find(name);
        }
       
        //高亮
        maskTra = UIManager.Instance.Show("Mask").transform;
        maskTra.SetAsLastSibling();
        go.AddComponent<Canvas>().overrideSorting = true;
        go.AddComponent<GraphicRaycaster>();

        //设置监听
        EventTriggerListener.GetListener(go).onPointerClick += CancelHightLight;
    }

}

3.目标UI的查找

首先,我们需要记录一下目标UI有哪些,查找的话,就是找UI的Hierarchy路径。对此,本人做了一个小工具:



这是unitypackage:

http://pan.baidu.com/s/1beSlVC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值