Toxic Star - UGUI
type: channel
link: https://toxicstar.top/index.php/category/UGUI/
items: Unity中使用代码控制TMP文本的材质
items: Unity背景高度自适应文本内容
items: Unity输入框限制最大最小值
items: Unity按钮图片自定义点击热区
items: Unity拖拽选中

Unity中使用代码控制TMP文本的材质
type: item
date: 2025-08-15T23:46:00+08:00
description: private const string materialName = "Common SDF Normal.mat"; private static Material _material_Normal; public static Material Material_Normal { get { if(_material_Normal is null) { // 从MaterialReferenceManager获取已加载的材质 int hash = CaculateTMPTagHash(materialName.Replace(".mat", "")); if (MaterialReferenceManager.TryGetMaterial(hash, out Material material)) { _material_Normal = material; } else { // 如果MaterialReferenceManager中没有，尝试通过UIManager加载 var mat = UIManager.instance.LoadTMPMaterial(materialName); MaterialReferenceManager.AddFontMaterial(hash, mat); _material_Normal = mat; } } return _material_Normal; } } public static int CaculateTMPTagHash(string tag) { if (string.IsNullOrEmpty(tag)) return 0; int hashCode = 0; for (int i = 0; i < tag.Length; i++) { int unicode = tag[i]; hashCode = (hashCode << 5) + hashCode ^ unicode; } return hashCode; } public void OnSelect_Color_141418_bbb5c8(TextMeshProUGUI tmp) { if (tmp is not null) { tmp.color = this.IsOn ? "#141418".ToColor32() : "#bbb5c8".ToColor32(); tmp.fontMaterial = this.IsOn ? Material_Normal : tmp.font.material; } }

Unity背景高度自适应文本内容
type: item
date: 2023-08-09T16:32:00+08:00
description: 在文本添加自适应组件，剩下的就是一句话：Img_Bg.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Txt_Tips.preferredHeight);

Unity输入框限制最大最小值
type: item
date: 2023-06-09T14:35:00+08:00
description: private void OnChangProductCount(string inputValueStr) { BuyPropCount = 0; if (!string.IsNullOrWhiteSpace(inputValueStr)) { int num = Convert.ToInt32(inputValueStr); num = num > 99 ? 99 : num; num = num < 0 ? 0 : num; //如果不是限购 就按输入的 if (CurSelectTable.PurchaseRestriction != 0) { //如果输入大于等于限购数 就等于限购数 if (num >= CurSelectTable.Number) { num = CurSelectTable.Number; } } BuyPropCount = num; } TmpInf_Input.text = BuyPropCount.ToString(); BuyTotalMoney = BuyPropCount * CurSelectTable.OriginalPrice[1]; Tmp_Price.text = BuyTotalMoney.ToWwwPrice("F0"); }

Unity按钮图片自定义点击热区
type: item
date: 2023-06-09T14:33:55+08:00
description: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// 为点击不规则图形而生,参与渲染 /// public class UIImageClick : Image { private Collider2D[] mColliders; // 可以带有多种碰撞区域 protected override void Awake() { base.Awake(); mColliders = GetComponents<Collider2D>(); if ((mColliders != null) && (mColliders.Length == 0)) { mColliders = null; } else { for (int i = 0; i < mColliders.Length; i++) { mColliders[i].enabled = false; // 等需要判断的时候打开，减少性能消耗 } } useLegacyMeshGeneration = false; } public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) { if (mColliders != null) { bool valid = false; for (int i = 0; i < mColliders.Length; i++) { var collider = mColliders[i]; collider.enabled = true; valid = collider.OverlapPoint(eventCamera.ScreenToWorldPoint(screenPoint)); collider.enabled = false; if (valid) { break; } } return valid; } return base.IsRaycastLocationValid(screenPoint, eventCamera); } }

Unity拖拽选中
type: item
date: 2023-06-09T11:02:00+08:00
description: 很多时候策划想要一个列表横向拖拽并且实现图标放大缩小的效果，根据这个效果写了以下代码：[tabs][tab name="有按钮拖拽选中" active="true"]代码：初始化需要的操作： private RectTransform _svRect = Sv_Games.GetComponent<RectTransform>(); Sv_Games.OnDragCallback = FnOnStartDragCallback; Sv_Games.OnEndDragCallback = FnOnEndDragCallback; 其它相关代码： /// /// 初始化游戏入口位置大小 /// private void FnInitGameEntry() { //初始化要在哪个位置 _curSelectIndex = 0; //更新 for (int i = 0, count = _gameEntryList.Count; i < count; i++) { var gameEntry = _gameEntryList[i]; gameEntry.Rect.localPosition = new Vector3(i * _dis, 0, 0); if (i == _curSelectIndex) { gameEntry.Rect.localScale = Vector3.one; } else { gameEntry.Rect.localScale = new Vector3(0.8f, 0.8f, 0.8f); } } //更新位置 Rt_Games.SetAnchorPosX(-(_curSelectIndex * _dis)); } /// /// 更新Cell缩放 /// private void FnUpdateCellScale() { for (int i = 0; i < _gameEntryList.Count; i++) { var gameEntry = _gameEntryList[i]; if (i == _curSelectIndex) { gameEntry.Rect.DOScale(Vector3.one, 0.2f); } else { gameEntry.Rect.DOScale(new Vector3(0.8f, 0.8f, 0.8f), 0.2f); } } } /// /// 开始拖拽 /// /// private void FnOnStartDragCallback(PointerEventData e) { //移动部分 float x = e.delta.x; int posX = (int)Mathf.Clamp(_svRect.anchoredPosition.x + x, -1050, 0); _svRect.anchoredPosition = new Vector2(posX, _svRect.anchoredPosition.y); //更新选中 (( 坐标 - ( 一半的间距 )) / 间距 ) - 1 int index = -(int)Math.Floor((_svRect.anchoredPosition.x - (_dis / 2)) / _dis) - 1; int maxIndex = _gameEntryList.Count - 1; if (index < 0) { index = 0; } else if (index > maxIndex) { index = maxIndex; } _curSelectIndex = index; //更新Scale FnUpdateCellScale(); } /// /// 结束拖拽 /// /// private void FnOnEndDragCallback(PointerEventData e) { FnGameSelect(_curSelectIndex); } /// /// 移动到选择的位置 进行操作 /// /// private void FnGameSelect(int selectIndex) { _curSelectIndex = selectIndex; _gameEntryList.ForEach(gameEntry => { if (selectIndex == gameEntry.Index) { gameEntry.Rect.localScale = Vector3.one; gameEntry.Btn.interactable = false; } else { gameEntry.Rect.localScale = Vector3.one * 0.8f; gameEntr…