👉关于作者
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!专注于 Android/Unity 和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
👉即将学会
了解到 Unity 发布 WebGL,可能遇到的问题以及解决方法。
👉背景
今天小芝🙎和小空🙈今天一起吃了昨天的剩饺子+泡面,更穷了!
👉实践过程
问题一
Text 字体不清晰
第一种可能是图片拉伸的问题,Text 控件长或宽被拉伸,导致单位面积像素点减少了(好像是称为像素密度),解决是对其 scale 进行成倍放大而不是对其拉伸
第二种 如果 Canvas 模式是一般相机模式(前两种),把 Canvas Scaler 的 Scale Factor 适当调大,字体会变清晰;
还有就是 Canvas 是 World 模式, 可以把 Canvas Scaler 的 Dynamic Pixels Per Unit 适当调大,这样 Text 每单元的动态 pixels 会增大,这样 vr 中的 3DText 字体会变的清晰。
问题二
Text 文字组件实现滑动变色
解决:添加 Selectable 组件就行
:
问题三
给 UGUI 精灵或按钮 添加自定义事件响应区域
所有 UI 都有 Image 组件,其中有 RaycastTarget 属性,勾选该属性为 true 则表示运行时 UI 精灵会响应相应交互事件,这套 UGUI(包括 NGUI)是通过射线检测实现的交互响应,那么我们可以通过添加可编辑碰撞器的方式,修改 Image 默认检测区域;
项目中我的按钮是这样的
如果你不做处理 默认是整张图片(即空白区域)都会响应,体验上是不太好的;
Unity 给我们提供了自定义区域,就是 PolygonCollider2D 组件
点击 EditCollide 会有小绿点出现让你编辑该多边形碰撞器(将区域设置有图片内容的区域);
还有我们要删除 button 原有的 Image 组件,新建一个 C#类且继承自 Image,把这个 C# 添加给 button,设置图片即可
using UnityEngine;using System.Collections;using UnityEngine.UI;public class CustomBtnArea : Image { public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) { return GetComponent<PolygonCollider2D>().OverlapPoint(screenPoint); }}
复制代码
官方的 Image 原生方法是这样的
跑起来,只有圈出的区域才响应
问题四
鼠标滑过 UI 检测碰撞位置,来实现提示信息;
注意:是 UI(2D)内容的碰撞,当然 3D 的也有;
using UnityEngine;using System.Collections;using UnityEngine.EventSystems;using UnityEngine.UI;//https://juejin.cn/user/4265760844943479/postspublic class BtnTips : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler{ public bool isShowTip; //是否展示提示 private string name;//物品名称 public Font this_font; //字体样式,方便显示中文 void Start () { isShowTip = false; } void Update () { } //这段注释的 是 3D物体的检测 //private void OnMouseEnter() //{ // Debug.Log("鼠标位置"); // isShowTip = true; //} //private void OnMouseExit() //{ // isShowTip = false; //} private void OnGUI() { if (isShowTip) { //Debug.Log("鼠标位置==="); GUIStyle style1 = new GUIStyle(); style1.fontSize = 20; style1.normal.textColor = Color.white; //style1.normal.textColor = new Color(0,0,0); //可以自定义任何颜色 style1.font = this_font; //自定义字体样式 GUI.Label(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y-20, 140, 60), name,style1); } } //下面是 2D UI 内容的鼠标划入检测 public void OnPointerExit(PointerEventData eventData) { isShowTip = false; name = ""; } public void OnPointerEnter(PointerEventData eventData) { isShowTip = true; name = gameObject.transform.GetChild(0).GetComponent<Text>().text; }}
复制代码
问题五
Button 组件设置不可点击且变灰,发现单纯的颜色按钮是可以的,如果是 Button 是精灵的就不行,目前只能是恰当的时机替换精灵 如设置不可点击 enabled=false 的时候把精灵替换成灰色图;反之一样
问题六
如果项目中 Text 组件多,且是中文,一个一个修改字体样式肯定劳神, 可一键替换字体样式资源,属于自定义编辑器的知识(EditorWindow) 注:过程中如果字体样式多样化 一定要注意,别全替换了;
using UnityEngine;using System.Collections;using UnityEditor;using UnityEditor.SceneManagement;using UnityEngine.UI; //https://juejin.cn/user/4265760844943479/postspublic class ChangeFontWindow : EditorWindow{ [MenuItem("Tools/更换字体")] public static void Open() { EditorWindow.GetWindow(typeof(ChangeFontWindow)); } Font toChange; static Font toChangeFont; FontStyle toFontStyle; static FontStyle toChangeFontStyle; void OnGUI() { toChange = (Font)EditorGUILayout.ObjectField(toChange, typeof(Font), true, GUILayout.MinWidth(100f)); toChangeFont = toChange; toFontStyle = (FontStyle)EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f)); toChangeFontStyle = toFontStyle; if (GUILayout.Button("更换")) { Change(); } } public static void Change() { Transform canvas = GameObject.Find("Canvas").transform; if (!canvas) { Debug.Log("NO Canvas"); return; } Transform[] tArray = canvas.GetComponentsInChildren<Transform>(); for (int i = 0; i < tArray.Length; i++) { Text t = tArray[i].GetComponent<Text>(); if (t) { //这个很重要,博主发现如果没有这个代码,unity是不会察觉到编辑器有改动的,自然设置完后直接切换场景改变是不被保存 //的 如果不加这个代码 在做完更改后 自己随便手动修改下场景里物体的状态 在保存就好了 Undo.RecordObject(t, t.gameObject.name); t.font = toChangeFont; t.fontStyle = toChangeFontStyle; //相当于让他刷新下 不然unity显示界面还不知道自己的东西被换掉了 还会呆呆的显示之前的东西 EditorUtility.SetDirty(t); } } Debug.Log("Succed"); }}
复制代码
👉其他
📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://www.infoq.cn/profile/DB2492B85795C4/publish
📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。
评论