HUD区域限制算是比较常用到的功能,方形的HUD区域限制多见于小地图,弧形或者椭圆多见于屏幕范围约束。

我没有研究倒角矩形做法,而是将椭圆和矩形进行插值得到一个弧度比较高的形状:

当插值为0时限制范围是椭圆,插值为1限制范围是矩形

矩形的判断使用了线段相交函数,函数来自网络收集

线段相交: http://www.cnblogs.com/hont/p/6106043.html

代码如下

using UnityEngine;
using System;
using System.Collections; public class HUDTest2 : MonoBehaviour
{
[Serializable]
public class EllipseSetting
{
public float radius = 2f;
public Vector2 scale = new Vector2(, );
} public Transform[] hudObjectArray;
public EllipseSetting ellipseSetting = new EllipseSetting();
[Range(, )]
public float lerp; void OnGUI()
{
for (int i = ; i < hudObjectArray.Length; i++)
{
var item = hudObjectArray[i]; var screenPosition = Camera.main.WorldToScreenPoint(item.transform.position);
screenPosition.z = ;
screenPosition.y = Screen.height - screenPosition.y;
screenPosition = HUDFix(screenPosition); GUI.Box(new Rect(screenPosition, Vector3.one * ), "");
}
} Vector3 HUDFix(Vector3 position)
{
var center = new Vector3(Screen.width, Screen.height, ) * 0.5f; var ellipsePoint = GetEllipsePoint(position);
var quadPoint = GetRectPoint(position); var finalPoint = Vector3.Lerp(ellipsePoint, quadPoint, lerp); if (Vector3.Distance(center, finalPoint) >= Vector3.Distance(center, position))
return position;
else
return finalPoint;
} Vector3 GetEllipsePoint(Vector3 currentPoint)
{
var center = new Vector3(Screen.width, Screen.height, ) * 0.5f;
var current = center + (currentPoint - center).normalized * ellipseSetting.radius; current.x *= ellipseSetting.scale.x;
current.y *= ellipseSetting.scale.y; return current;
} Vector3 GetRectPoint(Vector3 currentPoint)
{
var center = new Vector3(Screen.width, Screen.height, ) * 0.5f; const float INNER = ;
const float RAY = * ;
var p0 = new Vector3(INNER, INNER, );
var p1 = new Vector3(Screen.width - INNER, INNER, );
var p2 = new Vector3(INNER, Screen.height - INNER, );
var p3 = new Vector3(Screen.width - INNER, Screen.height - INNER, ); var dir = (currentPoint - center).normalized * RAY; var contractPoint = default(Vector3);
var r = GetIntersection(p0, p1, center, center + dir, out contractPoint);
if (r == )
return contractPoint; r = GetIntersection(p1, p3, center, center + dir, out contractPoint);
if (r == )
return contractPoint; r = GetIntersection(p0, p2, center, center + dir, out contractPoint);
if (r == )
return contractPoint; GetIntersection(p2, p3, center, center + dir, out contractPoint);
return contractPoint;
} int GetIntersection(Vector3 a, Vector3 b, Vector3 c, Vector3 d, out Vector3 contractPoint)
{
contractPoint = new Vector3(, ); if (Mathf.Abs(b.y - a.y) + Mathf.Abs(b.x - a.x) + Mathf.Abs(d.y - c.y)
+ Mathf.Abs(d.x - c.x) == )
{
if ((c.x - a.x) + (c.y - a.y) == )
{
//Debug.Log("ABCD是同一个点!");
}
else
{
//Debug.Log("AB是一个点,CD是一个点,且AC不同!");
}
return ;
} if (Mathf.Abs(b.y - a.y) + Mathf.Abs(b.x - a.x) == )
{
if ((a.x - d.x) * (c.y - d.y) - (a.y - d.y) * (c.x - d.x) == )
{
//Debug.Log("A、B是一个点,且在CD线段上!");
}
else
{
//Debug.Log("A、B是一个点,且不在CD线段上!");
}
return ;
}
if (Mathf.Abs(d.y - c.y) + Mathf.Abs(d.x - c.x) == )
{
if ((d.x - b.x) * (a.y - b.y) - (d.y - b.y) * (a.x - b.x) == )
{
//Debug.Log("C、D是一个点,且在AB线段上!");
}
else
{
//Debug.Log("C、D是一个点,且不在AB线段上!");
}
return ;
} if ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y) == )
{
//Debug.Log("线段平行,无交点!");
return ;
} contractPoint.x = ((b.x - a.x) * (c.x - d.x) * (c.y - a.y) -
c.x * (b.x - a.x) * (c.y - d.y) + a.x * (b.y - a.y) * (c.x - d.x)) /
((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y));
contractPoint.y = ((b.y - a.y) * (c.y - d.y) * (c.x - a.x) - c.y
* (b.y - a.y) * (c.x - d.x) + a.y * (b.x - a.x) * (c.y - d.y))
/ ((b.x - a.x) * (c.y - d.y) - (b.y - a.y) * (c.x - d.x)); if ((contractPoint.x - a.x) * (contractPoint.x - b.x) <=
&& (contractPoint.x - c.x) * (contractPoint.x - d.x) <=
&& (contractPoint.y - a.y) * (contractPoint.y - b.y) <=
&& (contractPoint.y - c.y) * (contractPoint.y - d.y) <= )
{ //Debug.Log("线段相交于点(" + contractPoint.x + "," + contractPoint.z + ")!");
return ; // '相交
}
else
{
//Debug.Log("线段相交于虚交点(" + contractPoint.x + "," + contractPoint.z + ")!");
return -; // '相交但不在线段上
}
}
}

