最终效果 前言 在这一集中我将使用Unity制作基于瓦片的网格库存系统。 就像在《逃离塔科夫》、《暗黑破坏神》或《流放之路》等游戏中一样。 素材下载 https://assetstore.unity.com/packages/2d/gui/icons/gui-parts-159068 图片配置 配置图片为重复 不懂UI画布适配查看:【Unity小技巧】最简单的UI设置适配方案 修改UI画布适配 新增UI图片,类型改为平铺,默认图片是256的,太大了,所以我们选择缩小4倍,每单位像素为4,同时注意修改轴心在左上角 获取格子坐标 新增ItemGrid代码 public class ItemGrid : MonoBehaviour { const float tileSizeWidth = 256 / 4; const float tileSizeHeight = 256 / 4; Vector2 positionOnTheGrid = new Vector2(); Vector2Int tileGridPosition = new Vector2Int();...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Articles by RectTransform rectTransform
Unity 鼠标控制 UI 放大、缩小、拖拽
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class UIDragZoom : MonoBehaviour, IDragHandler, IScrollHandler { private Vector2 originalSize; private Vector2 originalPosition; private RectTransform rectTransform; private float minSize = 30f; private float maxSize = 200f; private void Start() { rectTransform = GetComponent<RectTransform>(); originalSize = rectTransform.sizeDelta; originalPosition = rectTransform.anchoredPosition; } public void...
Unity中使用VR手柄射线触发UI事件_呼呼突突的博客-CSDN博客
创建射线检测,确定起点和终点 public class LineController : SingletonMono<LineController> { [HideInInspector] public Vector3 pointStartPos, pointEndPos; [HideInInspector] public Vector3 lineDirection; [HideInInspector] public float lineRealLength; public Hand hand; public float rayLength = 20.0f; public LayerMask layerMask; private IEventHandle_VR currentTarget; public bool hasTarget { get { return currentTarget != null; } } public Action OnEnterEvent; public Action OnExitEvent; public Action OnClickEvent;...
Unity Text文字超过长度显示为省略号
亲测可用 有这样一个需求,文字超过多少字,或者超过几排,就显示为省略号,这样既可以让用户看到内容,如果内容过多,也不会太复杂繁琐,特别是在聊天中最能体现它的优势,效果如下图: 思路:使用TextGenerator,预先得到文本框能显示字数,行数,高度,宽度等,用于逻辑开发。 获取宽高,TextGenerator为Unity文字生成类,text.GetGenerationSettings(vertec2 param)得到当前text的生成配置(字号,字体,间距等),参数意思:生成器将尝试使文本适合此范围。将文本生成到这个宽高的text中去,它会决定生成之后行、列数。例如高度比较小(20左右)那么生成之后就只有一行。 官方文档UnityEngine.TextGenerationSettings - Unity 脚本 API GetPreferredHeight和GetPreferredHeight这两个方法,如果传入单个字,输出就是单个字的宽高;如果传入长串字符,则是长串总宽高,注意:在刚才传入的范围内。...
RectTransform详解_静水流深&1111的博客-CSDN博客
简介 RectTransform组件继承自Transform, 它控制UI对象的位置,尺寸,旋转和坐标系原点等信息。 除了Canvas外,每个带有RectTransform组件的对象并不能单独存在,必须是另一个带有RectTransform组件对象的子节点。 这也能说明为什么当我们创建一个UI对象的时候,Unity会自动为我们创建一个Canvas对象,并且将该对象至于Canvas的子节点。 Anchors Anchors是UI对象在其父节点矩形区域的锚心位置,它由两个值组成,anchorMin和anchorMax。 anchorMin和anchorMax是一个Vector2类型的变量,x,y 的取值在0-1之间,对应于所在点位置占据父节点区域的对应轴向上的百分比。那么父节点的左下角就是Vector2(0,0), 右上角则为(1,1)。 当我们设置anchorMin和anchorMax的值的时候,将会出现以下几种情形。 anchorMin == anchorMax 当锚心位置重合,可设置属性如上图。锚心点重合又称作绝对布局,即UI对象的大小并不受父节点的影响,保持不变。位置信息则是...
Unity TMPro实现文本框自动随文本内容伸缩
主要用于给会变化的文本做一个背景板 思路如下: 1、背景为父物体,带有tmp的物体为子物体 我这里的背景是默认image,用其他的UI组件当背景也可以 2、给父物体挂上Horizontal Layout Group和Content Size Fitter Content Size Fitter的Horizontal Fit 配置Preferred Size可以让子物体rect 宽度拉伸带动父物体宽度拉伸,Vertical则是高度拉伸带动父物体高度拉伸 这部分来源于unity官方文档:使 UI 元素适应其内容的大小 - Unity 手册 PS.这个Child Force Expand是默认勾选的,但是我自己尝试后发现勾和不勾效果完全相同,所以取消了勾选,,感兴趣的小伙伴可以看看手册再研究研究 3、现在我们尝试去拖动子物体的矩形框,发现子物体的宽度变换已经可以带动父物体的宽度变换了,但是现在问题来了,unity本身的TMP组件只有AutoSize只能实现TMP Text的字号随着文本内容的变化而自动调整,不能变换其矩形外框的长宽。所以我们需要写一个脚本来让它随文字长度变换矩形宽度...
Show More
loading
Actions
Is this you?
As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.Get in touch with RectTransform
Contact RectTransform, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack