Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: laurentl on August 06, 2013, 12:51:46 AM

Title: bug with NGUI after moving prefab material and texture
Post by: laurentl on August 06, 2013, 12:51:46 AM
after I moved the atlas prefab, material and texture, I cannot add not update any of the texture
maybe that's not related
the video is this:
https://www.youtube.com/watch?v=BfzvYmC5naw

the error is this:
MissingReferenceException: The object of type 'Texture2D' has been destroyed but you are still trying to access it.
Title: Re: bug with NGUI after moving prefab material and texture
Post by: laurentl on August 06, 2013, 01:05:19 AM
Addition:
update/add creates this error
replace does not.
Title: Re: bug with NGUI after moving prefab material and texture
Post by: Ernest on August 06, 2013, 10:55:25 AM
In UpdateTexture(), do
  1. newTexture = true;
  2. tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  3.  
instead of update the texture. This is a hack, but it works...
Title: Re: bug with NGUI after moving prefab material and texture
Post by: laurentl on August 06, 2013, 05:31:56 PM
Where in the method?
Title: Re: bug with NGUI after moving prefab material and texture
Post by: ArenMook on August 07, 2013, 05:13:33 AM
He means UIAtlasMaker.UpdateTexture. This phantom bug is an odd one and is caused by something deep inside Unity that makes no sense, with no clear steps to reproduce it. Others reported that simply starting a new project and moving all the assets there fixed it, which is weird to say the least.

But in any case, Ernest suggested replacing that function with:
  1. static bool UpdateTexture (UIAtlas atlas, List<SpriteEntry> sprites)
  2.         {
  3.                 // Get the texture for the atlas
  4.                 Texture2D tex = atlas.texture as Texture2D;
  5.                 string oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
  6.                 string newPath = NGUIEditorTools.GetSaveableTexturePath(atlas);
  7.  
  8.                 // Clear the read-only flag in texture file attributes
  9.                 if (System.IO.File.Exists(newPath))
  10.                 {
  11.                         System.IO.FileAttributes newPathAttrs = System.IO.File.GetAttributes(newPath);
  12.                         newPathAttrs &= ~System.IO.FileAttributes.ReadOnly;
  13.                         System.IO.File.SetAttributes(newPath, newPathAttrs);
  14.                 }
  15.  
  16.                 NGUIEditorTools.ImportTexture(oldPath, true, false);
  17.                 tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  18.  
  19.                 // Pack the sprites into this texture
  20.                 if (PackTextures(tex, sprites))
  21.                 {
  22.                         byte[] bytes = tex.EncodeToPNG();
  23.                         System.IO.File.WriteAllBytes(newPath, bytes);
  24.                         bytes = null;
  25.  
  26.                         // Load the texture we just saved as a Texture2D
  27.                         AssetDatabase.SaveAssets();
  28.                         AssetDatabase.Refresh();
  29.                         tex = NGUIEditorTools.ImportTexture(newPath, false, true);
  30.  
  31.                         // Update the atlas texture
  32.                         if (tex == null) Debug.LogError("Failed to load the created atlas saved as " + newPath);
  33.                         else atlas.spriteMaterial.mainTexture = tex;
  34.                         ReleaseSprites(sprites);
  35.                                
  36.                         AssetDatabase.SaveAssets();
  37.                         AssetDatabase.Refresh();
  38.                         return true;
  39.                 }
  40.                 else
  41.                 {
  42.                         //Debug.LogError("Operation canceled: The selected sprites can't fit into the atlas.\n" +
  43.                         //      "Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead.");
  44.                        
  45.                         EditorUtility.DisplayDialog("Operation Canceled", "The selected sprites can't fit into the atlas.\n" +
  46.                                         "Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead", "OK");
  47.                         return false;
  48.                 }
  49.         }
Title: Re: bug with NGUI after moving prefab material and texture
Post by: laurentl on August 07, 2013, 05:05:52 PM
So I replaced the method and when I pres Update I am getting the same error in the PackTexture method (line 150)

