定义常量

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. Ios还是安卓的判断

    最近在做app的h5页面,涉及到一些小知识点 记录一下 1.微信屏蔽了下载的链接,所以在网页中添加的下载链接都要在浏览器中打开,这里需要一个提示用户在浏览器打开的提示弹框 //判断是否在微信终端打开 ...

  2. MFC开发(一)简单同步时间应用程序

    看了一个垃圾程序的架构,mmp真坑,自己费了一点功夫才搞定,就直接记录下吧,这个是windows简单的应用程序,但是里面有点复杂,我们需要首先建立一个基于mfc的appwinzard程序,(凭记忆写的 ...

  3. SpringBoot使用Nacos配置中心

    本文介绍SpringBoot如何使用阿里巴巴Nacos做配置中心. 1.Nacos简介 Nacos是阿里巴巴集团开源的一个易于使用的平台,专为动态服务发现,配置和服务管理而设计.它可以帮助您轻松构建云 ...

  4. 2017-11-4—稳态和暂态/瞬态(对运放积分电路的思考)[待仿真]

    先直接截图了,暂态或者说瞬态都是暂时的状态,是从一个稳定态到另一个稳定态的过程. 之所以要了解这个概念是因为对于使用运放搭建的模拟PID有很多的疑惑,比如负反馈没有电阻满不满足"虚短&quo ...

  5. DWM1000 测距原理简单分析 之 SS-TWR

    蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: DWM1000 超宽带测距,使用的TOF(time of fly) 的方式,也就是计算无线电磁波传输时 ...

  6. python提取xml属性导入Mysql

    xml文档来自ganglia-gmond端telnet localhost 8649产生出来的文档,由于ganglia每隔一段时间就更新数据,为了永久保存数据到MySQL中,就用python写了最开始 ...

  7. python之面向对象深入探测

    一 __doc__ 表示类的描述信息 #!/usr/bin/env python # -*- coding: utf-8 -*- class C: """ 这是个大美女 ...

  8. 04_ switch 练习 _ 你及格了吗

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. mobile_轮播图_style_left 版本

    mobile 轮播图 小圆点逻辑(排他) 1. 统一给所有 span 元素加 class=""; 2. 切换到谁,谁的 class="active"; 移动端轮 ...

  10. three.js 使用DragControls.js 拖动元素

    首先,引入js文件: <script type="text/javascript" src="./path/to/DragControls.js"> ...