UGUI-Misc

作者:追风剑情 发布于:2019-7-3 21:24 分类:Unity3d

示例:演示了如何正确销毁对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class Misc
{
    // 销毁对象
    static public void Destroy(UnityEngine.Object obj)
    {
        if (obj != null)
        {
            if (Application.isPlaying)
            {
                if (obj is GameObject)
                {
                    GameObject go = obj as GameObject;
                    go.transform.parent = null;
                }

                Object.Destroy(obj);
            }
            else Object.DestroyImmediate(obj);
        }
    }

    // 立即销毁对象
    static public void DestroyImmediate(Object obj)
    {
        if (obj != null)
        {
            if (Application.isEditor) Object.DestroyImmediate(obj);
            else Object.Destroy(obj);
        }
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号