MissingReferenceException: The object of type 'Texture2D' has been destroyed but you are still trying to access it.
Title: Re: bug with NGUI after moving prefab material and texture
Post by: laurentl on August 07, 2013, 05:10:44 PM
And I tried transfering the project over to a new project (pack, create, unpack) and the error remains
Title: Re: bug with NGUI after moving prefab material and texture
Post by: ArenMook on August 08, 2013, 09:50:15 AM
Line 150? Your line numbers don't match the latest. What version of NGUI are you running?

P.S. try switching to non-Unity packing (a checkbox on the atlas maker)
Title: Re: bug with NGUI after moving prefab material and texture
Post by: KlingOne on August 22, 2013, 04:14:39 AM
I have exactly the same problem.
And also in Line 150 and my NGUI version is the newest (2.6.4).
Switching to non-Unity Packing didn't help either.

The problem first occurd in an older version (somehtin like 2.0.x) were the bug would throw the error and create a new texture for the atlas only containing the textures i updated but missing the other ones. After updating to the newest version the bug remains with the only difference beeing that the atlas textures doesn't get modified.

  1. MissingReferenceException: The object of type 'Texture2D' has been destroyed but you are still trying to access it.
  2. Your script should either check if it is null or you should not destroy the object.
  3. UnityEngine.Texture.get_width () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/Graphics.cs:682)
  4. UIAtlasMaker.PackTextures (UnityEngine.Texture2D tex, System.Collections.Generic.List`1 sprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:150)
  5. UIAtlasMaker.UpdateTexture (.UIAtlas atlas, System.Collections.Generic.List`1 sprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:519)
  6. UIAtlasMaker.UpdateAtlas (.UIAtlas atlas, System.Collections.Generic.List`1 sprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:610)
  7. UIAtlasMaker.UpdateAtlas (System.Collections.Generic.List`1 textures, Boolean keepSprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:593)
  8. UIAtlasMaker.OnGUI () (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:908)
Title: Re: bug with NGUI after moving prefab material and texture
Post by: ArenMook on August 22, 2013, 10:22:30 AM
If you can give me a repro case where this happens 100%, I'd appreciate it. I've yet to run into this issue, as I mentioned.
Title: Re: bug with NGUI after moving prefab material and texture
Post by: KlingOne on August 23, 2013, 04:22:40 AM
I was able to trace the problem to having NGUI and Uni2D in the same project.
I've send you a link to a repro case via private message.
Title: Re: bug with NGUI after moving prefab material and texture
Post by: ArenMook on August 23, 2013, 02:34:59 PM
So it works fine without Uni2D? Does it do something funky with textures?
Title: Re: bug with NGUI after moving prefab material and texture
Post by: KlingOne on August 26, 2013, 03:12:50 AM
Yes, when I remove Uni2D from the project I can build the atlas.
And the atlas works fine when I reimport Uni2D afterwards.
I don't really know if Uni2D does anything funky with the textures. The texture import settings seem to stay the same.
As I said in my previous post I sent you a link to a project where this happens so that you may be able to figure out what is happening.

Edit:
Debugged it a bit and I have no idea what is happening.
The sprite gets properly created in the UiAtlasMaker.ExtractSprites() method but at the end NGUIEditorTools.ImportTexture() gets called and after that the sprite.tex field is suddenly null even though it was properly set before and I do not see anything that is resetting it int NGUIEditorTools.ImportTexture().
Title: Re: bug with NGUI after moving prefab material and texture
Post by: Bento on September 26, 2013, 09:51:03 AM
Hello,

This is an official post from Bento (the creators of the Uni2D plugin).

We've followed this thread with attention and your problem will be solved soon with the latest Uni2Dv2.0.2 version.

This version is yet under approval at Unity.
If it's all ok this version will be available in less than a week now.
Thanks for your feedback and for using Uni2D, we hope you'll make great games with it!

Best regards,
Bento.