[Unity]新手指引遮罩实现

 

 

两种实现方法

1.通过新建一个不同层级Sorting的UICanvas黑色遮罩的Plane和Button来复制目标按钮的功能

优点&缺点:实现起来很方便,但是对于所有的UI的对象的设置很难实现。

2.通过新建一个不同层级Sorting的UICanvas和4个不同的黑色遮罩的Plane把目标按钮围起来

优点&缺点:通过找到目标UI按钮的 对象,就可以进行遮罩,不需要很复杂的设置,在代码上面需要找到目标的坐标进行实现。

[Unity]新手指引遮罩实现_第1张图片

 

[Unity]新手指引遮罩实现_第2张图片

 

[Unity]新手指引遮罩实现_第3张图片

 

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

public class ButtonCopy : MonoBehaviour {

    public Button button;
    public Text buttonText;

    private Button thisCopyButton;
    private Text thisCopyButtonText;
    /// 
    /// 复制按钮 的 背景,黑色遮罩图片
    /// 
    [SerializeField]
    private Transform blackPlane;

    /// 
    /// 上下左右四个角 的定位图片
    /// 
    public Image LeftUp;
    public Image LeftDown;
    public Image RightUp;
    public Image RightDown;

    /// 
    /// 四个黑色遮罩的图片
    /// 
    public Image BlackUp;
    public Image BlackDown;
    public Image BlackLeft;
    public Image BlackRight;
    /// 
    /// 屏幕的 宽/高
    /// 
    private int ScreenWidth, ScreenHeight;



    // Use this for initialization
    void Start () {
        thisCopyButton = this.button;
        if (thisCopyButton != null)
        {
            thisCopyButtonText = this.GetComponentInChildren();
        }//

        initButton();//方法1
        UpdateScreenWidthHeight();
        UpdateBlackPlane();//方法2
    }//

    public void UpdateScreenWidthHeight()
    {
        ScreenWidth = UnityEngine.Screen.width;
        ScreenHeight = UnityEngine.Screen.height;
        Debug.Log("  ScreenWidth: "+ ScreenWidth+ "  ScreenHeight: "+ ScreenHeight);

    }//

    #region 方法1
    /// 
    /// 对 复制按钮的功能 进行 初始化
    /// 
    public void initButton()
    {

        if (button != null
            && thisCopyButton != null)
        {
            float width, height; Vector2 pos;
            buttonText = button.GetComponentInChildren();
            // https://blog.csdn.net/z5231656w/article/details/46789395
            this.GetComponent().sizeDelta = button.GetComponent().sizeDelta;//复制目标按钮 的宽 和 高
            this.transform.position = button.transform.position;
            thisCopyButtonText.text = buttonText.text;
            width = this.GetComponent().sizeDelta.x;
            height = this.GetComponent().sizeDelta.y;
            pos = this.transform.position;

            if (LeftUp != null
                && LeftDown != null
                && RightUp != null
                && RightDown != null)
            {
                LeftUp.transform.position = new Vector2(pos.x - width / 2, pos.y + height / 2);
                LeftDown.transform.position = new Vector2(pos.x - width / 2, pos.y - height / 2);
                RightUp.transform.position = new Vector2(pos.x + width / 2, pos.y + height / 2);
                RightDown.transform.position = new Vector2(pos.x + width / 2, pos.y - height / 2);
            }//

        }//


    }//
    #endregion//方法1

    #region 方法2
    /// 
    /// 
    /// 
    public void UpdateBlackPlane()
    {
        if (BlackUp != null
            && BlackDown != null
            && BlackLeft != null
            && BlackRight != null
            && button != null)
        {
            //获得 目标按钮 的坐标X,Y
            float x_ = button.GetComponent().position.x;
            float y_ = button.GetComponent().position.y;
            float z_ = button.GetComponent().position.z;
            float width, height;
            width = button.GetComponent().sizeDelta.x;//获得 目标按钮 的宽
            height = button.GetComponent().sizeDelta.y;//获得 目标按钮 的高

            BlackUp.transform.GetComponent().sizeDelta = new Vector2(ScreenWidth, ScreenHeight - (y_+ height / 2));
            BlackDown.transform.GetComponent().sizeDelta = new Vector2(ScreenWidth, (y_- height/2));
            BlackLeft.transform.GetComponent().sizeDelta = new Vector2(x_-width/2, height);
            BlackRight.transform.GetComponent().sizeDelta = new Vector2(ScreenWidth - x_ - width/2, height);

            BlackUp.transform.GetComponent().position = new Vector3(BlackUp.transform.GetComponent().sizeDelta.x / 2, ScreenHeight - BlackUp.transform.GetComponent().sizeDelta.y / 2, 0);
            BlackDown.transform.GetComponent().position = new Vector3(+BlackDown.transform.GetComponent().sizeDelta.x/ 2, +BlackDown.transform.GetComponent().sizeDelta.y/2, 0);
            BlackLeft.transform.GetComponent().position = new Vector3(BlackLeft.transform.GetComponent().sizeDelta.x/2, y_, 0);
            BlackRight.transform.GetComponent().position = new Vector3(BlackRight.transform.GetComponent().sizeDelta.x, y_, 0);
        }//
    }//
    #endregion//方法2

    /// 
    /// 当 复制的按钮 按下按钮
    /// 
    public void PushButton()
    {
        if (button != null
            && thisCopyButton != null)
        {
            //https://blog.csdn.net/kuilaurence/article/details/100982205
            button.onClick.Invoke();//复制按钮的响应事件
        }//

        if (blackPlane != null)
        {
            blackPlane.transform.localScale = Vector3.zero;
        }//
    }//

}//

 

 

 

 

你可能感兴趣的:(游戏实现,Unity)