定义常量

public class Content
{
//当前UI分辨率
public const float UI_Width = 1366f;
public const float UI_Height = 768f; //手机屏幕大小的二分之一
public static float screen_width_half = Screen.width / ;
public static float screen_height_half = Screen.height / ; //手机屏幕与UI的比率
public static float screen_width_ratio = UI_Width / Screen.width;
public static float screen_height_ratio = UI_Height / Screen.height;
}

血条跟随代码实现

using UnityEngine;

public class UI_Follow : MonoBehaviour
{
public Camera m_camera;
public Transform m_target;
public RectTransform m_ui; private Vector3 position_sp; private void Awake()
{
if (m_camera == null)
{
m_camera = Camera.main;
}
if (m_ui == null && transform is RectTransform)
{
m_ui = (RectTransform)transform;
}
} private void LateUpdate()
{
if (m_target != null)
{
position_sp = m_camera.WorldToScreenPoint(m_target.position);
Format_Position(ref position_sp);
m_ui.localPosition = position_sp;
}
} private void Format_Position(ref Vector3 pos)
{
pos.x -= Content.screen_width_half;
pos.y -= Content.screen_height_half;
pos.x *= Content.screen_width_ratio;
pos.y *= Content.screen_height_ratio;
}
}

UGUI canvas.renderMode需设置为overlay,或camera但camera.projection需改为正交模式【在透视模式下会发生偏移】

实现拖动3D物体

using UnityEngine;

public class DragObject : MonoBehaviour
{
private Transform m_target; private Vector3 pos_target;
private Vector3 pos_mouse;
private Vector3 pos_temp; private void Update()
{
if (Input.GetMouseButtonDown())
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.CompareTag("Untagged"))
{
m_target = hit.transform;
pos_target = Camera.main.WorldToScreenPoint(m_target.position);
}
}
}
if (Input.GetMouseButton())
{
if (m_target != null)
{
pos_mouse = Input.mousePosition;
pos_mouse.z = pos_target.z;
pos_temp = Camera.main.ScreenToWorldPoint(pos_mouse);
m_target.position = pos_temp;
}
}
if (Input.GetMouseButtonUp())
{
if (m_target != null)
{
m_target = null;
}
}
}
}

UGUI血条跟随的更多相关文章

  1. UGUI血条

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; pu ...

  2. Unity UGUI实现分段式血条

    我们可以看到像英雄联盟等游戏里英雄头顶的血条显示并非是纯色的,而是根据血量的多少而显示一定量的格子,这种方式明显是比较友好.比较美观的,事实上我们的游戏里面也想实现这样的效果,那该怎么办呢?根据血量的 ...

  3. UGUI之Slider使用,制作血条

    用Slider来控制Cube旋转 Slider是滑动条.

  4. Unity UGUI HUD 怪物血条实现

    首先做一个血条,创建一个名为Follow3DObject的脚本添加到血条控件上. Follow3DObject.cs的代码如下: using UnityEngine; using System.Col ...

  5. unity如何显示血条(不使用NGUI)

    用unity本身自带的功能,如何显示血条? 显示血条,从资源最小化的角度,只要把一个像素的色点放大成一个矩形就足够,三个不同颜色的矩形,分别显示前景色,背景色,填充色,这样会消耗最少的显存资源. un ...

  6. 关于Unity中NGUI的3D角色血条的实现

    首先要到Unity的Assets Store里面去下载一个扩展的Package叫NGUI HUD Text v1.13(81),注意如果没有安装NGUI就必须先安装NGUI插件,否则会用不了,因为HU ...

  7. unity制作简单血条

    学习Unity已经10天了,也没发现有什么长进,真的急.昨天仿着官方Demo做了个射击游戏轮廓,其中需要给每个怪做一个血条. 搜了一些,挺复杂的,用NGUI或者UGUI,外加很长的代码...不过还是找 ...

  8. Shader实例:NGUI制作网格样式血条

    效果: 思路: 1.算出正确的uv去采样过滤图,上一篇文章说的很明白了.Shader实例:NGUI图集中的UISprite正确使用Shader的方法 2.用当前血量占总血量的百分比来设置shader中 ...

  9. UE4 使用UGM制作血条

    声明:本文是自己做的项目,可能不是最好的方法,或有错误使用方式.各位不喜勿喷! HP进度 HP背景 将上面的资源拖到UE4中(使用UE4自带的颜色也可实现效果,具体参考官方教程 https://doc ...

随机推荐

  1. UOJ#110. 【APIO2015】Bali Sculptures 贪心 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ110.html 题解 我们发现n=2000 的子任务保证A=1! 分两种情况讨论: $n\leq 100$ ...

  2. POJ 1201 Intervals (经典) (差分约束)

    <题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...

  3. (一)主域相同子域不同之document.domain跨域

    一.什么是主域名,什么是子域名? 主域名又称一级域名或者顶级域名,由域名主体.域名后缀组成,比如cnblogs.com : 子域名有二级域名,比如www.cnblogs.com.三级域名,比如home ...

  4. 2019-2-14sql server数据库模糊查询语句

    sql server数据库模糊查询语句   确切匹配: select * from hs_user where ID=123 模糊查询 select * from hs_user where ID l ...

  5. Intellij IDEA导入eclipse项目配置jdk、tomcat到浏览器正常访问

    转发自:博客园---Lindp(大佬写的甚好) 以下是转发的正文 intellij idea中文资料网上比较少,对于eclipse的项目如何导入intellij idea也没有完整的说明,本人在这里整 ...

  6. MVC 微信开发获取用户OpenID

    第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来 1.首先得有appid和appsecret . public class WeiXin { public static string ...

  7. Nginx+Https自己敲命令生成证书

    nginx配置https访问 一.准备 环境:centos6.8 nginx:1.13.6 二.开始       首先安装依赖包: yum install -y gcc gcc-c++ autocon ...

  8. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

    1.在vs编写时出现这个问题(以下为网络查询结果) 问题的原因是“SSL: CERTIFICATE_VERIFY_FAILED”. Python 升级到 2.7.9 之后引入了一个新特性,当使用url ...

  9. 关于redis分布式锁实现原理

    具体详情 http://www.cnblogs.com/SUNSHINEC/p/8302540.html

  10. PHP字符串处理 单引号 双引号 heredoc nowdoc 定界符

    ---恢复内容开始--- 2019年2月22日09:49:54 参考文档:   http://php.net/manual/zh/language.types.string.php#language. ...