Beispiel #1
0
    static void CreateIndex()
    {
        tk2dIndex newIndex = ScriptableObject.CreateInstance <tk2dIndex>();

        newIndex.version   = tk2dIndex.CURRENT_VERSION;
        newIndex.hideFlags = HideFlags.DontSave;         // get this to not be destroyed in Unity 4.1

        List <string> rebuildSpriteCollectionPaths = new List <string>();

        // check all prefabs to see if we can find any objects we are interested in
        List <string>  allPrefabPaths = new List <string>();
        Stack <string> paths          = new Stack <string>();

        paths.Push(Application.dataPath);
        while (paths.Count != 0)
        {
            string   path  = paths.Pop();
            string[] files = Directory.GetFiles(path, "*.prefab");
            foreach (var file in files)
            {
                allPrefabPaths.Add(file.Substring(Application.dataPath.Length - 6));
            }

            foreach (string subdirs in Directory.GetDirectories(path))
            {
                paths.Push(subdirs);
            }
        }

        // Check all prefabs
        int currPrefabCount = 1;

        foreach (string prefabPath in allPrefabPaths)
        {
            EditorUtility.DisplayProgressBar("Rebuilding Index", "Scanning project folder...", (float)currPrefabCount / (float)(allPrefabPaths.Count));

            GameObject iterGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
            if (!iterGo)
            {
                continue;
            }

            tk2dSpriteCollection     spriteCollection     = iterGo.GetComponent <tk2dSpriteCollection>();
            tk2dSpriteCollectionData spriteCollectionData = iterGo.GetComponent <tk2dSpriteCollectionData>();
            tk2dFont            font = iterGo.GetComponent <tk2dFont>();
            tk2dSpriteAnimation anim = iterGo.GetComponent <tk2dSpriteAnimation>();

            if (spriteCollection)
            {
                tk2dSpriteCollectionData thisSpriteCollectionData = spriteCollection.spriteCollection;
                if (thisSpriteCollectionData)
                {
                    if (thisSpriteCollectionData.version < 1)
                    {
                        rebuildSpriteCollectionPaths.Add(AssetDatabase.GetAssetPath(spriteCollection));
                    }
                    newIndex.AddSpriteCollectionData(thisSpriteCollectionData);
                }
            }
            else if (spriteCollectionData)
            {
                string guid    = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(spriteCollectionData));
                bool   present = false;
                foreach (var v in newIndex.GetSpriteCollectionIndex())
                {
                    if (v.spriteCollectionDataGUID == guid)
                    {
                        present = true;
                        break;
                    }
                }
                if (!present && guid != "")
                {
                    newIndex.AddSpriteCollectionData(spriteCollectionData);
                }
            }
            else if (font)
            {
                newIndex.AddOrUpdateFont(font);                 // unfortunate but necessary
            }
            else if (anim)
            {
                newIndex.AddSpriteAnimation(anim);
            }
            else
            {
                iterGo = null;
                System.GC.Collect();
            }

            tk2dEditorUtility.UnloadUnusedAssets();
            ++currPrefabCount;
        }
        EditorUtility.ClearProgressBar();

        // Create index
        newIndex.hideFlags = 0;         // to save it
        AssetDatabase.CreateAsset(newIndex, indexPath);
        AssetDatabase.SaveAssets();

        // unload all unused assets
        tk2dEditorUtility.UnloadUnusedAssets();

        // Rebuild invalid sprite collections
        if (rebuildSpriteCollectionPaths.Count > 0)
        {
            EditorUtility.DisplayDialog("Upgrade required",
                                        "Please wait while your sprite collection is upgraded.",
                                        "Ok");

            int count = 1;
            foreach (var scPath in rebuildSpriteCollectionPaths)
            {
                tk2dSpriteCollection sc = AssetDatabase.LoadAssetAtPath(scPath, typeof(tk2dSpriteCollection)) as tk2dSpriteCollection;
                EditorUtility.DisplayProgressBar("Rebuilding Sprite Collections", "Rebuilding Sprite Collection: " + sc.name, (float)count / (float)(rebuildSpriteCollectionPaths.Count));

                tk2dSpriteCollectionBuilder.Rebuild(sc);
                sc = null;

                tk2dEditorUtility.UnloadUnusedAssets();

                ++count;
            }

            EditorUtility.ClearProgressBar();
        }

        index = newIndex;
        tk2dSpriteGuiUtility.ResetCache();
    }
    static void CreateIndex()
    {
        GameObject go       = new GameObject();
        tk2dIndex  newIndex = go.AddComponent <tk2dIndex>();

        go.active = false;

        List <string> rebuildSpriteCollectionPaths = new List <string>();

        // check all prefabs to see if we can find any objects we are interested in
        List <string>  allPrefabPaths = new List <string>();
        Stack <string> paths          = new Stack <string>();

        paths.Push(Application.dataPath);
        while (paths.Count != 0)
        {
            string   path  = paths.Pop();
            string[] files = Directory.GetFiles(path, "*.prefab");
            foreach (var file in files)
            {
                allPrefabPaths.Add(file.Substring(Application.dataPath.Length - 6));
            }

            foreach (string subdirs in Directory.GetDirectories(path))
            {
                paths.Push(subdirs);
            }
        }

        // Check all prefabs
        int currPrefabCount = 1;

        foreach (string prefabPath in allPrefabPaths)
        {
            EditorUtility.DisplayProgressBar("Rebuilding Index", "Scanning project folder...", (float)currPrefabCount / (float)(allPrefabPaths.Count));

            GameObject iterGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
            if (!iterGo)
            {
                continue;
            }

            tk2dSpriteCollection spriteCollection = iterGo.GetComponent <tk2dSpriteCollection>();
            tk2dFont             font             = iterGo.GetComponent <tk2dFont>();
            tk2dSpriteAnimation  anims            = iterGo.GetComponent <tk2dSpriteAnimation>();

            if (spriteCollection)
            {
                tk2dSpriteCollectionData spriteCollectionData = spriteCollection.spriteCollection;
                if (spriteCollectionData)
                {
                    if (spriteCollectionData.version < 1)
                    {
                        rebuildSpriteCollectionPaths.Add(AssetDatabase.GetAssetPath(spriteCollection));
                    }
                    newIndex.AddSpriteCollectionData(spriteCollectionData);
                }
            }
            else if (font)
            {
                newIndex.AddFont(font);
            }
            else if (anims)
            {
                newIndex.AddSpriteAnimation(anims);
            }
            else
            {
                iterGo = null;
                System.GC.Collect();
            }

            tk2dEditorUtility.UnloadUnusedAssets();
            ++currPrefabCount;
        }
        EditorUtility.ClearProgressBar();

        // Create index
        Object p = EditorUtility.CreateEmptyPrefab(indexPath);

        EditorUtility.ReplacePrefab(go, p, ReplacePrefabOptions.ConnectToPrefab);
        GameObject.DestroyImmediate(go);

        // unload all unused assets
        tk2dEditorUtility.UnloadUnusedAssets();

        // Rebuild invalid sprite collections
        if (rebuildSpriteCollectionPaths.Count > 0)
        {
            EditorUtility.DisplayDialog("Upgrade required",
                                        "Please wait while your sprite collection is upgraded.",
                                        "Ok");

            int count = 1;
            foreach (var scPath in rebuildSpriteCollectionPaths)
            {
                tk2dSpriteCollection sc = AssetDatabase.LoadAssetAtPath(scPath, typeof(tk2dSpriteCollection)) as tk2dSpriteCollection;
                EditorUtility.DisplayProgressBar("Rebuilding Sprite Collections", "Rebuilding Sprite Collection: " + sc.name, (float)count / (float)(rebuildSpriteCollectionPaths.Count));

                tk2dSpriteCollectionBuilder.Rebuild(sc);
                sc = null;

                tk2dEditorUtility.UnloadUnusedAssets();

                ++count;
            }

            EditorUtility.ClearProgressBar();
        }
    }