UGUI血条跟随
定义常量
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血条跟随的更多相关文章
- UGUI血条
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; pu ...
- Unity UGUI实现分段式血条
我们可以看到像英雄联盟等游戏里英雄头顶的血条显示并非是纯色的,而是根据血量的多少而显示一定量的格子,这种方式明显是比较友好.比较美观的,事实上我们的游戏里面也想实现这样的效果,那该怎么办呢?根据血量的 ...
- UGUI之Slider使用,制作血条
用Slider来控制Cube旋转 Slider是滑动条.
- Unity UGUI HUD 怪物血条实现
首先做一个血条,创建一个名为Follow3DObject的脚本添加到血条控件上. Follow3DObject.cs的代码如下: using UnityEngine; using System.Col ...
- unity如何显示血条(不使用NGUI)
用unity本身自带的功能,如何显示血条? 显示血条,从资源最小化的角度,只要把一个像素的色点放大成一个矩形就足够,三个不同颜色的矩形,分别显示前景色,背景色,填充色,这样会消耗最少的显存资源. un ...
- 关于Unity中NGUI的3D角色血条的实现
首先要到Unity的Assets Store里面去下载一个扩展的Package叫NGUI HUD Text v1.13(81),注意如果没有安装NGUI就必须先安装NGUI插件,否则会用不了,因为HU ...
- unity制作简单血条
学习Unity已经10天了,也没发现有什么长进,真的急.昨天仿着官方Demo做了个射击游戏轮廓,其中需要给每个怪做一个血条. 搜了一些,挺复杂的,用NGUI或者UGUI,外加很长的代码...不过还是找 ...
- Shader实例:NGUI制作网格样式血条
效果: 思路: 1.算出正确的uv去采样过滤图,上一篇文章说的很明白了.Shader实例:NGUI图集中的UISprite正确使用Shader的方法 2.用当前血量占总血量的百分比来设置shader中 ...
- UE4 使用UGM制作血条
声明:本文是自己做的项目,可能不是最好的方法,或有错误使用方式.各位不喜勿喷! HP进度 HP背景 将上面的资源拖到UE4中(使用UE4自带的颜色也可实现效果,具体参考官方教程 https://doc ...
随机推荐
- java集成swagger
概览: java集成Swagger Swagger-UI的使用 Springboot跨域请求的访问解决 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...
- Hierarchical clustering:利用层次聚类算法来把100张图片自动分成红绿蓝三种色调—Jaosn niu
#!/usr/bin/python # coding:utf-8 from PIL import Image, ImageDraw from HierarchicalClustering import ...
- bzoj3124: [Sdoi2013]直径 树形dp two points
题目链接 bzoj3124: [Sdoi2013]直径 题解 发现所有直径都经过的边 一定在一条直径上,并且是连续的 在一条直径上找这段区间的两个就好了 代码 #include<map> ...
- Python: 你不知道的 super
https://segmentfault.com/a/1190000007426467 Python: 你不知道的 super 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我 ...
- 12树莓派VNC远程桌面
2017-09-04 23:11:28 http://bbs.elecfans.com/forum.php?mod=viewthread&tid=583803&extra= 开 ...
- XIV Open Cup named after E.V. Pankratiev. GP of America
A. Ancient Diplomacy 建图,同色点间边权为$0$,异色点间边权为$1$,则等价于找一个点使得到它最短路最长的点的最短路最小,Floyd即可. 时间复杂度$O(n^3)$. #inc ...
- CSS,浮动及其影响
浮动(float): 让默认文档流(标准文档流)下的元素漂浮起来,水平排列. 通俗点来说,浮动可以让元素浮到第二层,而其他没有浮动的元素就往上排,而我们是俯视去看的,所以往上顶的那个元素就会被遮住,这 ...
- Codechef August Challenge 2018 : Chef at the River
传送门 (要是没有tjm(Sakits)的帮忙,我还真不知道啥时候能做出来 结论是第一次带走尽可能少的动物,使未带走的动物不冲突,带走的这个数量就是最优解. 首先这个数量肯定是下界,更少的话连第一次都 ...
- 使用JS获取input值
获取input值,设置input值 可以使用 $(".class") $("#id") $("input[name='name']") re ...
- CSS3_3D 变换
3D 变换 1. 在 2D 父元素中,绕轴旋转,效果需要想象 #ele_3d { width: transform: rotateX(2deg); } rotateX 为正,元素左上角往里跑...对象 ...