Author Topic: MissingReferenceException in UIAtlasMaker.AddOrUpdate  (Read 7343 times)

glequeux

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
MissingReferenceException in UIAtlasMaker.AddOrUpdate
« on: April 02, 2013, 10:21:12 AM »
Hello,
I'm working on auto updating UIAtlases.
My steps are pretty simple :
- Find all UIAtlas with FindObjectsOfTypeAll
- Find and load all textures with Directory.GetFiles then NGUIEditorTools.ImportTexture
- Match texture names with Atlas SpriteList and call AddOrUpdate for each texture in an atlas

The problem it that sometimes, during AddOrUpdate, a texture gets destroyed and generates a MissingReferenceException in AddSprite.

I tried to find what was causing the destruction and it seems related to AssetDatabase.SaveAssets() but if I comment this line UpdateTexture I get fewer exeption but the problem is still there.
Sometimes my process goes fine without exception and my Atlases are correctly updated.

Do you know what can cause this destruction ?

Thx,
G.

Here is my code :
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6.  
  7. public class UpdateAtlas {
  8.         public void StartUpdate() {
  9.                 Debug.Log("Update atlases started !!");
  10.                 //      ***     Find all atlases
  11.                 UIAtlas[] allAtlas = (UIAtlas[])Resources.FindObjectsOfTypeAll(typeof (UIAtlas));
  12.                 Debug.Log("UIAtlas " + allAtlas.Length);
  13.  
  14.                 //      ***     Find and load all textures
  15.                 string[] aTextureFiles = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories);
  16.                 List<Texture2D> textureList = new List<Texture2D>();
  17.                 foreach (string currentFile in aTextureFiles) {
  18.                         string assetPath = "Assets" + currentFile.Replace(Application.dataPath, "").Replace('\\', '/');
  19.                         Texture2D texture = NGUIEditorTools.ImportTexture(assetPath, true, false);                     
  20.                         if (texture) {
  21.                                 textureList.Add(texture);
  22.                         }      
  23.                 }
  24.                 Debug.Log("Textures " + textureList.Count);
  25.  
  26.                 //      ***     Find duplicates
  27.                 List<string> duplicateNames = new List<string>();
  28.                 List<string> allNames = new List<string>();
  29.                 foreach (Texture2D currentTexture in textureList) {
  30.                         if (!allNames.Contains(currentTexture.name)) {
  31.                                 allNames.Add(currentTexture.name);
  32.                         }
  33.                         else {
  34.                                 duplicateNames.Add(currentTexture.name);
  35.                         }
  36.                 }
  37.  
  38.                 //      ***     List duplicates
  39.                 foreach (string textureName in duplicateNames) {
  40.                         Debug.Log("Found duplicate "+textureName);
  41.                 }
  42.  
  43.                 //      ***     Update atlases
  44.                 foreach(UIAtlas currentAtlas in allAtlas) {
  45.                         BetterList<string> spriteList = currentAtlas.GetListOfSprites();
  46.                         //      ***     Match textures
  47.                         foreach (Texture2D currentTexture in textureList) {
  48.                                 if (spriteList.Contains(currentTexture.name) && !duplicateNames.Contains(currentTexture.name)) {
  49.                                         UIAtlasMaker.AddOrUpdate(currentAtlas,currentTexture);
  50.                                 }
  51.                         }              
  52.                 }
  53.         }      
  54. }
  55.  
  56.  

glequeux

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: MissingReferenceException in UIAtlasMaker.AddOrUpdate
« Reply #1 on: April 04, 2013, 03:32:26 AM »
And it would be GREAT if AddOrUpdate could take a texture list instead of a single texture!

Tutanhomon

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: MissingReferenceException in UIAtlasMaker.AddOrUpdate
« Reply #2 on: May 23, 2013, 01:58:58 PM »
I have same problem
And I guess it occures when atlas switches it's resolution - whether using "force square" option or when filling atlas with some number of sprites that it gets switched automatically.

Tutanhomon

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: MissingReferenceException in UIAtlasMaker.AddOrUpdate
« Reply #3 on: May 28, 2013, 04:24:43 AM »
several people on our project met this error...
anyone else?

xenonii

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: MissingReferenceException in UIAtlasMaker.AddOrUpdate
« Reply #4 on: June 17, 2013, 04:38:48 AM »
Yes I encountered it too when trying to update a big atlas (bigger than 2048x2048)
"MissingReferenceException: The object of type 'Texture2D' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UIAtlasMaker.AddSprite (System.Collections.Generic.List`1 sprites, .SpriteEntry se) (at Assets/3rd Party/NGUI/Scripts/Editor/UIAtlasMaker.cs:285)
UIAtlasMaker.ReplaceSprites (.UIAtlas atlas, System.Collections.Generic.List`1 sprites) (at Assets/3rd Party/NGUI/Scripts/Editor/UIAtlasMaker.cs:461)
UIAtlasMaker.UpdateAtlas (.UIAtlas atlas, System.Collections.Generic.List`1 sprites) (at Assets/3rd Party/NGUI/Scripts/Editor/UIAtlasMaker.cs:637)
UIAtlasMaker.OnGUI () (at Assets/3rd Party/NGUI/Scripts/Editor/UIAtlasMaker.cs:971)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)"

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile