示例#1
0
    void OnInspectorUpdate()
    {
        bool refresh = false;

        switch (retinaProState.state)
        {
        default:
        case retinaProState.rpState.kWaiting:
            break;

        case retinaProState.rpState.kGen:
        {
            deviceIndex    = 0;
            fileIndex      = 0;
            progressPeriod = 0.0f;
            progressString = "Atlas " + genAtlasItem.atlasName;

#if RETINAPRO_DEBUGLOG
            Debug.Log(progressString);
#endif

            if (genAtlasItem.isFont)
            {
                retinaProState.state = retinaProState.rpState.kFont;
            }
            else
            {
                // create atlas that will be used as the reference for the device specific atlas
                UIAtlas atlasRef = retinaProNGTools.createAtlas(genAtlasItem.atlasName, null, out oldSpriteData);
                oldSpriteData = null;
                if (atlasRef == null)
                {
                    Debug.LogWarning("Could not create atlas reference for " + genAtlasItem.atlasName);
                    retinaProState.state = retinaProState.rpState.kDone;
                    break;
                }

                retinaProParent parent = atlasRef.gameObject.GetComponent <retinaProParent>();
                if (parent == null)
                {
                    atlasRef.gameObject.AddComponent <retinaProParent>();
                }

                EditorUtility.SetDirty(atlasRef.gameObject);
                retinaProState.state = retinaProState.rpState.kAtlas;
            }

            break;
        }


        case retinaProState.rpState.kAtlas: {
            retinaProDevice deviceItem = retinaProDataSerialize.sharedInstance.deviceList[deviceIndex];
            progressString       = "Atlas " + genAtlasItem.atlasName + " / " + deviceItem.name + " - Processing images";
            retinaProState.state = retinaProState.rpState.kAtlasProcess;
            break;
        }


        case retinaProState.rpState.kAtlasProcess: {
            retinaProDevice deviceItem = retinaProDataSerialize.sharedInstance.deviceList[deviceIndex];

#if RETINAPRO_DEBUGLOG
            Debug.Log("addNewAtlas; " + genAtlasItem.atlasName + ", for device: " + deviceItem.name + ", pixelSize = " + deviceItem.pixelSize);
#endif
            // gather textures for this atlas / device
            DirectoryInfo dinfo = new DirectoryInfo(retinaProFileLock.baseDataPath + retinaProConfig.atlasTextureFolder + deviceItem.name + "/" + genAtlasItem.atlasName);
            if (dinfo == null || !dinfo.Exists)
            {
                Debug.LogWarning("Folder does not exist; " + genAtlasItem.atlasName + ", for device: " + deviceItem.name + ", pixelSize = " + deviceItem.pixelSize);
            }
            else
            {
                List <FileInfo> fis;
                retinaProConfig.getValidArtFiles(dinfo, out fis);

                if (fis != null && fis.Count > 0)
                {
                    genAtlas = retinaProNGTools.createAtlas(genAtlasItem.atlasName, deviceItem.name, out oldSpriteData);
                    if (genAtlas == null)
                    {
                        Debug.LogWarning("Could not create atlas for " + genAtlasItem.atlasName + ", device = " + deviceItem.name);
                        retinaProState.state = retinaProState.rpState.kDone;
                        break;
                    }

                    NGUISettings.atlas         = genAtlas;
                    NGUISettings.atlasPadding  = genAtlasItem.atlasPadding;
                    NGUISettings.atlasTrimming = false;
                    NGUISettings.allow4096     = true;
                    NGUISettings.fontTexture   = null;
                    NGUISettings.unityPacking  = true;

                    genAtlas.pixelSize = deviceItem.pixelSize;
                    EditorUtility.SetDirty(genAtlas.gameObject);

                    // add all art files into the atlas (one-pass)
                    List <Texture> textures = new List <Texture>();

                    foreach (FileInfo fi in fis)
                    {
                        // check to see if this file has a corresponding .txt file (i.e. it's a font)
                        bool isFont = false;
                        {
                            string fontName = Path.GetFileNameWithoutExtension(fi.Name);
                            if (dinfo != null && dinfo.Exists)
                            {
                                FileInfo [] fontTextFile = dinfo.GetFiles(fontName + ".txt");
                                if (fontTextFile != null && fontTextFile.Length == 1)
                                {
                                    isFont = true;
                                }
                            }
                        }

                        if (isFont)
                        {
                            progressString = "Atlas " + genAtlasItem.atlasName + " / " + deviceItem.name + " - Fonts in a sprite atlas are not supported!";
                            string fontName = Path.GetFileNameWithoutExtension(fi.Name);
                            Debug.LogWarning("Fonts (" + fontName + ") in a sprite atlas is not supported. Use a font atlas instead, see the example scene.");
                        }
                        else
                        {
                            {
                                string textureName = "Assets" + retinaProConfig.atlasTextureFolder + deviceItem.name + "/" + genAtlasItem.atlasName + "/" + fi.Name;

                                // source texture
                                Texture2D tex = AssetDatabase.LoadAssetAtPath(textureName, typeof(Texture2D)) as Texture2D;
                                if (retinaProDataSerialize.sharedInstance.getUtilityRefreshSourceTextures())                                            // refresh importer settings on source texture?
                                // update texture importer settings on source artwork
                                // this ensures that we don't bring in assets into the atlas that are too small (for their given size)
                                {
                                    TextureImporter tImporter = AssetImporter.GetAtPath(textureName) as TextureImporter;
                                    if (tImporter != null)
                                    {
                                        tImporter.textureType         = TextureImporterType.Advanced;
                                        tImporter.normalmap           = false;
                                        tImporter.linearTexture       = true;
                                        tImporter.alphaIsTransparency = true;
                                        tImporter.convertToNormalmap  = false;
                                        tImporter.grayscaleToAlpha    = false;
                                        tImporter.lightmap            = false;
                                        tImporter.npotScale           = TextureImporterNPOTScale.None;
                                        tImporter.filterMode          = FilterMode.Point;
                                        tImporter.maxTextureSize      = 4096;
                                        tImporter.mipmapEnabled       = false;
                                        tImporter.textureFormat       = TextureImporterFormat.AutomaticTruecolor;
                                        AssetDatabase.ImportAsset(textureName, ImportAssetOptions.ForceUpdate);
                                    }
                                }

                                tex.filterMode = genAtlasItem.atlasFilterMode;
                                tex.wrapMode   = TextureWrapMode.Clamp;
                                textures.Add(tex);

#if RETINAPRO_DEBUGLOG
                                Debug.Log("- added tex: " + textureName);
#endif
                            }
                        }
                    }

                    List <UIAtlasMaker.SpriteEntry> sprites = UIAtlasMaker.CreateSprites(textures);
                    UIAtlasMaker.ExtractSprites(genAtlas, sprites);
                    UIAtlasMaker.UpdateAtlas(genAtlas, sprites);
                    AssetDatabase.SaveAssets();

                    // set texture filter mode
                    {
                        string          newTex    = "Assets" + retinaProConfig.atlasResourceFolder + deviceItem.name + "/" + genAtlasItem.atlasName + "~" + deviceItem.name + ".png";
                        TextureImporter tImporter = AssetImporter.GetAtPath(newTex) as TextureImporter;

                        if (tImporter != null)
                        {
                            tImporter.filterMode    = genAtlasItem.atlasFilterMode;
                            tImporter.textureFormat = genAtlasItem.atlasTextureFormat;
                            AssetDatabase.ImportAsset(newTex, ImportAssetOptions.ForceUpdate);
                        }
                    }

                    // restore the sprite data within the atlas
                    // update the new atlas with the old sprite data
                    retinaProNGTools.updateAtlasSpriteData(ref genAtlas, ref oldSpriteData);
#if RETINAPRO_DEBUGLOG
                    retinaProNGTools.debugAtlasSpriteData(ref genAtlas);
#endif

                    genAtlas.MarkAsChanged();
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();


                    // continue with next device
                    deviceIndex++;
                    if (deviceIndex >= retinaProDataSerialize.sharedInstance.deviceList.Count)
                    {
                        retinaProState.state = retinaProState.rpState.kDone;
                        break;
                    }
                    else
                    {
                        retinaProState.state = retinaProState.rpState.kAtlas;
                    }

                    progressPortion = 0.0f;
                    break;
                }
            }
            break;
        }

        case retinaProState.rpState.kFont:
        {
            retinaProDevice deviceItem = retinaProDataSerialize.sharedInstance.deviceList[deviceIndex];

            DirectoryInfo dinfo = new DirectoryInfo(retinaProFileLock.baseDataPath + retinaProConfig.atlasTextureFolder + deviceItem.name + "/" + genAtlasItem.atlasName);
            if (dinfo == null || !dinfo.Exists)
            {
                Debug.LogWarning("Folder does not exist; " + genAtlasItem.atlasName + ", for device: " + deviceItem.name + ", pixelSize = " + deviceItem.pixelSize);
                retinaProState.state = retinaProState.rpState.kDone;
                break;
            }

            FileInfo [] fis    = dinfo.GetFiles("*.png");
            FileInfo [] fisTxt = dinfo.GetFiles("*.txt");

            if (fis == null || fis.Length != 1 || fisTxt == null || fisTxt.Length != 1)
            {
                Debug.LogWarning("Font atlases should contain two files; thefont.png / thefont.txt");
                fileIndex            = fis.Length;
                retinaProState.state = retinaProState.rpState.kDone;
                break;
            }

            FileInfo fi = fis[fileIndex];

            {
                string fontName = Path.GetFileNameWithoutExtension(fi.Name);
                progressString = "Atlas " + genAtlasItem.atlasName + " / " + deviceItem.name + " / " + fontName;

#if RETINAPRO_DEBUGLOG
                Debug.Log("addNewFont; " + fontName + ", for device: " + deviceItem.name + ", pixelSize = " + deviceItem.pixelSize);
#endif

                UIFont font = retinaProNGTools.createFont(genAtlasItem.atlasName, fontName, deviceItem.name);
                //font.pixelSize = deviceItem.pixelSize;
                EditorUtility.SetDirty(font.gameObject);

                // create the reference font version
                UIFont fontRef = retinaProNGTools.createFont(genAtlasItem.atlasName, fontName, null);
                fontRef.replacement = font;
                //fontRef.pixelSize = deviceItem.pixelSize;

                retinaProParent parent = fontRef.gameObject.GetComponent <retinaProParent>();
                if (parent == null)
                {
                    fontRef.gameObject.AddComponent <retinaProParent>();
                }

                EditorUtility.SetDirty(fontRef.gameObject);
            }

            // set texture filter mode
            {
                string          newTex    = "Assets" + retinaProConfig.atlasTextureFolder + deviceItem.name + "/" + genAtlasItem.atlasName + "/" + fis[0].Name;
                TextureImporter tImporter = AssetImporter.GetAtPath(newTex) as TextureImporter;

                if (tImporter != null)
                {
                    tImporter.filterMode    = genAtlasItem.atlasFilterMode;
                    tImporter.textureFormat = genAtlasItem.atlasTextureFormat;
                    AssetDatabase.ImportAsset(newTex, ImportAssetOptions.ForceUpdate);
                }
            }


            fileIndex++;
            if (fileIndex >= fis.Length)
            {
                fileIndex = 0;

                deviceIndex++;
                if (deviceIndex >= retinaProDataSerialize.sharedInstance.deviceList.Count)
                {
                    fileIndex            = fis.Length;
                    retinaProState.state = retinaProState.rpState.kDone;
                }
            }
            progressPortion = (((float)(fileIndex + 1)) / ((float)fis.Length));
            progressPortion = Mathf.Clamp01(progressPortion);
            break;
        }

        case retinaProState.rpState.kDone:
        {
            EditorUtility.ClearProgressBar();
            refresh = true;
            retinaProState.state = retinaProState.rpState.kWaiting;
            Repaint();
            break;
        }
        }

        if (refresh)
        {
#if RETINAPRO_DEBUGLOG
            Debug.Log("refresh called");
#endif
            int             idx = retinaProDataSerialize.sharedInstance.getPreviewDeviceIdx();
            retinaProDevice di  = retinaProDataSerialize.sharedInstance.deviceList[idx];
            if (di.isDeviceValid())
            {
                retinaProNGTools.refreshReferencesForDevice(di, retinaProDataSerialize.sharedInstance.getPreviewScreenIdx(), retinaProDataSerialize.sharedInstance.getPreviewGameViewIdx());
                retinaProNGTools.refresh();
            }
        }

        if (retinaProState.state != retinaProState.rpState.kWaiting)
        {
            Repaint();
        }
    }
    public void fixFont(ref UIFont fontRef)
    {
        // determine if this is the parent reference font or it's a device specific font
        retinaProParent parent = fontRef.gameObject.GetComponent <retinaProParent>();

        if (parent == null)
        {
            return;
        }

        // get name of prefab we want to have this parent font reference
        retinaProRuntimeDevice d = rt.deviceList[activeDeviceIdx];
        string fontName          = fontRef.name + "~" + d.name;

#if RETINAPRO_ATLASCONTROLLER_DEBUGLOG
        Debug.Log("Required Font = " + fontName);
#endif

        // determine whether we should load a font / replace existing font reference
        bool replace = false;
        if (fontRef.replacement == null)
        {
            // this font isn't referencing anything, fix it
            replace = true;
        }
        else
        {
            // this font is referencing something, is that the one we want?
            if (fontRef.replacement.name.CompareTo(fontName) != 0)
            {
                // this font isn't referencing the atlas we want, fix it
                replace = true;
            }
        }

        // load the required font and update the parents font reference
        if (replace)
        {
#if RETINAPRO_ATLASCONTROLLER_DEBUGLOG
            Debug.Log("- loading");
#endif
            string prefabName = retinaProDataRuntime.atlasFolder + d.name + "/" + fontName;

            UIFont font = Resources.Load(prefabName, typeof(UIFont)) as UIFont;
            if (font != null)
            {
                fontRef.replacement = font;
#if RETINAPRO_ATLASCONTROLLER_DEBUGLOG
                Debug.Log("- loaded");
#endif
            }
            else
            {
                Debug.LogWarning("Could not load the font");
            }
        }
        else
        {
#if RETINAPRO_ATLASCONTROLLER_DEBUGLOG
            Debug.Log("- already loaded");
#endif
        }
    }