0%

Destroy

Destroy()函数调用之后,对象会销毁么?

Destroy(obj) delay Destroy之后,调用objC#类对象某些方法会抛MissingReferenceException异常,一些方法不会抛异常;
此时objC#类对象内存回收了么?
Destroy(obj) delay Destroy之后,将obj引用置空,会抛NullReferenceException异常;
此时objC#类对象内存回收了么?

Destroy()函数调用之后,引用是Null?

MissingReferenceException

NullReferenceException

Object基类重载了 == != bool 运算符

1
2
3
public static bool operator == (Object x,Object y);
public static bool operator != (Object x,Object t);
public static implicit operator bool(Object exists);

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Obj : MonoBehaviour
{
public string str;
public string str2;
public void setstr2(string _str2)
{
str2 = _str2;
}
public string getstr2()
{
return str2;
}
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Control : MonoBehaviour
{
public Obj m_obj;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
}

private void OnGUI()
{
//1.Destroy Script not m_obj = null時 拋異常 MissingReferenceException
//2.Destroy Script and m_obj = null時 拋異常 NullReferenceException
if (GUI.Button(new Rect(0, 0, 100, 100), "m_obj name"))
{
m_obj.name = "name" + Random.Range(1,1000);
}
//1.Destroy Script not m_obj = null時 有效,無異常
//2.Destroy Script and m_obj = null時 拋異常 NullReferenceException
if (GUI.Button(new Rect(100, 0, 100, 100), "m_obj str"))
{
m_obj.str = "testobj" + Random.Range(1, 1000);
}
//1.Destroy Script not m_obj = null時 有效,無異常
//2.Destroy Script and m_obj = null時 拋異常 NullReferenceException
if (GUI.Button(new Rect(200, 0, 100, 100), "m_obj setstr2"))
{
m_obj.setstr2("testobj" + Random.Range(1, 1000));
}
//1.Destroy Script not m_obj = null時 有效,無異常
//2.Destroy Script and m_obj = null時 拋異常 NullReferenceException
if (GUI.Button(new Rect(0, 100, 100, 100), "m_obj.GetHashCode()"))
{
Debug.Log(m_obj.GetHashCode());
} }

//1.Destroy Script not m_obj = null時 拋異常 MissingReferenceException
//2.Destroy Script and m_obj = null時 拋異常 NullReferenceException
if (GUI.Button(new Rect(100, 100, 100, 100), "m_obj.name"))
{
Debug.Log(m_obj.name);
}
//1.Destroy Script not m_obj = null時 有效,無異常
//2.Destroy Script and m_obj = null時 拋異常 NullReferenceException
if (GUI.Button(new Rect(200, 100, 100, 100), "m_obj.str"))
{
Debug.Log(m_obj.str);
}
//1.Destroy Script not m_obj = null時 有效,無異常
//2.Destroy Script and m_obj = null時 拋異常 NullReferenceException
if (GUI.Button(new Rect(300, 100, 100, 100), "m_obj.getstr2()"))
{
Debug.Log(m_obj.getstr2());
}

//1.Destroy Script not m_obj = null時 true
//2.Destroy Script and m_obj = null時 true
if (GUI.Button(new Rect(400, 100, 100, 100), "m_obj == null"))
{
Debug.Log(m_obj== null);
}

//1.Destroy Script not m_obj = null時 false
//2.Destroy Script and m_obj = null時 false
if (GUI.Button(new Rect(500, 100, 100, 100), "m_obj==true"))
{
Debug.Log(m_obj==true);
}

if (GUI.Button(new Rect(100,200,100,100),"Destroy GameObject"))
{
Destroy(m_obj.gameObject);

}

if (GUI.Button(new Rect(200, 200, 100, 100), "Destroy Script not m_obj = null"))
{
Destroy(m_obj);
}

if (GUI.Button(new Rect(300, 200, 100, 100), "Destroy Script and m_obj = null"))
{
Destroy(m_obj);
m_obj = null;
}
}
}