UguiText字体自适应文本框的大小

unity文本大小自适应文本框第一种方法

img_30994891684de95219d38d2d45774b69.png
图片.png
using System.Collections;
using System.Collections.Generic;
using UnityEditor.VersionControl;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public enum StartAxisType
    {
        Horizontal,
        Vertical
    }

    public bool mAdaptiveSize = false;
    public StartAxisType mStartAxis = StartAxisType.Horizontal;
    public int mBeginFontSize;
    public Vector2 mRectSize;
    public Text mText;
    void Awake()
    {
        mRectSize = transform.GetComponent<RectTransform>().sizeDelta;
        mText = transform.GetComponent<Text>();
        mBeginFontSize = mText.fontSize;
    }

    void Start()
    {
        if (!mAdaptiveSize)
            return;
        switch (mStartAxis)
        {
            case StartAxisType.Horizontal:
                while (mText.preferredWidth > mRectSize.x)
                {
                    mText.fontSize -= 1;
                }
                break;
            case StartAxisType.Vertical:
                while (mText.preferredHeight > mRectSize.y)
                {
                    mText.fontSize -= 1;
                }
                break;
        }
    }
}

第二种,直接设置text上的BestFit属性为true即可

img_135a834881f40cdbd2a9fa31f6803f11.png
图片.png
相关推荐
<div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;"> </span> <div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;"> </span> <div style="color:rgba(0,0,0,.75);"> <div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;">当前课程中商城项目的实战源码是我发布在 GitHub 上的开源项目 newbee-mall (新蜂商城),目前已有 6300 多个 star,</span><span style="color:#4d4d4d;">本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 商城项目功能的讲解,让大家实际操作并实践上手一个大型的线上商城项目,并学习到一定的开发经验以及其中的开发技巧。<br /> 商城项目所涉及的功能结构图整理如下:<br /> </span> </div> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <p style="color:#4d4d4d;"> <img alt="modules" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3N0b3JlL25ld2JlZS1tYWxsLXMucG5n?x-oss-process=image/format,png" /> </p> </div> <p style="color:rgba(0,0,0,.75);"> <strong><span style="color:#e53333;">课程特色</span></strong> </p> <p style="color:rgba(0,0,0,.75);">   </p> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <ul> <li> 对新手开发者十分友好,无需复杂的操作步骤,仅需 2 秒就可以启动这个完整的商城项目 </li> <li> 最终的实战项目是一个企业级别的 Spring Boot 大型项目,对于各个阶段的 Java 开发者都是极佳的选择 </li> <li> 实践项目页面美观且实用,交互效果完美 </li> <li> 教程详细开发教程详细完整、文档资源齐全 </li> <li> 代码+讲解+演示网站全方位保证,向 Hello World 教程说拜拜 </li> <li> 技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,可以进一步提升你的市场竞争力 </li> </ul> </div> <p style="color:rgba(0,0,0,.75);">   </p> <p style="color:rgba(0,0,0,.75);"> <span style="color:#e53333;">课程预览</span> </p> <p style="color:rgba(0,0,0,.75);">   </p> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <p style="color:#4d4d4d;"> 以下为商城项目的页面和功能展示,分别为: </p> </div> <div style="color:rgba(0,0,0,.75);"> <ul> <li> 商城首页 1<br /> <img alt="" src="https://img-bss.csdnimg.cn/202103050347585499.gif" /> </li> <li> 商城首页 2<br /> <img alt="" src="https://img-bss.csdn.net/202005181054413605.png" /> </li> <li>   </li> <li> 购物车<br /> <img alt="cart" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3QvY2FydC5wbmc?x-oss-process=image/format,png" /> </li> <li> 订单结算<br /> <img alt="settle" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3Qvc2V0dGxlLnBuZw?x-oss-process=image/format,png" /> </li> <li> 订单列表<br /> <img alt="orders" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3Qvb3JkZXJzLnBuZw?x-oss-process=image/format,png" /> </li> <li> 支付页面<br /> <img alt="" src="https://img-bss.csdn.net/201909280301493716.jpg" /> </li> <li> 后台管理系统登录页<br /> <img alt="login" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3QvbWFuYWdlLWxvZ2luLnBuZw?x-oss-process=image/format,png" /> </li> <li> 商品管理<br /> <img alt="goods" src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9uZXdiZWUtbWFsbC5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20vcG9zdGVyL3Byb2R1Y3QvbWFuYWdlLWdvb2RzLnBuZw?x-oss-process=image/format,png" /> </li> <li> 商品编辑<br /> <img alt="" src="https://img-bss.csdnimg.cn/202103050348242799.png" /> </li> </ul> </div> </div> </div> </div>
Unity3D UGUI 专用文字特效插件 Text Effects 1.15 UI Text Effects are a set of effects for the standard Unity UI (uGUI) Text. - All script and shader sources included! - Extremely easy to use - just choose the effect from the component menu, and it's applied. - Add fancy titles, custom text appearance, mix multiple effects. - Rich Text support. - Mobile platform support. 12 extremely valuable effects: - Better Outline: a more continuous outline than the standard one. - Gradient Color: global/local, vertical/horizontal, override/additive/multiply. - Depth Effect: add thickness to text. - Soft Shadow: blurry shadow. - Outer Bevel: add outer lit and shaded edges. - Skew Effect: add horizontal+vertical transformations and perspective. - Curve Effect: bend or distort text vertically. - Character Spacing: increase or decrease the distance between individual characters. - Limit Visible Characters: hide characters, make a typewriter. - Overlay Texture: add an image overlay, local/global, override/additive/multiply. - Inner Bevel: add lit and shaded edges inside the characters, override/additive/multiply (only "override" on SM2 level GPUs). - Inner Outline: add outline inside the characters, override/additive/multiply. - With this package, you can finally echo effects from Photoshop or Word, while still using the standard Unity UI Text. - Suggestions for new effects are very welcome. The newly made effects will be added to the package. 仅供学习交流使用,如有侵权请告知删除。
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页