zoukankan      html  css  js  c++  java
  • 屏幕坐标点转UGUI坐标【包含屏幕适配】

    using UnityEngine;
    
    public class ScreenToUI : MonoBehaviour
    {
        public const float UI_Width = 1366f;
        public const float UI_Height = 768f;
    
        public readonly static float Screen_Width_Half = Screen.width / 2f;
        public readonly static float Screen_Height_Half = Screen.height / 2f;
    
        public readonly static float Screen_UI_Ratio = UI_Width / UI_Height;
        public readonly static float Screen_Screen_Ratio = Screen.width / Screen.height;
    
        public readonly static float Screen_Width_Ratio = UI_Width / Screen.width;
        public readonly static float Screen_Height_Ratio = UI_Height / Screen.height;
    
        private Vector3 m_position_offset;
    
        private void Awake()
        {
            m_position_offset = Vector3.zero;
    
            //计算目标偏移量,遍历到以中心为锚点的父物体为止
            Transform target = transform;
    
            int index = 0, count = 0;
    
            while (target != null)
            {
                if (index++ < count)
                {
                    m_position_offset += target.localPosition;
    
                    target = target.parent;
                }
                else
                {
                    break;
                }
            }
        }
    
        private void Update()
        {
            if (Input.GetMouseButton(0))
            {
                Vector3 position = FormatPosition(Input.mousePosition);
    
                //do something...
    
                Debug.LogError(position);
            }
        }
    
        private Vector3 FormatPosition(Vector3 position)
        {
            float ratio_width = 1, ratio_height = 1;
    
            if (Screen_UI_Ratio != Screen_Screen_Ratio)
            {
                if (Screen_UI_Ratio < Screen_Screen_Ratio)
                {
                    float real = Screen.height * Screen_UI_Ratio;
                    ratio_width = Screen.width / real;
                }
                else
                {
                    float real = Screen.width / Screen_UI_Ratio;
                    ratio_height = Screen.height / real;
                }
            }
    
            position.x -= Screen_Width_Half;
            position.y -= Screen_Height_Half;
    
            position.x *= Screen_Width_Ratio * ratio_width;
            position.y *= Screen_Height_Ratio * ratio_height;
    
            position -= m_position_offset;
    
            return position;
        }
    }
    View Code

    屏幕坐标以左下角为坐标

    UGUI以中心为坐标

    首先将坐标位置转换

    然后通过屏幕宽高比与UGUI设置的宽高比进行计算,UGUI.UIScaleMode = Scale With Screen Size

    最后计算父物体与中心点的偏移量

  • 相关阅读:
    leetcode 190 Reverse Bits
    vs2010 单文档MFC 通过加载位图文件作为客户区背景
    leetcode 198 House Robber
    记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
    逆序数2 HDOJ 1394 Minimum Inversion Number
    矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence
    递推DP URAL 1586 Threeprime Numbers
    递推DP URAL 1167 Bicolored Horses
    递推DP URAL 1017 Staircases
    01背包 URAL 1073 Square Country
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/11696548.html
Copyright © 2011-2022 走看看