Unity中文字体获取

Posted Kellen-Gram

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity中文字体获取相关的知识,希望对你有一定的参考价值。

Unity对中文字体支持较差,这里给出了几个可以使用的中文字体下载网站

后续会更新如何使用:

  1. 51Font -「免费字体发布平台」为您第一时间发布商用免费字体开源字体 (17font.com)
  2. freefonts.top/

Unity:无法通过getcomponent获取对类的引用

【中文标题】Unity:无法通过getcomponent获取对类的引用【英文标题】:Unity: Can't get reference to a class via getcomponent 【发布时间】:2017-02-12 15:36:52 【问题描述】:
    using UnityEngine;

    public class CartMovement : MonoBehaviour 

    SpriteRenderer spriteRenderer;

    LevelManager LevelManIns;

    void Start () 

        spriteRenderer = GetComponent<SpriteRenderer>();

        spriteRenderer.enabled = true;

        (line25)LevelManIns = GetComponent<LevelManager>();

        Debug.Log("--" + LevelManIns.xy.X);
        //transform.position = LevelManIns.Tiles[LevelManIns.PortalGreen].GetComponent<TileScript>().transform.position;

        iTween.MoveTo(this.gameObject, iTween.Hash("path", iTweenPath.GetPath("cartPath"), "time", 3));
        
      

我得到错误:

NullReferenceException:对象引用未设置为对象的实例 CartMovement.Start () (在 Assets/scripts/CartMovement.cs:25)

我不明白为什么我无法获得对另一个脚本的引用。任何人都可以解决这个问题。谢谢。

【问题讨论】:

你确定LevelManIns = GetComponent();正在产生错误?因为这不会产生错误。如果它无法获取组件,它将返回 null。它必须是下面(或上面)的行或另一行。它所说的只是你试图调用一个空的东西。我最好的猜测是 GameObject 没有附加 LevelManager。 是的,levelmanager 是另一个游戏对象的脚本,但我需要访问此类中的一些属性。我怎样才能实现它? 【参考方案1】:

如果关卡管理器附加到另一个对象(如您在 cmets 中提到的),一种方法是通过检查器引用该对象,然后从该对象获取脚本。

public class CartMovement : MonoBehaviour 

SpriteRenderer spriteRenderer;
// Make it public, so it is visible in the inspector, and drag and drop the object into that instance
public LevelManager LevelManIns; 

    void Start () 
        // No need to assign it here, just maybe check if it is assigned like so
        if (LevelManIns == null)
            // Error, this should be assign outside
    

【讨论】:

以上是关于Unity中文字体获取的主要内容,如果未能解决你的问题,请参考以下文章

Unity C#,获取文本宽度/字体字符宽度

unity 获取子数据

unity获取raycast的XYZ

在 Unity 中获取英特尔实感深度流

Unity/C# 查找对象并获取组件

Unity:无法通过getcomponent获取对类的引用