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 = , count = ; while (target != null)
{
if (index++ < count)
{
m_position_offset += target.localPosition; target = target.parent;
}
else
{
break;
}
}
} private void Update()
{
if (Input.GetMouseButton())
{
Vector3 position = FormatPosition(Input.mousePosition); //do something... Debug.LogError(position);
}
} private Vector3 FormatPosition(Vector3 position)
{
float ratio_width = , ratio_height = ; 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;
}
}

屏幕坐标以左下角为坐标

UGUI以中心为坐标

首先将坐标位置转换

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

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

屏幕坐标点转UGUI坐标【包含屏幕适配】的更多相关文章

  1. 【Unity笔记】UGUI中Canvas屏幕适配

    1.通过RectTransform中的Anchors和Pivot来进行控件和窗体的布局适配. Anchors控制当前Panel相对于父窗体的布局位置,可以设置为居中或者左上角,当父窗体拉伸的时候当前P ...

  2. iOS - iPhone屏幕适配/启动图适配/APP图标适配(iPhone最全尺寸包含iPhoneX/XR/XS/XS Max等)

    趁iPhone新品还没有发布,先整理一下屏幕适配.启动图适配.APP图标适配的笔记,方便以后查阅: 注:部分图片来源于网络 违删; (一)iPhone屏幕适配: (1)屏幕分辨率: ①设计尺寸规范(表 ...

  3. 关于Unity中的屏幕适配

    一.Game视图的屏幕分辨率可以先自定义添加,供以后选择,以下是手游经常用到的分辨率: 1.1136X640,iPhone5 2.1920X1080,横屏,主流游戏都是这个分辨率 3.1080X192 ...

  4. IOS开发之绝对布局和相对布局(屏幕适配)

    之前如果做过Web前端页面的小伙伴们,看到绝对定位和相对定位并不陌生,并且使用起来也挺方便.在IOS的UI设计中也有绝对定位和相对定位,和我们的web前端的绝对定位和相对定位有所不同但又有相似之处.下 ...

  5. JavaScript强化教程 —— Cocos2d-JS的屏幕适配方案

    1. 设置屏幕适配策略(Resolution Policy) 如果你还没有用过Resolution Policy,只需要在游戏载入过程完成之后(cc.game.onStart函数回调中),调用下面的代 ...

  6. CSS基础教程 -- 媒体查询屏幕适配

    响应式布局 Media Query 的使用方法 在上例中, 我们使用Media Queries来根据3种不同尺寸的窗口使用3种不同的样式.通过不同的媒体类型和条件定义样式表规则,媒体查询让CSS可以更 ...

  7. 【收藏】Android屏幕适配全攻略(最权威的Google官方适配指导)

    来源:http://blog.csdn.net/zhaokaiqiang1992 更多:Android AutoLayout全新的适配方式, 堪称适配终结者 Android的屏幕适配一直以来都在折磨着 ...

  8. Android屏幕适配全攻略(最权威的官方适配指导)(转),共大家分享。

    Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的官方文档为基础,全面而深入的讲解了Android屏幕适配的原因.重要概念.解决方案及最佳实践,我相信如果你能认真的学习 ...

  9. iOS开发——UI基础-屏幕适配

    一.适配 1.什么是适配?适应.兼容各种不同的情况 2.移动开发中,适配的常见种类 2.1系统适配 针对不同版本的操作系统进行适配 2.2屏幕适配 针对不同大小的屏幕尺寸进行适配 二.点和像素 1.在 ...

随机推荐

  1. 图片公式转为word格式

    mathpix提取Latex格式,下载mathpix snipping tool工具,截图即可获取Latex格式公式 Latex格式去下面网站转换为mathml格式 https://johnmacfa ...

  2. 修改Win7登陆界面墙纸

    修改Win7登陆界面墙纸 修改注册表.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\W ...

  3. Task10.Bert

    Transformer原理 论文地址:Attention Is All You Need:https://arxiv.org/abs/1706.03762 Transformer是一种完全基于Atte ...

  4. linux find rm ls 逻辑非运用

    需求场景描述 查找出除已知文件外的文件 办法: [root@VM_58_118_centos test]# .1_fv1..0_pv1..6_15752678845473..2_fv1..4_pv1. ...

  5. char* 和 cha[]

    char* s1 = "hello";//字符串常量 s是一个保存了字符串首地址的指针变量,同时也是字符串的名字,s的内容是第一个字符的地址,当s指向常量字符串时候,内容不能改变( ...

  6. 配置自己的CocoaPods库

    序 默认安装的cocoapods确实很好用,可是毕竟自己会写一些库和修改一些第三方库来用.所幸cocoapods确实是一个神器.他可以定义自己的库来用. 如何安装Cocoapods,请参考这篇 从头来 ...

  7. 新建 SecondFragment 实现类

    package com.test.mvp.mvpdemo.mvp.v6.view; import android.os.Bundle;import android.support.annotation ...

  8. Linux C编程

    Linux C网络编程 1.Linux套接字 1.1 套接字介绍 套接字(Sockets),即为网络进程ID,是由运行这个进程的计算机的IP地址和这个进程使用的端口(Port)组成. 可以只用'net ...

  9. Malformed UTF-8 characters, possibly incorrectly encoded 或中文乱码 (Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in)

    问题: Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in 是 ...

  10. loj#501 「LibreOJ β Round」ZQC 的树列

    分析 代码(我的代码是瞎jb水过去的) #include<bits/stdc++.h> using namespace std; #define li long long li a[]; ...