unity destro pfxtyimmediate 在什么空间下

c# - Unity - OnDestroy - When is it called? - Game Development Stack Exchange
to customize your list.
Game Development Stack Exchange is a question and answer site for professional and independent game developers. J it only takes a minute:
Here's how it works:
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
I was wondering about the method called
in Unity (4.5.2f1) on Windows 8.1 Update 1. I know garbage collection in C# in non-deterministic, so I was wondering if game objects had
called the moment I called
on them, or when the garbage collector was invoked?
If this is not the case, what are my alternatives? I could of course just call a method inside of the object to be destroyed just before I invoke destroy, but there may be a more elegant solution available that I don't know about.
Note: I did try checking this myself, but being non-deterministic I may not be able to rely on that behavior occurring every time.
Destroy(); is an explicit command to remove the object from the game scene immediately* or after a set time increment. As soon as you call it - the item is destroyed in context of the scene.
Garbage collection will take it as soon as there are no more
when it is ready assuming there are no more hard references to that item. However, this is not controllable in Unity without calling the GC system object.
* clarification - immediately meaning in that frame. After the Update loop the item is destroyed, it will always be done before rendering
Some clarifications.
Only memory allocated by scripts is managed and can be garbage collected.
So if you destroy a GameObject the attached script can be eventually collected, but the life time of resources allocated from the C++ side of the engine is managed in a different way.
You can force a garbage collection explicitely call . If you don't force it, it's up to garbage collector decide when free the memory with zero ref count.
For what concern
I cite the doc:
This function should only be used when writing editor code since the
delayed destruction will never be invoked in edit mode. In game code
you should use Object.Destroy instead. Destroy is always delayed (but
executed within the same frame).
I think the main reasons for that are 2:
Other objects may try to access the deleted object within the same frame, so it's better to let it leave until all Update functions have been called.
From a memory management point of view it could be convenient to handle allocations/deallocations in specific moment.
Despite you destroy or destroy immediate an object, the used resources aren't necessary unallocated immediately.
Just an example, suppose your destroyed GameObject has a Material referencing a Texture, even if that resource is used only by the destroyed object, it's still up to unity decides when deallocate the relative memory.
If you want to force the release of unused resources you can have a look at .
As DarioOO pointed out, Unity overrides the comparison operator, in such a way that when an object has been destroyed all C# references to it are null (even if the related resources aren't still been released).
1,69911330
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25390
Game Development Stack Exchange works best with JavaScript enabled正文内容加载中...
14:49 dahaiwuliang 阅读(620) 评论(0)
摘要: 研究了两个小时,终于搞定,哎,问题很简单,时间切花的很多,今天的问题是,通过javascript调用后台函数,实在是费解,首先我要实现的是,textbox控件失去焦点时就调用事件来处理我的逻辑,可惜.net中的textbox没有这个事件,只有通过javascript也写,我给1个textbox控件添加1个属性: this.TextBox1.Attributes.Add("onblur", "myf...
00:29 dahaiwuliang 阅读(166) 评论(0)1、Resource.load(string path)优点:同步,使用方便缺点:只能加载Resource目录下的资源
2、WWW优点:灵活,可以加载Application.streamingAssetsPath、Application.persistentDataPath目录下的资源,以及从网络上下载资源缺点:异步,如果业务需要按需加载资源,容易打散逻辑
3、AssetBundle.CreateFromFile(string path)优点:同步,可以加载Application.persistentDataPath目录下的AssetBundle缺点:AssetBundle不能压缩,在Android下不能加载Application.streamingAssetsPath下的AssetBundle
4、AssetBundle.CreateFromMemoryImmediate(byte[] binary)优点:同步,可以加载Application.persistentDataPath目录下压缩过的AssetBundle缺点:在Android下不能加载Application.streamingAssetsPath下的AssetBundle
阅读(...) 评论()首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止依然没有更新正确的示例代码。
// C# Example
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to "Menu" -& "Assets" and select one of the choices
// to build the Asset Bundle
using UnityE
using UnityE
using System.IO;
public class ExportAssetBundles {
[MenuItem("Assets/Build AssetBundle Binary file From Selection - Track dependencies ")]
static void ExportResource () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
if (path.Length != 0) {
// Build the resource file from the active selection.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | pleteAssets);
Selection.objects =
FileStream fs = new FileStream(path,FileMode.Open,FileAccess.ReadWrite);
byte[] buff = new byte[fs.Length+1];
fs.Read(buff,0,(int)fs.Length);
buff[buff.Length-1] = 0;
fs.Close();
File.Delete(path);
string BinPath = path.Substring(0,path.LastIndexOf('.'))+".bytes";
FileStream cfs = new FileStream(BinPath,FileMode.Create);
cfs.Write(buff,0,buff.Length);
cfs.Close();
string AssetsPath = BinPath.Substring(BinPath.IndexOf("Assets"));
Object ta = AssetDatabase.LoadAssetAtPath(AssetsPath,typeof(Object));
BuildPipeline.BuildAssetBundle(ta, null, path);
[MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
static void ExportResourceNoTrack () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
if (path.Length != 0) {
// Build the resource file from the active selection.
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
  把打包的文件转换成了binary文件并多加了一个字节加密。
  当bytes文件生成好后再选中它,使用"Assets/Build AssetBundle From Selection - No dependency tracking"再次打包。
using UnityE
using System.C
public class WWWLoadTest : MonoBehaviour
public string BundleURL;
public string AssetN
IEnumerator Start()
WWW www =WWW.LoadFromCacheOrDownload(BundleURL,2);
yield return
TextAsset txt = www.assetBundle.Load("characters",typeof(TextAsset)) as TextA
data = txt.
byte[] decryptedData = Decryption(data);
Debug.LogError("decryptedData length:"+decryptedData.Length);
StartCoroutine(LoadBundle(decryptedData));
IEnumerator LoadBundle(byte[] decryptedData){
AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);
yield return
AssetBundle bundle = acr.assetB
Instantiate(bundle.Load(AssetName));
byte[] Decryption(byte[] data){
byte[] tmp = new byte[data.Length-1];
for(int i=0;i&data.Length-1;i++){
tmp[i] = data[i];
的作用就是下载并缓存资源,要注意后面的版本号参数,如果替换了资源却没有更新版本号,客户端依然会加载缓存中的文件。
www.assetBundle.Load("characters",typeof(TextAsset)) as TextAsset& //characters是加密文件的名字
AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);&&&&&&&&&&&
这句是官网最坑爹的,AssetBundle.CreateFromMemory明明返回的是AssetBundleCreateRequest官网却写得是AssetBundle,而且AssetBundleCreateRequest是一个异步加载,必须用协程的方式加载官网也没有提到。跟多兄弟就倒在了这里
阅读(...) 评论()&在写编辑器的过程中,想要动态销毁场景里面的游戏体时,被unity说不能用Destory,只能用DestoryImmediate,查看这个函数的api,该函数解释:
该函数只在写编辑器代码时使用,因为延时的销毁永远不会在编辑模式下调用。在游戏代码推荐使用代替。销毁总是延迟的(但在同一帧内执行),小心使用该函数,因为它能永久销毁资源。
看似没问题,直接使用了DestrotyImmediate
1 private void DeleteTrans()
foreach (Transform tran in transform)
if (tran.name == "bullet" || tran.name == "effect")
DestroyImmediate(tran.gameObject);
结果发现,不能删尽,再回头看DestoryImmediate的api解释,是不是DestoryImmediate也有延迟,或者说某种机制让他没有删尽。个人觉得,因为Transform是个迭代器类型,删除的过程中迭代器内容发生改变而不能删尽。尝试修正如下:
1 private void DeleteTrans()
List&Transform& trans = new List&Transform&(GetTransform&Transform&());
foreach (Transform tran in trans)
if (tran.name == "bullet" || tran.name == "effect")
DestroyImmediate(tran.gameObject);
private IEnumerable&T& GetTransform&T&()
IEnumerator tor = gameObject.transform.GetEnumerator();
while (tor.MoveNext())
object current = tor.C
yield return (T)
可以看出解决方案是先获取出所有节点存放在链表中,然后循环删除,不知道&List&Transform& trans = new List&Transform&(GetTransform&Transform&());这句是不是就是等待找出所有节点。.net没有帧的概念,unity的yield是携程返回,每帧都再调用。希望有高人解释一下。
阅读(...) 评论()

我要回帖

更多关于 unity3d 命名空间 的文章

 

随机推荐