Unity如何检测sprite mssing?或为空? [英] Unity How to detect sprite mssing? or null?

查看:724
本文介绍了Unity如何检测sprite mssing?或为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测图像精灵是缺少"还是无"

I want to detect image sprite is "Missing" or "None"

当我得到这个精灵时,他们都像这样空", 那么,我怎么知道它是缺失"还是无"?

When I Get this sprite , They all got "null" like this , So How do I know it is "Missing" or "None" ?

PS:我想知道它丢失了还是没有,对我来说是不同的情况.

PS : I want to know it is missing or none , it is different situations for me.

推荐答案

由于尝试访问引用时引用null的原因不同,Unity会引发不同的异常!

Unity throws different exceptions for different reasons why the reference is null when trying to access them!

这也是您应已覆盖行为类型为Object== null(基本上是大多数Unity内置类型的父类),即使对象看起来是null,它仍会存储一些信息,如前所述-其原因null.

This is also the reason why you should strongly avoid someObject == null checks. Unity has overwritten the behavior of == null for the type Object (basically the mother class of most Unity built-in types) and even if an object appears to be null it still stores some information like - as just mentioned - the reason why it is null.

因此,您可以使用一些技巧",然后只需尝试访问一个字段并检查您在try - catch块中确切地获得了哪个异常:

So you could use a little "trick" and simply try to access a field and check which exception exactly you get within try - catch blocks:

public void CheckReference(Object reference)
{
    try
    {
        var blarf = reference.name;
    }
    catch (MissingReferenceException) // General Object like GameObject/Sprite etc
    {
        Debug.LogError("The provided reference is missing!");
    }
    catch (MissingComponentException) // Specific for objects of type Component
    {
        Debug.LogError("The provided reference is missing!");
    }
    catch (UnassignedReferenceException) // Specific for unassigned fields
    {
        Debug.LogWarning("The provided reference is null!");
    }
    catch (NullReferenceException) // Any other null reference like for local variables
    {
        Debug.LogWarning("The provided reference is null!");
    }
}


示例


Example

public class Example : MonoBehaviour
{
    public Renderer renderer;
    public Collider collider;

    private void Awake()
    {
        renderer = GetComponent<Renderer>();
        Destroy(renderer);
    }

    private void Update()
    {
        if (!Input.GetKeyDown(KeyCode.Space)) return;

        CheckReference(renderer); // MissingComponentException
        CheckReference(collider); // UnassignedReferenceException

        Sprite sprite = null;

        CheckReference(sprite);   // NullReferenceException

        sprite = Sprite.Create(new Texture2D(1, 1), new Rect(0, 0, 1, 1), Vector2.zero);
        DestroyImmediate(sprite);
        CheckReference(sprite);   // MissingReferenceException
    }

    public void CheckReference(Object reference)
    {
        try
        {
            var blarf = reference.name;
        }
        catch (MissingReferenceException) // General Object like GameObject/Sprite etc
        {
            Debug.LogError("The provided reference is missing!");
        }
        catch (MissingComponentException) // Specific for objects of type Component
        {
            Debug.LogError("The provided reference is missing!");
        }
        catch (UnassignedReferenceException) // Specific for unassigned fields
        {
            Debug.LogWarning("The provided reference is null!");
        }
        catch (NullReferenceException) // Any other null reference like for local variables
        {
            Debug.LogWarning("The provided reference is null!");
        }
    }
}

这篇关于Unity如何检测sprite mssing?或为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