定义常量

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. 这篇文章主要介绍了Citrix XenServer 6.1 安装图解教程

    本次为使用VirtualBox虚拟机过安装测试机过程,我们在使用Vm(无论是Vbox还是VMware等)我们的CPU都必须可支持Intel-V或AMD-V,并且在VM软件设置和BIOS设置开启虚拟化支 ...

  2. Spark安装部署| 运行模式

    Spark 一种基于内存的快速.通用.可扩展的大数据分析引擎: 内置模块: Spark Core(封装了rdd.任务调度.内存管理.错误恢复.与存储系统交互): Spark SQL(处理结构化数据). ...

  3. IDEA中添加servlet的jar

    问题解决:办法1:使用Project Structure 方法二:使用Maven 在pom.xml文件中添加如下

  4. Python3-Cookbook总结 - 第一章:数据结构和算法

    第一章:数据结构和算法 Python 提供了大量的内置数据结构,包括列表,集合以及字典.大多数情况下使用这些数据结构是很简单的. 但是,我们也会经常碰到到诸如查询,排序和过滤等等这些普遍存在的问题. ...

  5. 表单组件 form fastadmin(生成表单元素)

    Form组件 定义文件位置: /extend/fast/Formphp 通用参数 $name 通常为我们组件的名称(name属性值),我们在后台接收时可以通过这个名称来获取到它所对应的值 $value ...

  6. vue加载优化策略

    vue.js是一个比较流行的前端框架,与react.js.angular.js相比来说,vue.js入手曲线更加流畅,不管掌握多少都可以快速上手.但是单页面应用也都有其弊病,有时候首屏加载慢的让人捏舌 ...

  7. Linux安装RocketMQ

    本文介绍Linux安装RocketMQ. 1.RocketMQ简介 RocketMQ是阿里巴巴中间件开发的分布式消息系统,曾经经历过很多阿里巴巴大型项目的实际检验.在去年已经正式捐献给Apache开源 ...

  8. PHP服务器时差8小时的解决办法

    PHP服务器时差8小时的解决办法 <?php date_default_timezone_set('Asia/Shanghai');  echo date("Y-m-d")? ...

  9. 前台js根据当前时间生成订单号

    *********前台显示框**************** <input type="text" id="WIDout_trade_no" name=& ...

  10. CSS3属性上调

    一.为什么使用CSS 1.有效的传递页面信息 2.使用CSS美化过的页面文本,使页面漂亮.美观,吸引用户 3.可以很好的突出页面的主题内容,使用户第一眼可以看到页面主要内容 4.具有良好的用户体验 二 ...