UI画布助手

18 篇文章 0 订阅
7 篇文章 0 订阅
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UICanvasHelper : MonoBehaviour {

    #region Public Members
    public CanvasScaler UICanvasScaler;
    public Camera UICamera;
    #endregion


    #region Properties
    public static UICanvasHelper Instance { get; private set; }
    #endregion


    #region Private Members
    private float m_fWidthScale = -1;
    private float m_fHeightScale = -1;
    private float m_fMatchValue = -1;
    #endregion


    void Start()
    {
        Instance = this;

        this._SetUIMatch();
        this._SetSizeScale();
    }


    void OnDestroy()
    {
        Instance = null;
    }


    #region Public Methods
    /// <summary>
    /// 世界坐标转换为屏幕坐标
    /// </summary>
    public Vector2 WorldToScreenPoint(Vector3 pWorldPosition, Camera pCamera = null)
    {
        if (pCamera == null)
        {
            pCamera = Camera.main;
        }

#if UNITY_EDITOR // 编辑器模式可能随时要调整画面大小
        this._SetSizeScale();
#endif

        Vector2 ret = pCamera.WorldToScreenPoint(pWorldPosition);
        this._SetPositionScale(ref ret);

        return ret;
    }

    /// <summary>
    /// 屏幕坐标转换为UI坐标
    /// </summary>
    public void ScreenToUIPoint(ref Vector2 pPosition)
    {
#if UNITY_EDITOR // 编辑器模式可能随时要调整画面大小
        this._SetSizeScale();
#endif

        this._SetPositionScale(ref pPosition);
    }
    #endregion


    #region Private Methods
    /// <summary>
    /// 设置分辨率适配比例
    /// </summary>
    private void _SetUIMatch()
    {
        this.UICanvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;

        var scale = Screen.width / (float)(Screen.height);
        if (scale > 1.5f)
        {
            this.m_fMatchValue = 1;
        }
        else if (scale < 1.4f)
        {
            this.m_fMatchValue = 0;
        }
        else
        {
            this.m_fMatchValue = 0.5f;
        }
        this.UICanvasScaler.matchWidthOrHeight = this.m_fMatchValue;
    }

    /// <summary>
    /// 设置尺寸缩放比例
    /// </summary>
    private void _SetSizeScale()
    {
        this.m_fWidthScale = this.UICanvasScaler.referenceResolution.x / Screen.width;
        this.m_fHeightScale = this.UICanvasScaler.referenceResolution.y / Screen.height;
    }

    /// <summary>
    /// 将传入的坐标转换为缩放后的值
    /// </summary>
    private void _SetPositionScale(ref Vector2 pPosition)
    {
        pPosition.x = (pPosition.x - Screen.width * 0.5f) * ((1 - this.m_fMatchValue) * this.m_fWidthScale + this.m_fMatchValue * m_fHeightScale);
        pPosition.y = (pPosition.y - Screen.height * 0.5f) * ((1 - this.m_fMatchValue) * this.m_fWidthScale + this.m_fMatchValue * m_fHeightScale);
    }
    #endregion
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值