Skip to content

Commit

Permalink
Unity: Prevent destroy problem with ExecuteInEditMode
Browse files Browse the repository at this point in the history
  • Loading branch information
splhack committed Jun 6, 2014
1 parent 5ea0333 commit ade3ad9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
Binary file modified csharp/unity/dist/lwf.unitypackage
Binary file not shown.
3 changes: 2 additions & 1 deletion csharp/unity/renderer/bitmapfont/bitmapfont_renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public void Destruct()
ResourceCache cache = ResourceCache.SharedInstance();
cache.UnloadTexture(mData.textureName);
cache.UnloadData(mName);
Mesh.Destroy(mMesh);
if (!Application.isEditor)
Mesh.Destroy(mMesh);
mName = null;
}

Expand Down
3 changes: 2 additions & 1 deletion csharp/unity/renderer/bitmapfont/bitmapfont_resourcecache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public void UnloadTexture(string name)
Material material = item.Entity();
if (material.mainTexture != null)
m_textureUnloader((Texture2D)material.mainTexture);
Material.Destroy(material);
if (!Application.isEditor)
Material.Destroy(material);
m_textureCache.Remove(name);
}
}
Expand Down
25 changes: 20 additions & 5 deletions csharp/unity/renderer/combinedmesh/lwf_combinedmesh_factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,23 @@ public Factory(Data d, GameObject gObj,
mesh.MarkDynamic();
#endif

meshFilter = gameObject.AddComponent<MeshFilter>();
if (Application.isEditor) {
meshFilter = gameObject.GetComponent<MeshFilter>();
if (meshFilter == null)
meshFilter = gameObject.AddComponent<MeshFilter>();
} else {
meshFilter = gameObject.AddComponent<MeshFilter>();
}
meshFilter.sharedMesh = mesh;

meshRenderer = gameObject.AddComponent<UnityEngine.MeshRenderer>();
if (Application.isEditor) {
meshRenderer = gameObject.GetComponent<UnityEngine.MeshRenderer>();
if (meshRenderer == null)
meshRenderer =
gameObject.AddComponent<UnityEngine.MeshRenderer>();
} else {
meshRenderer = gameObject.AddComponent<UnityEngine.MeshRenderer>();
}
meshRenderer.castShadows = false;
meshRenderer.receiveShadows = false;

Expand All @@ -112,9 +125,11 @@ public override void Destruct()
meshRenderer.sharedMaterial = null;
ResourceCache.SharedInstance().UnloadTexture(data.name, textureName);
meshFilter.sharedMesh = null;
UnityEngine.MeshRenderer.Destroy(meshRenderer);
MeshFilter.Destroy(meshFilter);
Mesh.Destroy(mesh);
if (!Application.isEditor) {
UnityEngine.MeshRenderer.Destroy(meshRenderer);
MeshFilter.Destroy(meshFilter);
Mesh.Destroy(mesh);
}
base.Destruct();
}

Expand Down
31 changes: 20 additions & 11 deletions csharp/unity/renderer/common/lwf_unity_resourcecache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ public void UnloadTexture(string lwfName, string filename)
TextureContext context = item.Entity();
if (context.material.mainTexture != null)
context.unloader((Texture2D)context.material.mainTexture);
Material.Destroy(context.material);
if (!Application.isEditor)
Material.Destroy(context.material);
m_textureCache.Remove(cacheName);
}
}
Expand Down Expand Up @@ -356,7 +357,8 @@ public void UnloadMesh(string lwfName, int bitmapExId)
string cacheName = string.Format("{0}/{1}", lwfName, bitmapExId);
if (m_meshCache.TryGetValue(cacheName, out item)) {
if (item.Unref() <= 0) {
Mesh.Destroy(item.Entity().mesh);
if (!Application.isEditor)
Mesh.Destroy(item.Entity().mesh);
m_meshCache.Remove(cacheName);
}
}
Expand Down Expand Up @@ -391,9 +393,11 @@ public void DeleteRenderedMesh(string lwfName)
RenderedMeshItem item;
if (m_renderedMeshCache.TryGetValue(lwfName, out item)) {
if (item.Unref() <= 0) {
foreach (Mesh mesh in item.Entity()) {
if (mesh != null)
Mesh.Destroy(mesh);
if (!Application.isEditor) {
foreach (Mesh mesh in item.Entity()) {
if (mesh != null)
Mesh.Destroy(mesh);
}
}
m_renderedMeshCache.Remove(lwfName);
}
Expand All @@ -403,21 +407,26 @@ public void DeleteRenderedMesh(string lwfName)
public void UnloadAll()
{
foreach (RenderedMeshItem item in m_renderedMeshCache.Values) {
foreach (Mesh mesh in item.Entity()) {
if (mesh != null)
Mesh.Destroy(mesh);
if (!Application.isEditor) {
foreach (Mesh mesh in item.Entity()) {
if (mesh != null)
Mesh.Destroy(mesh);
}
}
}
m_renderedMeshCache.Clear();
m_lwfDataCache.Clear();
foreach (MeshItem item in m_meshCache.Values)
Mesh.Destroy(item.Entity().mesh);
if (!Application.isEditor) {
foreach (MeshItem item in m_meshCache.Values)
Mesh.Destroy(item.Entity().mesh);
}
m_meshCache.Clear();
foreach (TextureItem item in m_textureCache.Values) {
TextureContext context = item.Entity();
if (context.material.mainTexture != null)
context.unloader((Texture2D)context.material.mainTexture);
Material.Destroy(context.material);
if (!Application.isEditor)
Material.Destroy(context.material);
}
m_textureCache.Clear();
m_shaderCache.Clear();
Expand Down

0 comments on commit ade3ad9

Please sign in to comment.