您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 2浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 使用this关键字进行函数拓展 - Text

CoderZ1010 发布时间:2021-09-25 11:22:46 ,浏览量:2

Example:

using UnityEngine;
using UnityEngine.UI;

using SK.Framework;

public class Foo : MonoBehaviour
{
    private void Start()
    {
        GetComponent()
            //设置文本内容
            .SetContent("Today is a good day.")
            //设置字体
            .SetFont(Resources.Load(""))
            //设置字体样式
            .SetFontStyle(FontStyle.Bold)
            //设置行间距
            .SetLineSpacing(1.5f)
            //设置富文本
            .SetRichText(true)
            //设置对齐方式
            .SetAlignment(TextAnchor.MiddleCenter)
            //设置水平溢出
            .SetHorizontalOverflow(HorizontalWrapMode.Overflow)
            //设置垂直溢出
            .SetVerticalOverflow(VerticalWrapMode.Overflow)
            //设置大小适应
            .SetBestFit(false);
    }
}

Extension:

using UnityEngine;
using UnityEngine.UI;

namespace SK.Framework
{
    /// 
    /// Text相关拓展
    /// 
    public static class TextExtension
    {
        /// 
        /// 设置Text内容
        /// 
        /// 类型
        /// 实例
        /// 
        /// 实例
        public static T SetContent(this T self, string content) where T : Text
        {
            self.text = content;
            return self;
        }
        /// 
        /// 设置字体
        /// 
        /// 类型
        /// 实例
        /// 字体
        /// 实例
        public static T SetFont(this T self, Font font) where T : Text
        {
            self.font = font;
            return self;
        }
        /// 
        /// 设置字体样式
        /// 
        /// 类型
        /// 实例
        /// 字体样式
        /// 实例
        public static T SetFontStyle(this T self, FontStyle fontStyle) where T : Text
        {
            self.fontStyle = fontStyle;
            return self;
        }
        /// 
        /// 设置行间距
        /// 
        /// 类型
        /// 实例
        /// 行间距
        /// 实例
        public static T SetLineSpacing(this T self, float lineSpacing) where T : Text
        {
            self.lineSpacing = lineSpacing;
            return self;
        }
        /// 
        /// 设置是否支持富文本
        /// 
        /// 类型
        /// 实例
        /// 是否支持富文本
        /// 实例
        public static T SetRichText(this T self, bool richText) where T : Text
        {
            self.supportRichText = richText;
            return self;
        }
        /// 
        /// 设置对齐方式
        /// 
        /// 类型
        /// 实例
        /// 对齐方式
        /// 实例
        public static T SetAlignment(this T self, TextAnchor alignment) where T : Text
        {
            self.alignment = alignment;
            return self;
        }
        /// 
        /// 设置AlignByGeometry
        /// 
        /// 类型
        /// 实例
        /// AlignByGeometry
        /// 实例
        public static T SetAlignByGeometry(this T self, bool alignByGeometry) where T : Text
        {
            self.alignByGeometry = alignByGeometry;
            return self;
        }
        /// 
        /// 设置HorizontalOverflow
        /// 
        /// 类型
        /// 实例
        /// HorizontalOverflow
        /// 实例
        public static T SetHorizontalOverflow(this T self, HorizontalWrapMode horizontalWrapMode) where T : Text
        {
            self.horizontalOverflow = horizontalWrapMode;
            return self;
        }
        /// 
        /// 设置VerticalOverflow
        /// 
        /// 类型
        /// 实例
        /// VerticalOverflow
        /// 实例
        public static T SetVerticalOverflow(this T self, VerticalWrapMode verticalWrapMode) where T : Text
        {
            self.verticalOverflow = verticalWrapMode;
            return self;
        }
        /// 
        /// 设置BestFit
        /// 
        /// 类型
        /// 实例
        /// BestFit
        /// 实例
        public static T SetBestFit(this T self, bool bestFit) where T : Text
        {
            self.resizeTextForBestFit = bestFit;
            return self;
        }
    }
}
关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.3580s