定义常量

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. git更新Activemq在远程github上指定版本的源码步骤

    第一步:根据地址克隆源码 (activemq-5.9) $  git  clone   https://github.com/apache/activemq.git 第二步:查看远程源码的版本清单 ( ...

  2. JavaEE 之 后台验证+拦截器

    1.Validator后台验证 a.在web.xml中配置 <listener> <listener-class>org.springframework.web.context ...

  3. SA:利用SA算法解决TSP(数据是14个虚拟城市的横纵坐标)问题——Jason niu

    %SA:利用SA算法解决TSP(数据是14个虚拟城市的横纵坐标)问题——Jason niu X = [16.4700 96.1000 16.4700 94.4400 20.0900 92.5400 2 ...

  4. 通过 ContentResolver 读取联系人信息

    1.首先动态获取 读取联系人信息权限    <1>配置文件中声明对应权限 ) } ] == PackageManager.PERMISSION_GRANTED) { readContact ...

  5. Spark环境搭建(三)-----------yarn环境搭建及测试作业提交

    配置好HDFS之后,接下来配置单节点的yarn环境 1,修改配置文件 文件 : /root/app/hadoop-2.6.0-cdh5.7.0/etc/hadoop/yarn-site-xml 插入 ...

  6. Python requests--初识接口自动化

    requests模块初级宝典:http://docs.python-requests.org/zh_CN/latest/user/quickstart.htmlrequests模块之葵花宝典:http ...

  7. 潭州课堂25班:Ph201805201 tornado 项目 第十二课 项目部署(课堂笔记)

    运行多个Tornado实例 网页响应不是特别的计算密集型处理 多个实例充分利用 CPU 多端口怎么处理 Linux 常见应用服务配置模式 nginx 和 supervisord:采用主配置文件 + 项 ...

  8. 白盒测试实践-day03

    一.任务进展情况 完成了基本的测试过程,下一步整理文档. 二.存在的问题 对于JUnit测试,对测试系统还不是太了解,导致测试脚本无法写. 三.解决方法 熟悉测试系统的结构,上网搜集资料.

  9. html中的列表

    无序列表(什么前面默认一个实心的黑原点) <ul> <li>什么</li> <li>什么</li> <li>什么</li& ...

  10. 小甲鱼零基础python课后题 P21 020函数:内嵌函数和闭包函数

    测试题 0.如果希望在函数中修改全局变量的值,应该使用什么关键字? 答:globe 1.在嵌套函数中,如果希望在内部函数修改外部函数的局部变量,应该使用什么关键字? 答:nonlocal 2.pyth ...