Unity UGUI不規則響應區域

需求說明

有時候需要點擊圖片的某一部分,而這一部分又是不規則的圖形

解決方案

做法其實很簡單,添加PolygonCollider2D組件,點擊EditCollider,自己拖動成想要的圖案,最後重寫射線檢測的方法就大功告成了

//***********************************************************
// 描述:不規則圖形精確點擊
// 作者:花花蔡 
// 創建時間:2020-05-21 14:43:08
// 版 本:1.0
//***********************************************************
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CustomImage : Image
{
    PolygonCollider2D _polygon;
    PolygonCollider2D Polgon
    {
        get
        {
            if (_polygon == null) _polygon.GetComponent<PolygonCollider2D>();

            return _polygon;
        }
    }

    public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    {
        Vector3 point;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, screenPoint, eventCamera, out point);
        return _polygon.OverlapPoint(point);
    }

}
  • 最後貼上Editor腳本用於在直接創建不規則點擊的Prefab
//***********************************************************
// 描述:UGUI不規則響應區域
// 作者:花花蔡 
// 創建時間:2020-05-21 14:42:43
// 版 本:1.0
//***********************************************************
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class CustomImageEditor : Editor
{
    private const int UI_LAYER = 5;

    [MenuItem("GameObject/UI/CustomImageBtn", priority = 4)]
    private static void AddImage()
    {

        Transform canvasTrans = GetCanvasTrans();

        Transform image = AddCustomImage();

        if (Selection.activeGameObject!= null && Selection.activeGameObject.layer == UI_LAYER)
        {
            image.SetParent(Selection.activeGameObject.transform);
        }
        else
        {
            image.SetParent(canvasTrans);
        }
       
        image.localPosition = Vector3.zero;
    }

    private static Transform GetCanvasTrans()
    {
        Canvas canvas = GameObject.FindObjectOfType<Canvas>();
        if (canvas == null)
        {
            GameObject canvasObj = new GameObject("Canvas");
            SetLayer(canvasObj);
            canvasObj.AddComponent<RectTransform>();
            canvasObj.AddComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
            canvasObj.AddComponent<CanvasScaler>();
            canvasObj.AddComponent<GraphicRaycaster>();
            return canvasObj.transform;
        }
        else
        {
            return canvas.transform;
        }
    }

    private static Transform AddCustomImage()
    {
        GameObject image = new GameObject("CustomImageBtn");
        SetLayer(image);
        image.AddComponent<RectTransform>();
        image.AddComponent<PolygonCollider2D>();
        image.AddComponent<CustomImage>();
        image.AddComponent<Button>();
        return image.transform;
    }

    private static void SetLayer(GameObject ui)
    {
        ui.layer = UI_LAYER;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章