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; } }
在文本添加自适应组件，剩下的就是一句话：Img_Bg.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Txt_Tips.preferredHeight);
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"); }
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); } }
很多时候策划想要一个列表横向拖拽并且实现图标放大缩小的效果，根据这个效果写了以下代码：[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; gameEntry.Btn.interactable = true; } }); //移动到选中的位置 Sv_Games.enabled = false; float x = -(selectIndex * _dis); _svRect.DOKill(); _svRect.DOAnchorPosX(x, 0.2f).onComplete = () => { Sv_Games.enabled = true; }; //刷新界面 }[/tab][tab name="无按钮拖拽选中"]直接两个对象拖拽对象所需接口：IBeginDragHandler IDragHandler IEndDragHandler展示对象：Rt_Car/// /// 更新Cell缩放 /// private void FnUpdateCellScale() { for (int i = 0; i < mCarChangeCellPool.AllocatedLst.Count; i++) { var cell = mCarChangeCellPool.AllocatedLst[i]; if (i == mCurrCarIndex) { cell.rectTransform.DOScale(Vector3.one, 0.2f); } else { cell.rectTransform.DOScale(new Vector3(0.8f, 0.8f, 0.8f), 0.2f); } } } /// /// 移动到选中的位置 /// private void FnSwitchCurrCell() { float x = -(mCurrCarIndex * mDis); Rt_Car.DOAnchorPosX(x, 0.2f); } private void FnOnDragCallback(PointerEventData e) { //移动部分 float x = e.delta.x; Rt_Car.localPosition = new Vector3(Rt_Car.localPosition.x + x, Rt_Car.localPosition.y, Rt_Car.localPosition.z); //更新选中 (( 坐标 - ( 一半的间距 )) / 间距 ) - 1 int index = -(int)Math.Floor((Rt_Car.localPosition.x - (mDis / 2)) / mDis) - 1; int maxIndex = mCarChangeCellPool.AllocatedLst.Count - 1; if (index < 0) { index = 0; } else if (index > maxIndex) { index = maxIndex; } mCurrCarIndex = index; //更新Scale FnUpdateCellScale(); } private void FnOnOnEndDragCallback(PointerEventData obj) { FnSwitchCurrCell(); } [/tab][/tabs]