UGUI和NGUI 的屏幕坐标

指示UI 的localRotation.z

NGUI

NGUI_ui.transform.localPosition = new Vector3(x, y, 0);

UGUI

m_arrow.localPosition = new Vector3(x, y, 0);//RectTransform

rot_z =

(NGUI_ui.transform.eulerAngles.z > 180)?NGUI_ui.transform.eulerAngles.z-360:NGUI_ui.transform.eulerAngles.z

在参考资料1 的基础上,添加了 判断 地图指示UI 箭头指示的位置 来设置 自身 位于 屏幕 的哪个位置。

对 摄像机和 3D 玩家 实体 的坐标 进行 判断,进一步优化

.../// <summary>/// 玩家 对象/// </summary>public Transform m_target;public RectTransform m_arrow;               //UGUI UI指示箭头/// <summary>/// 屏幕中心点/// </summary>Vector3 m_Screen_Center;public Vector3 cross;float x = 0, y = 0, x_1 = 0, y_1 = 0,rot_z = 0;/// <summary>/// NGUI UI指示/// </summary>public Transform NGUI_ui;
...private void Start(){m_Screen_Center = new Vector3(Screen.width / 2, Screen.height / 2, 0);}
...
update()
{
...#region 地图UI 屏幕边缘指示//将目标的世界坐标转换成屏幕坐标Vector3 target_ScreenPoint = Camera.main.WorldToScreenPoint(m_target.position);//计算target_ScreenPoint和屏幕中心点的绝对值角度float angle = Mathf.Atan(Mathf.Abs(target_ScreenPoint.y - Screen.height / 2)/ Mathf.Abs(target_ScreenPoint.x - Screen.width / 2)) * 180 / Mathf.PI;//通过判断target_ScreenPoint相对屏幕中心点所在象限处理angle的差值if (target_ScreenPoint.x <= Screen.width / 2)angle = target_ScreenPoint.y >= Screen.height / 2 ? 90 - angle : 90 + angle;elseangle = target_ScreenPoint.y >= Screen.height / 2 ? 270 + angle : 270 - angle;#region  计算摄像机和目标的叉乘//以屏幕中心所在的单位向量为起始向量from,并将from的y值设成和目标y值保持一致Vector3 from = Camera.main.transform.forward;from.y = m_target.forward.y;//将屏幕中心坐标转换成世界坐标Vector3 cameraPos = Camera.main.ScreenToWorldPoint(m_Screen_Center);cameraPos.y = m_target.position.y;//求出目标点与摄像机之间的向量Vector3 to = m_target.position - cameraPos;//求出两向量之间的叉乘//Vector3 cross = Vector3.Cross(from, to);#endregion//根据叉乘求出带符号的角度//cross.y > 0:目标向量位于起始向量右侧//cross.y < 0:目标向量位于起始向量左侧if (cross.y > 0 && angle < 180)angle += 180;else if (cross.y < 0 && angle > 180)angle -= 180;Vector3 euler = m_arrow.eulerAngles;euler.z = angle;//设置箭头的角度m_arrow.eulerAngles = euler;NGUI_ui.eulerAngles = euler;rot_z = (NGUI_ui.transform.eulerAngles.z > 180)?NGUI_ui.transform.eulerAngles.z-360:NGUI_ui.transform.eulerAngles.z;
//设置 指示UI 的 坐标if (rot_z >= -60&& rot_z <0)//屏幕上方{print("  屏幕上方 2 ");/*rot_z = Mathf.Abs(rot_z);x_1 = (rot_z / 60);x_1 = (x_1 > 1) ? 1 : x_1;x = (x_1) * Screen.width / 2f;NGUI_ui.transform.localPosition = new Vector3(x, Screen.height / 2, 0);m_arrow.localPosition = new Vector3(x, Screen.height / 2, 0);*/test(0, 60, true, 1,1, 1);}else if (rot_z >= 0&& rot_z <= 60)//屏幕上方{print("  屏幕上方 1 ");/*x_1 = (rot_z / 60);x_1 = (x_1 > 0.9f) ? 0.9f : x_1;x = (x_1) * (-Screen.width / 2f);NGUI_ui.transform.localPosition = new Vector3(x, Screen.height / 2, 0);m_arrow.localPosition = new Vector3(x, Screen.height / 2, 0);*/test(0, 60, true, 1, -1, 1);}else if (rot_z >= -90&& rot_z < -60)//屏幕右方1{print("  屏幕右方 1 ");/*y_1 = ((Mathf.Abs(rot_z) - 60)/ 30);y_1 = (y_1 > 0.9f) ? 0.9f : y_1;y = (0.9f - y_1) * Screen.height / 2;NGUI_ui.transform.localPosition = new Vector3(Screen.width / 2, y, 0);m_arrow.localPosition = new Vector3(Screen.width / 2, y, 0);*/test(60, 30, false, 1, 1,1);}else if (rot_z >= -120&& rot_z < -90)//屏幕右方2{print("  屏幕右方  2");/*y_1 = ((Mathf.Abs(rot_z) - 90)/ 30);y_1 = (y_1 > 0.9f) ? 0.9f : y_1;y_1 = (y_1 < 0.1f) ? 0.1f : y_1;y = (  y_1) * (-Screen.height / 2);NGUI_ui.transform.localPosition = new Vector3(Screen.width / 2, y, 0);m_arrow.localPosition = new Vector3(Screen.width / 2, y, 0);*/test(90, 30, false, -1, 1, 1);}else if (rot_z > 120&& rot_z <= 180)//屏幕下方 2{print("  屏幕下方  2");/*x_1 = ((Mathf.Abs(rot_z) - 120)/ 60);x_1 = (x_1 > 0.9f) ? 0.9f : x_1;x_1 = (x_1 < 0.1f) ? 0.1f : x_1;x = (1f-x_1) * (-Screen.width/2f);NGUI_ui.transform.localPosition = new Vector3(x, -Screen.height / 2, 0);m_arrow.localPosition = new Vector3(x, -Screen.height / 2, 0);*/test(120, 60, true, -1, -1, -1);}else if (rot_z > -180&& rot_z <= -120)//屏幕下方 1{print("  屏幕下方  1");/*x_1 = ((Mathf.Abs(rot_z)-120)/ 60);//x_1 = (x_1 > 1) ? 1 : x_1;x_1 = (x_1 > 0.9f) ? 0.9f : x_1;x_1 = (x_1 < 0.1f) ? 0.1f : x_1;x = (1 - x_1) * Screen.width / 2f;NGUI_ui.transform.localPosition = new Vector3(x, -Screen.height / 2, 0);m_arrow.localPosition = new Vector3(x, -Screen.height / 2, 0);*/test(120, 60, true, -1, 1,-1);}else if (rot_z > 60&& rot_z <= 90)//屏幕左方{//rot_z 与 60~120 的范围相减/*print("  屏幕左方  1");y_1 = (((rot_z) - 60)/ 30);y_1 = (y_1 > 0.9f) ? 0.9f : y_1;y = (0.9f - y_1) * Screen.height / 2;NGUI_ui.transform.localPosition = new Vector3(-Screen.width / 2, y, 0);m_arrow.localPosition = new Vector3(-Screen.width / 2, y, 0);*/test(60, 30, false, 1, 1,-1);}else if (rot_z > 90&& rot_z <= 120)//屏幕左方{//rot_z 与 60~120 的范围相减print("  屏幕左方  2");/*y_1 = (((rot_z) - 90)/ 30);y_1 = (y_1 > 0.9f) ? 0.9f : y_1;y_1 = (y_1 < 0.1f) ? 0.1f : y_1;y = (y_1) * (-Screen.height / 2);NGUI_ui.transform.localPosition = new Vector3(-Screen.width / 2, y, 0);m_arrow.localPosition = new Vector3(-Screen.width / 2, y, 0);*/test(90,30,false,-1, 1,-1);}
//设置 指示UI 的 坐标--------------------------------------------------------print("euler.z:"+ euler.z + "///rot_z:" + rot_z + "///x:" +x+"/y:"+y+ "/x_1:" + x_1 + "/y_1:" + y_1 + "///Screen.width" + Screen.width + "///Screen.height" + Screen.height);#endregion//地图UI 屏幕边缘指示
}//update()
.../// <summary>/// y_1 = (((rot_z) - 90)/ 30);/// </summary>/// <param name="value1">y_1 = (((rot_z) - value1)/ 30);</param>/// <param name="value2">y_1 = (((rot_z) - 90)/ value2);</param>/// <param name="XOrY">true 为X,false 为Y</param>/// <param name="symbol">-1 或者 +1</param>/// <param name="symbol">((x_1) * Screen.width / 2f)* symbol1</param>private void test(int value1,int value2,bool XOrY,int symbol, int symbol1, int symbol2){rot_z = Mathf.Abs(rot_z);if (XOrY){x_1 = ((rot_z - value1) / value2);x_1 = (x_1 > 0.9f) ? 0.9f : x_1;x_1 = (x_1 < 0.1f) ? 0.1f : x_1;x = (symbol > 0) ?((x_1) * Screen.width / 2f)* symbol1 :(0.9f - x_1) * Screen.width / 2f * symbol1;NGUI_ui.transform.localPosition = new Vector3(x, symbol2 *Screen.height / 2, 0);m_arrow.localPosition = new Vector3(x, symbol2 * Screen.height / 2, 0);}else{y_1 = ((rot_z - value1) / value2);y_1 = (y_1 > 0.9f) ? 0.9f : y_1;y_1 = (y_1 < 0.1f) ? 0.1f : y_1;y = (symbol > 0) ?((0.9f - y_1) * Screen.height / 2) * symbol1 :((y_1) * (-Screen.height / 2)) * symbol1;NGUI_ui.transform.localPosition = new Vector3(symbol2 * Screen.width / 2, y, 0);m_arrow.localPosition = new Vector3(symbol2*Screen.width / 2, y, 0);}}
...

