实现2dUI跟随游戏中角色的移动(应用于玩家名称,血条,称号)

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()
{
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;
}
}
using UnityEngine;

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;
}

需要根据手机分辨率与UI进行适配

另一种解决方案:

每个3D物体身上都挂载一个Canvas,通过调整UI角度实现

unity UGUI UI跟随的更多相关文章

  1. Unity UGUI——UI基础,Canvas

    主题:画布--Canvas 内容:创建Canvas UI控件的绘制顺序 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTXJfQUhhbw==/font/5 ...

  2. Curved UI - VR Ready Solution To Bend Warp Your Canvas 1.7,1.8,2.2,2.3 四种版本压缩包(Unity UGUI曲面插件),可以兼容VRTK

    Curved UI - VR Ready Solution To Bend Warp Your Canvas 1.7,1.8,2.2,2.3 四种版本压缩包(Unity UGUI曲面插件) 可以兼容V ...

  3. Unity UGUI鼠标穿透UI问题(Unity官方的解决方法)

    简述 最近在用UGUI的时候遇到了鼠标穿透的问题,就是说在UGUI和3D场景混合的情况下,点击UI区域同时也会 触发3D中物体的鼠标事件.比如下图中 这里给Cube加了一个鼠标点击改变颜色的代码,如下 ...

  4. Unity UGUI —— 无限循环List

    还记得大学毕业刚工作的时候是做flash的开发,那时候看到别人写的各种各样的UI组件就非常佩服,后来自己也慢慢尝试着写,发现其实也就那么回事.UI的开发其实技术的成分相对来说不算多,但是一个好的UI是 ...

  5. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  6. Unity UGUI图文混排源码(一)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  7. Unity UGUI

    超详细的基础教程传送门:(持续更新中) Unity UGUI之Canvas&EventSystem:http://blog.csdn.net/qq992817263/article/detai ...

  8. Unity UGUI Layout自动排版组件用法介绍

    Unity UGUI布局组件 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...

  9. Unity UGUI 小知识

    1.有个控件叫Selectable 这个控件在button,slider等身上有,也可以自行添加,可通过API搜索所有带这个控件的物体统一控制. 2.实现ScrollView只使用Scrollbar操 ...

随机推荐

  1. VM虚拟机下安装CentOS_6.5_x64

    在开始Linux的学习之前,我们必须要有一个实验环境.这里我选择了安装CentOS-6.5-x86_64的Minimal版本,大家也可以选择安装其他的Linux发行版.选择CentOS主要还是因为它比 ...

  2. git踩过的坑

    一.git 解决fatal: Not a git repository 我用git add file添加文件时出现这样错误: fatal: Not a git repository (or any o ...

  3. CocoaPods更新过程中出现的坑及解决方法

    如果CocoaPods很久没有更新,那么在search.install.update,都会强制升级他的repo,然后如果安装他正常的安装程序,从github上下的时候,你会发现,这个过程不管你FQ不F ...

  4. 【中间件安全】WebSphere安全加固规范

    1. 适用情况 适用于使用WebSphere进行部署的Web网站. 2. 技能要求 熟悉WebSphere安装部署,熟悉WebSphere常见漏洞利用方式,并能针对站点使用WebSphere进行安全加 ...

  5. IDEA-各模块间引用出现问题的解决方法

    1 点击项目右上角的Project Structure 2 选择Modules->父项目->点击右上角的加号->添加需要依赖的模块

  6. c++ 模板相关

    https://blog.csdn.net/lezardfu/article/details/60466161 https://www.cnblogs.com/ymy124/p/3632634.htm ...

  7. docker 应用-4(swarm模式搭建集群)

    swam模式 使用docker的swarm模式,可以很方便的搭建docker engine集群.docker engine是docker 容器的运行时环境,可以在docker engine上build ...

  8. Elasticsearch设置最大返回条数

    在Elasticsearch中如果需要做分页查询,我们通常使用form和size实现.form指定从有序哪一行开始,size表示从当前开始读取多少行.但是我们发现查询结果最大只能到10000,这是因为 ...

  9. 禅道迁移(windows_to_linux)

    需求分析 随着禅道数据的增加,原来通过虚拟机提供的mysql服务器相应速度跟不上需求.且原来禅道的前端与数据库分离安装在windows与linux中,现在提供实体服务器,需要将禅道环境迁移. 确认环境 ...

  10. wpgcms---循环导航

    使用wpgcms的时候,在后台设置了导航菜单,那么在前端是如何循环呢? 第一种:简便方法 {% set array = [ {name:'移动APP',icon:'icon-yidongAPP',co ...