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. Promise.race 的原理

    // race的原理 Promise.race = function(values){ return new Promise((resolve,reject)=>{ for(let i = 0 ...

  2. java中super的用法总结

    package com.ssm.java; /** * Super * usage1:super. 直接去调用父类的方法和属性. * usage2:放在构造器中的第一位,代表引用父类的构造器. */ ...

  3. [STL]lower_bound&upper_bound

    源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...

  4. Harbor在安装前的几个注意点

    由于Harbor有1.8后和前的配置不一样,决定先安装1.8,结果报错如下 [root@localhost harbor]# ./install.sh [Step 0]: checking insta ...

  5. C++ Standard Template Library (STL) 高级容器

    更多 STL 数据结构请阅读 NOIp 数据结构专题总结(STL structure 章节) std::map Definition: template < class Key, // map: ...

  6. [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:lovemu' did not find a matching property.

    [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.js ...

  7. php面试专题---16、MySQL创建高性能索引考点

    php面试专题---16.MySQL创建高性能索引考点 一.总结 一句话总结: 注意:只写精品 1.索引的基础? 类似书籍的目录:索引类似于书籍的目录,要想找到一本书的某个特定主题,需要先查找书的目录 ...

  8. python如何判断1个列表中所有的数据都是相等的?

    方法一: 元素两两比较,如果有数据不同,则r的值变为false #!/usr/bin/python a=[22,22,22,22] b = len(a) r=True for i in range(b ...

  9. Jenkins+GitLab持续集成

    向GitLab提交代码之后自动触发Jenkins构建 https://baijiahao.baidu.com/s?id=1630678692475452408&wfr=spider&f ...

  10. 写的一个双向链表,测试OK

    #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct DoubleLi ...