test函数里面的 0.9f和 0.1f 表示的是距离屏幕 Screen.Height或Screen.Width,以至于 不使得指示UI 不会到屏幕的四个角。

参考资料:

1.

Unity3D 世界坐标转屏幕坐标的坑

2.

在屏幕边缘指示怪物/敌人当前所处的方位

3.

[Unity][UGUI][NGUI]地图指示UI屏幕边缘显示相关推荐

  1. 计算机桌面显示左右有黑边,电脑屏幕显示不全左右两边黑边框 win7电脑屏幕边缘显示不全怎么调整...

    我们在使用电脑的时候,总是会遇到很多的电脑难题.当我们在遇到了电脑屏幕显示不全左右两边黑边框的时候,那么我们应该怎么办呢?今天就一起来跟随知识屋的小编看看怎么解决的吧. 电脑屏幕显示不全左右两边黑边框 ...

  2. Unity UGUI NGUI 模型 粒子特效 三者之间 渲染层级设置

    目录 1.介绍两大UI插件NGUI和UGUI  2.unity渲染顺序控制方式  3.NGUI的控制  4.UGUI的控制  5.模型深度的控制  6.粒子特效深度控制  7.NGUI与模型和粒子特效 ...

  3. 【绝对原创】Unity开发 NGUI如何使游戏屏幕 自适应

    使用NGUI如何使游戏屏幕 自适应 Hello 大家好,我叫阿祥,英文名叫LuckyDog.俺是新来滴,大家多多关照.. 首次写技术博客,我只是将这儿当做自己存储知识笔记的地方和以知识会友的平台,希望 ...

  4. iOS 高德地图怎么在屏幕内显示所有的Marker?

    IOS:参考 MAMapView 类的 showAnnotations:edgePadding:animated 方法 Android: 用LatLngBounds.Builder 将所有marker ...

  5. Unity UGUI 的锚点Anchor与轴心点Pivot生成相对位置的UI

    UGUI是什么 UGUI 是Unity 官方推出的最新UI系统.它从 Unity 4.6 开始,被集成到 Unity 的编辑器中.相较于旧的 UI 系统,它绝对属于一个巨大的飞跃!因为只要有过旧 UI ...

  6. Unity UGUI 效果 之 UI 元素 多边形UI (例如雷达图,圆形,不规则多边形 UI等)显示 的简单实现的几种方法整理

    Unity UGUI 效果 之 UI 元素 多边形UI (例如雷达图,圆形,不规则多边形 UI等)显示 的简单实现的几种方法整理 目录 Unity UGUI 效果 之 UI 元素 多边形UI (例如雷 ...

  7. 【Unity使用UGUI实现王者荣耀UI界面(四)】游戏开始界面

    文章目录 [Unity使用UGUI实现王者荣耀UI界面(四)]游戏开始界面 1. 把一些重复的UI添加 2. 开始游戏按钮 3. 注销按钮 4. 完成 5. 打包 6. 打包完成 内 容 简 介 章节 ...

  8. Curved UI - VR Ready Solution To Bend Warp Your Canvas 1.7,1.8,2.2,2.3 四种版本压缩包(Unity UGUI曲面插件)

    Curved UI - VR Ready Solution To Bend Warp Your Canvas 1.7,1.8,2.2,2.3 四种版本压缩包(Unity UGUI曲面插件) 可以兼容V ...

  9. 【Unity使用UGUI实现王者荣耀UI界面(三)】登录界面以及加载界面优化

    [Unity使用UGUI实现王者荣耀UI界面(三)]登录界面以及加载界面优化 [只是用来玩玩的,不要太当真] 效果显示: zhans 1. 加载界面进度100%跳转登录界面 这个功能好做,只需要将上次 ...

最新文章

  1. Bash中的whereis
  2. Cocos2d-x 3.0 简捷的物理引擎
  3. DateTimeFormatInfo 类
  4. SSIS hang with unhandle exception
  5. 足不出户带你体验专业实验室,技术实现不在话下
  6. Description Resource Path LocationType Java compiler level does not match the version of the instal
  7. Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。
  8. 计算机做无线AP共享文件,Win7开启AP无线一键共享网络(包括闪讯)给wifi设备使用!...
  9. Java中的重载(overloading)和重写(overriding)
  10. windows系统突然提示winload.exe文件错误
  11. 广西(柳州)创建国家级车联网先导区建设方案
  12. python 手机号码归属地 软件,Python查询手机号码归属地几种方法
  13. 嵌入式软件开发之程序架构(一)
  14. android 字体删除线,android TextView 设置和取消删除线的两种方法
  15. python从1 2 3 4 1000_Python语句序列“a=(1,2,3,None,(),[]);print(len(a))”的运行结果是()。 (2.0分)_学小易找答案...
  16. python玩扫雷_Python玩转算法—扫雷
  17. Fragment中getContext得到的context从哪来?
  18. mysql 基本配置
  19. 如何选择eclipse svn插件的版本
  20. 调用MapReduce对文件中各个单词出现的次数进行统计

热门文章

  1. 直播|BIA Separations 和元生物两位大咖关于质粒DNA的制造工艺和质量控制
  2. [译]用R语言做挖掘数据《七》
  3. CSS------定位和动画
  4. 计算机图学概念,计算机图形学(概念).doc
  5. 微信个性签名怎么弄成竖的?不仅可以竖着还可以加边框效果!
  6. 从零搭建阿里云服务器(图文详解)
  7. 一个barcode 多个 sku号_互联网洪流之下:美的已悄悄变成一个科技集团|36氪专访...
  8. 一.二.管理和信息化软件的关系
  9. Seed, Expand and Constrain: Three Principles for Weakly-Supervised Image Segmentation 论文笔记
  10. 【海神】各种工作特色比照和介绍