鸟语天空
UGUI-Misc
post by:追风剑情 2019-7-3 21:24

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

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);
        }
    }
}

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容