两种屏幕HUD区域限制的做法(矩形,弧形)的更多相关文章

  1. 设置一个DIV块固定在屏幕中央(两种方法)

    设置一个DIV块固定在屏幕中央(两种方法) 方法一: 对一个div进行以下设置即可实现居中. <style> #a{ position: fixed; top: 0px; left: 0p ...

  2. jqGrid中实现radiobutton的两种做法

    http://blog.sina.com.cn/s/blog_4f925fc30102e27j.html   jqGrid中实现radiobutton的两种做法 ------------------- ...

  3. SqlServer保留几位小数的两种做法

    SqlServer保留几位小数的两种做法   数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 解决: 1. 使用 Round( ...

  4. HTML5工具做屏幕自适应的两种方法

    近一两年,HTML5在中国很火,也出了不少HTML5工具和模板.别的先不说,对于不同的H5工具,解决屏幕自适应问题的区别是什么? 简单来说,感应式设计是当用不同设备访问时,能够根据设备的宽度和高度对设 ...

  5. LVS+keepalived 的DR模式的两种做法

    LVS DR模式搭建 准备工作 三台机器: dr:192.168.13.15 rs1:192.168.13.16 rs2: 192.168.13.17 vip:192.168.13.100 修改DR上 ...

  6. Android 高级UI设计笔记23:Android 夜间模式之 两种常用方法(降低屏幕亮度+替换theme)

    1. 夜间模式 所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛.特别是一些新闻类App实现夜间模式是非常人性化的,增强用户体验. 2. 我根据网上的资料 以及自 ...

  7. [正经分析] DAG上dp两种做法的区别——拓扑序与SPFA

    在下最近刷了几道DAG图上dp的题目. 要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点. 第二道是洛谷上的NOI导刊题目<最长路 ...

  8. CSS实现自适应不同大小屏幕的背景大图的两种方法(转自简书)

    CSS实现自适应不同大小屏幕的背景大图的两种方法 一张清晰漂亮的背景图片能给网页加分不少,设计师也经常会给页面的背景使用大图,我们既不想图片因为不同分辨率图片变形,也不希望当在大屏的情况下,背景有一块 ...

  9. Android 截取手机屏幕两种实现方案解析

    近期在开发的过程中,遇到了一个须要截取屏幕保存为图片的需求,详细为截取webview的视图保存图片. 方法1:首先想到的思路是利用SDK提供的View.getDrawingCache()方法: pub ...

随机推荐

  1. mysql日期/时间转换为字符串

    将日期时间2016-05-13 16:07:50转化为字符串20160513 date_format select phone,  date_format(time, '%Y%m%d%H%i%s') ...

  2. ssm(spring mvc+mybatis)+netty4开发qiq

    发布时间:2018-10-30   技术:spring mvc+mybatis+nett4+layui   概述 简单快捷的IM方案,快速打造在线IM,可用于公司内网.外网通讯,客服系统等,实现了so ...

  3. 浅谈iOS中MVVM的架构设计

    MVVM就是在MVC的基础上分离出业务处理的逻辑到viewModel层. M:  Model层是API请求的原始数据,充当DTO(数据传输对象),当然,用字典也是可以的,编程么,要灵活一些.Model ...

  4. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  5. Nginx 和 PHP 的两种部署方式比较

    2种部署方式简介 第一种 前置1台nginx服务器做HTTP反向代理和负载均衡 后面多态服务器部署Nginx Web服务和php-fpm提供的fast cgi服务 第二种 前置1台nginx服务器做W ...

  6. UNIX 家族及Linux

    Unix成长为一个非私有的操作系统,是因为1956年的AT&T公司受命于联邦去经营电报电话服务.当然也可以开发软件,甚至那个软件可以有”合理”收费的许可证,但是这个公司却被禁止从事任何和计算机 ...

  7. dubbo 学习(5) dubbo多协议和多注册中心

    转载 http://blog.csdn.net/songjinbin/article/details/49498431 一.配置dubbo多协议模式 1.默认协议 Dubbo缺省协议采用单一长连接和N ...

  8. 富文本编辑器 CKeditor 配置使用

    作者:Tyler Ning出处:http://www.cnblogs.com/tylerdonet/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连 ...

  9. ViewPager 源码分析(一) —— setAdapter() 与 populate()

    写在前面 做安卓也有一定时间了,虽然常用控件都已大致掌握,然而随着 Android N 的发布,不自觉的愈发焦虑起来.说来惭愧,Android L 的 Material Design 库里的许多控件都 ...

  10. 常用的apache commons工具,直接使用,便于快速开发

    详情 :http://commons.apache.org/ Components Description Latest Version Released Attributes Runtime API ...