Exemplo n.º 1
0
 static void TryResolveEditorData(GiraffeAtlas atlas)
 {
     // atlas._sourceResolved:
     // 0 - no attempt
     // 1 - attempt, and found
     // 2 - attemped, and not found
     switch (atlas._importDataResolved)
     {
     case 0:
     case 2:
     {
         foreach (var n in AssetDatabase.GetAllAssetPaths())
         {
             if (n.StartsWith("Assets/") == false)
             {
                 continue;
             }
             if (n.EndsWith(".asset") == false)
             {
                 continue;
             }
             UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(n, typeof(GiraffeImportData));
             if (obj != null && obj is GiraffeImportData)
             {
                 GiraffeImportData source = obj as GiraffeImportData;
                 if (source.atlasIdA == atlas.atlasIdA && source.atlasIdB == atlas.atlasIdB)
                 {
                     atlas._importData         = source;
                     atlas._importDataResolved = 1;
                     source._atlas             = atlas;
                     source._atlasResolved     = 1;
                     return;
                 }
             }
         }
         atlas._importDataResolved = 2;
     }
     break;
     }
 }
Exemplo n.º 2
0
    void InspectImport()
    {
        if (mAtlas._importDataResolved == 2)
        {
            GUILayout.BeginVertical();
            GUILayout.Label("Importer File", EditorStyles.boldLabel);
            GUILayout.EndVertical();

            GUILayout.BeginVertical(EditorStyles.textArea);
            GUILayout.Label("An Giraffe Import Data file must be created before using, this is used by the editor to keep track of external resources to build the texture atlas.", EditorStyles.wordWrappedLabel);
            if (GUILayout.Button("Create"))
            {
                String path = EditorUtility.SaveFilePanelInProject("Atlas Import Data", String.Format("{0} ImportData", System.IO.Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(mAtlas))), "asset",
                                                                   "Create an ImportData");

                if (String.IsNullOrEmpty(path) == false)
                {
                    GiraffeImportData importData = ScriptableObject.CreateInstance <GiraffeImportData>();
                    importData.atlasIdA = mAtlas.atlasIdA;
                    importData.atlasIdB = mAtlas.atlasIdB;

                    mAtlas._importData         = importData;
                    mAtlas._importDataResolved = 1;
                    importData._atlas          = mAtlas;
                    importData._atlasResolved  = 1;

                    AssetDatabase.CreateAsset(importData, path);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();

                    EditorUtility.SetDirty(importData);
                }
            }
            GUILayout.Label("Note: To stop the Import Data and any textures/files it references as part of the build, place them outside of the Resources folder.", EditorStyles.wordWrappedLabel);

            GUILayout.EndVertical();
            return;
        }
        else
        {
            GUILayout.BeginVertical();
            GUILayout.Label("Import tool", EditorStyles.boldLabel);
            GUILayout.EndVertical();
        }

        bool changed = false;

        GUILayout.BeginHorizontal();

        Color col = GUI.color;

        if (mAtlas._importData.atlasOutOfDate)
        {
            GUI.color = Color.yellow;
        }

        if (GUILayout.Button("Build", EditorStyles.miniButton, GUILayout.MinWidth(120)))
        {
            BuildAtlas();
        }

        GUI.color = col;

        GUILayout.FlexibleSpace();
        GUILayout.BeginHorizontal(GUILayout.MinWidth(120));
        GUI.changed = false;

        GUILayout.Label("Add", GUILayout.Width(25));
        var v = (FriendlyPartType)EditorGUILayout.EnumPopup(FriendlyPartType.Texture);

        if (GUI.changed)
        {
            switch (v)
            {
            case FriendlyPartType.Texture:
            {
                GiraffeAtlasImportDataPart part = new GiraffeAtlasImportDataPart();
                part.type = GiraffeAtlasImportDataType.Texture2D;
                mAtlas._importData.parts.Add(part);
                changed = true;
                break;
            }

            case FriendlyPartType.SquareTileSheet:
            {
                GiraffeAtlasImportDataPart part = new GiraffeAtlasImportDataPart();
                part.type = GiraffeAtlasImportDataType.TileSheetSquare;
                mAtlas._importData.parts.Add(part);
                changed = true;
                break;
            }

            case FriendlyPartType.RectangularTileSheet:
            {
                GiraffeAtlasImportDataPart part = new GiraffeAtlasImportDataPart();
                part.type = GiraffeAtlasImportDataType.TilesheetRectangular;
                mAtlas._importData.parts.Add(part);
                changed = true;
                break;
            }
            }
        }

        GUILayout.EndHorizontal();
        GUILayout.EndHorizontal();


        GUILayout.BeginVertical();

        GUI.changed = false;

        mAtlas._importData.generateWhiteTexture = EditorGUILayout.Toggle("Make 'Giraffe/White' sprite",
                                                                         mAtlas._importData.generateWhiteTexture);

        if (GUI.changed)
        {
            changed = true;
        }


        GUI.changed = false;
        mAtlas._importData.padding = EditorGUILayout.IntSlider("Padding", mAtlas._importData.padding, 0, 8);

        if (GUI.changed)
        {
            changed = true;
        }

        GUI.changed = false;
        mAtlas._importData.border = EditorGUILayout.IntSlider("Border", mAtlas._importData.border, 0, 32);

        if (GUI.changed)
        {
            changed = true;
        }


        GUILayout.EndVertical();


        mImportScroll = GUILayout.BeginScrollView(mImportScroll);

        foreach (var part in mAtlas._importData.parts)
        {
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("x", GUILayout.Width(25)))
            {
                mAtlas._importData.parts.Remove(part);
                changed = true;
                break;
            }

            switch (part.type)
            {
            case GiraffeAtlasImportDataType.Texture2D:
            {
                GUI.changed       = false;
                part.textureAsset = EditorGUILayout.ObjectField(part.textureAsset, typeof(Texture2D), false) as Texture2D;

                if (GUI.changed)
                {
                    changed = true;
                }
            }
            break;

            case GiraffeAtlasImportDataType.TileSheetSquare:
            {
                GUILayout.BeginVertical();
                GUI.changed = false;
                bool refreshCount = false;
                part.textureAsset = EditorGUILayout.ObjectField(part.textureAsset, typeof(Texture2D), false) as Texture2D;

                if (GUI.changed)
                {
                    refreshCount = true;
                    changed      = true;
                }

                GUI.changed = false;
                part.height = part.width = EditorGUILayout.IntSlider("Tile size", part.width, 2, 512);

                if (GUI.changed)
                {
                    refreshCount = true;
                    changed      = true;
                }

                if (refreshCount)
                {
                    refreshCount = false;
                    if (part.textureAsset == null)
                    {
                        part.count = 0;
                    }
                    else
                    {
                        part.count = (part.textureAsset.width * part.textureAsset.height) / (part.width * part.height);
                    }
                }

                EditorGUILayout.LabelField("Count", part.count.ToString());
                GUILayout.EndVertical();
            }
            break;

            case GiraffeAtlasImportDataType.TilesheetRectangular:
            {
                GUILayout.BeginVertical();
                GUI.changed = false;
                bool refreshCount = false;
                part.textureAsset = EditorGUILayout.ObjectField(part.textureAsset, typeof(Texture2D), false) as Texture2D;

                if (GUI.changed)
                {
                    refreshCount = true;
                    changed      = true;
                }

                GUI.changed = false;
                part.width  = EditorGUILayout.IntSlider("Tile width", part.width, 2, 512);

                if (GUI.changed)
                {
                    refreshCount = true;
                    changed      = true;
                }

                GUI.changed = false;
                part.height = EditorGUILayout.IntSlider("Tile height", part.height, 2, 512);

                if (GUI.changed)
                {
                    refreshCount = true;
                    changed      = true;
                }

                if (refreshCount)
                {
                    refreshCount = false;
                    if (part.textureAsset == null)
                    {
                        part.count = 0;
                    }
                    else
                    {
                        part.count = (part.textureAsset.width * part.textureAsset.height) / (part.width * part.height);
                    }
                }

                EditorGUILayout.LabelField("Count", part.count.ToString());
                GUILayout.EndVertical();
            }
            break;
            }

            GUILayout.EndHorizontal();
        }

        GUILayout.EndScrollView();

        if (changed)
        {
            mAtlas._importData.atlasOutOfDate = true;
            EditorUtility.SetDirty(mAtlas._importData);
            EditorUtility.SetDirty(mAtlas);
        }
    }