Author Topic: Multiple Fonts 1 Atlas - Texture2D error  (Read 3012 times)

SpookyFishGames

  • Guest
Multiple Fonts 1 Atlas - Texture2D error
« on: July 23, 2013, 12:04:52 PM »
I'm having an issue with trying to create multiple fonts and have them share the same atlas.

I've followed the instruction video on youtube, and I create an atlas and the first font fine, but when I come to use the fontmaker again for the second font, I enter all the values/fnt/textures etc. and when I click on create font I get the following error:

  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.AddOrUpdate (.UIAtlas atlas, UnityEngine.Texture2D tex) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:567)
  8. UIFontMaker.OnGUI () (at Assets/NGUI/Scripts/Editor/UIFontMaker.cs:278)
  9. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Applications/buildAgent/work/84669f285f6a667f/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
  10.  

Having searched the forum, I have found a few other posts with a similar error, but they are all about adding sprites to the atlas by hand, not using the font maker. Is it the same problem? and if so how can I fix it/get the fix?

My specs: Unity Basic V4.1.5f1 - Windows 7 64 bit - NGUI V2.6.3

Ignore the p.s. that was below, I've just realised I didn't do the NGUI upgrade properly.  :-[ I have done the proper update procedure for NGUI and I am now on 2.6.3 but the error is still occuring. I thought I'd try another way (add the font sprites to the atlas by hand, then create fonts from them) but the same error also occurs when I try to add a sprite to the atlas using Atlas Maker.

Thanks in advance for any assistance.

P.S. I've followed the link in my original purchase email to download NGUI as I purchased direct from Tasharen. The email says it will always point to the latest version, yet when I download it it downloads 2.5.1 but according to the release thread the latest version is 2.6.3. Where can I get 2.6.3 from?
« Last Edit: July 23, 2013, 12:12:36 PM by SpookyFishGames »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Multiple Fonts 1 Atlas - Texture2D error
« Reply #1 on: July 23, 2013, 06:18:18 PM »
You need to make sure to save after every atlas modification, and when creating your 2nd font make sure you're creating a new font into the atlas, not replacing an existing one. If you do everything correctly and still get this issue, I'd love to have a reproduceable case to investigate and fix.

SpookyFishGames

  • Guest
Re: Multiple Fonts 1 Atlas - Texture2D error
« Reply #2 on: July 24, 2013, 07:44:44 AM »
Thanks for the quick reply.

I've followed your instructions and in the main project I am working on, the error still occurs.

However if I start a whole project, and then follow your instructions, using the same png/fnt files, it works fine. So I guess the fact it's gone wrong once in my original project means it won't work at all in that one now. Could this be because the old project has an upgraded version of NGUI, whereas the new project has been started from scratch with the lastest version?

Anyway I can sort of work round it now, by exporting the atlas/fonts created in the new project into my old one.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Multiple Fonts 1 Atlas - Texture2D error
« Reply #3 on: July 24, 2013, 02:22:07 PM »
Did you follow upgrade instructions closely, where it asks you to start a new scene before deleting the NGUI folder?

SpookyFishGames

  • Guest
Re: Multiple Fonts 1 Atlas - Texture2D error
« Reply #4 on: July 24, 2013, 02:54:27 PM »
I did, but only after I'd done it wrong once  ::). I guess somethings gone awry with that project now, because I've tried creating a couple of new projects and they have no problems with the atlas, or font, maker, but no matter what I do that project will get this error everytime I try to edit an atlas.

As I said now I know I can create the atlases/fonts in another project, and copy them across, it's not as big a problem. But I think I'll rebuild my main project from scratch, just to be on the safe side.

Thanks for the assistance.

Ernest

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 40
    • View Profile
Atlas - Texture2D error
« Reply #5 on: July 30, 2013, 04:03:59 AM »
I still get this problem... By debugging, it's due to the AssetDatabase.ImportAsset() in MakeTextureReadable(), it reset the "tex" field in the sprite list to null (don't ask me why)! I would be glad if anyone could help me on that =)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Multiple Fonts 1 Atlas - Texture2D error
« Reply #6 on: July 30, 2013, 11:05:12 AM »
Does it happen only the first time you do it, or actually multiple times though?

If it happens repeatedly (which is something I have never seen!), then you can try modifying NGUIEditorTools.ImportTexture function from this:
  1.         static public Texture2D ImportTexture (string path, bool forInput, bool force)
  2.         {
  3.                 if (!string.IsNullOrEmpty(path))
  4.                 {
  5.                         if (forInput) { if (!MakeTextureReadable(path, force)) return null; }
  6.                         else if (!MakeTextureAnAtlas(path, force)) return null;
  7.                         //return AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
  8.  
  9.                         Texture2D tex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
  10.                         AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
  11.                         return tex;
  12.                 }
  13.                 return null;
  14.         }
To this:
  1.         static public Texture2D ImportTexture (string path, bool forInput, bool force)
  2.         {
  3.                 if (!string.IsNullOrEmpty(path))
  4.                 {
  5.                         if (forInput) { if (!MakeTextureReadable(path, force)) return null; }
  6.                         else if (!MakeTextureAnAtlas(path, force)) return null;
  7.                         AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
  8.                         return AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
  9.                 }
  10.                 return null;
  11.         }

Ernest

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 40
    • View Profile
Re: Multiple Fonts 1 Atlas - Texture2D error
« Reply #7 on: July 31, 2013, 03:12:26 AM »
Thanks  :) I still get the error and it happen indeed everytime... For now the only solution i found is to recreate a new texture everytime you want to build (or rebuild) your atlas (tex = new Texture2D(1, 1, TextureFormat.ARGB32, false)). But I lose in performance!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Multiple Fonts 1 Atlas - Texture2D error
« Reply #8 on: July 31, 2013, 01:03:43 PM »
The two times I've heard of this issue previously both were resolved by starting a new project and were likely caused by something going wrong in the Unity project after not following the 3 step update process when updating NGUI.