public override void OnInspectorGUI()
    {
//		EyeDropper.GetPickedColor();
//		System.Type eyeDropperType = GetType("EyeDropper");
//		MethodInfo mi = eyeDropperType.GetMethod("GetPickedColor", BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
//		FieldInfo fi = eyeDropperType.GetField("s_PickCoordinates", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly);
//		Vector2 a = GUIUtility.GUIToScreenPoint(Event.current.mousePosition);
//		fi.SetValue(null, a);
//		Debug.Log(fi);
//		Debug.Log(mi);
//		Color c = (Color)mi.Invoke(null, null);
//
//
//		EditorGUILayout.ColorField(Color.white);
//		Debug.Log(
//			"r:" + ((int)(c.r * 256)) +
//			" g:" + ((int)(c.g * 256)) +
//			" b:" + ((int)(c.b * 256)) +
//			" a:" + ((int)(c.a * 256))
//		);
        baseEditor.OnInspectorGUI();

        GUILayout.Space(20);

        TextureImporter textureImporter = (TextureImporter)target;

        tmTextureCollectionIndex currentCollectionIndex = tmIndex.Instance.CollectionIndexForTexturePath(textureImporter.assetPath);

        string[] names = new string[tmIndex.Instance.TextureCollections.Count];

        int index = -1;

        for (int i = 0; i < tmIndex.Instance.TextureCollections.Count; i++)
        {
            tmTextureCollectionIndex collectionIndex = tmIndex.Instance.TextureCollections[i];
            names[i] = collectionIndex.name;

            if (collectionIndex.Equals(currentCollectionIndex))
            {
                index = i;
            }
        }

        if (newIndex < -1)
        {
            newIndex = index;
        }
        newIndex = EditorGUILayout.Popup("Texture Atlas", newIndex, names);

        if (newIndex > 0)
        {
            tmTextureCollectionIndex collectionIndex = tmIndex.Instance.TextureCollections[newIndex];
            string collectionGUID                  = collectionIndex.textureCollectionGUID;
            string collectionGuidPath              = tmUtility.PathForPlatform(collectionGUID, tmSettings.Instance.CurrentPlatform);
            tmResourceCollectionLink    link       = tmUtility.ResourceLinkByGUID(collectionGuidPath);
            tmTextureCollectionPlatform collection = link.collectionInEditor;
            collection.LoadTexture();
            Texture2D atlas = collection.Atlas;

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel(" ");
            Rect rect = EditorGUILayout.GetControlRect(GUILayout.Width(150f * atlas.width / atlas.height), GUILayout.Height(150f));
            EditorGUI.DrawRect(rect, Color.black);
            EditorGUI.DrawTextureTransparent(rect, atlas, ScaleMode.ScaleToFit);
            EditorGUILayout.EndHorizontal();

            Object asset = tmEditorUtility.GUIDToAsset(collection.AtlasAssetGUID, typeof(Object));
            if (rect.Contains(Event.current.mousePosition))
            {
                if (Event.current.clickCount == 1)
                {
                    if (asset)
                    {
                        EditorGUIUtility.PingObject(asset);
                    }
                    Event.current.Use();
                }
            }
        }

        GUILayout.BeginHorizontal();
        {
            if (currentCollectionIndex != null)
            {
                EditorGUILayout.PrefixLabel(" ");

                if (GUILayout.Button("Remove from atlas"))
                {
                    newIndex = -1;
                }
            }

            GUILayout.FlexibleSpace();

            bool enabled = GUI.enabled;
            GUI.enabled = index != newIndex;

            if (GUILayout.Button("Revert"))
            {
                newIndex = -2;
            }
            if (GUILayout.Button("Apply"))
            {
                if (currentCollectionIndex != null)
                {
                    tmTextureCollection collection = tmEditorUtility.GUIDToAsset(currentCollectionIndex.assetGUID, typeof(tmTextureCollection)) as tmTextureCollection;
                    Debug.Log(collection);
                    if (collection)
                    {
                        tmTextureDefenition def = collection.GetTextureDefenitionByID(AssetDatabase.AssetPathToGUID(textureImporter.assetPath));
                        if (def != null)
                        {
                            Debug.Log(def.texture);

                            collection.textureDefenitions.Remove(def);
                            collection.Textures.Remove(def.texture);
                            EditorUtility.SetDirty(collection);

                            tmCollectionBuilder.BuildCollection(collection);
                        }
                    }
                }

                if (newIndex > 0)
                {
                    tmTextureCollectionIndex newCollectionIndex = tmIndex.Instance.TextureCollections[newIndex];
                    tmTextureCollection      collection         = tmEditorUtility.GUIDToAsset(newCollectionIndex.assetGUID, typeof(tmTextureCollection)) as tmTextureCollection;
                    collection.Textures.Add(AssetDatabase.LoadAssetAtPath(textureImporter.assetPath, typeof(Texture2D)) as Texture2D);
                    EditorUtility.SetDirty(collection);

                    tmCollectionBuilder.BuildCollection(collection);
                }

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }

            GUI.enabled = enabled;
        }
        GUILayout.EndHorizontal();

        GUILayout.FlexibleSpace();

        GUILayout.Label("Description");
        GUILayout.BeginHorizontal("Box");
        {
            textureImporter.userData = GUILayout.TextField(textureImporter.userData, GUI.skin.label);
        }
        GUILayout.EndHorizontal();

//		EditorGUILayout.LabelField("test label");
    }
예제 #2
0
    public static void BuildAtlas(tmTextureCollectionPlatform collectionPlatform)
    {
        string collectionPath      = AssetDatabase.GetAssetPath(collectionPlatform);
        string collectionDirectory = System.IO.Path.GetDirectoryName(collectionPath);
        string collectionFileName  = System.IO.Path.GetFileNameWithoutExtension(collectionPath);
        string atlasName           = collectionFileName + "_atlas.png";

        string collectionAtlasPath;

        if (collectionPlatform.useStreamingAssets)
        {
            collectionAtlasPath = "Assets/StreamingAssets/" + tmManager.stramingAssetsDirectory + "/" + atlasName;
        }
        else
        {
            collectionAtlasPath = collectionDirectory + "/" + atlasName;
        }

        Texture2D atlas = new Texture2D(4096, 4096, collectionPlatform.importSettings.format, collectionPlatform.importSettings.generateMipMaps);

        List <Texture2D> textures = new List <Texture2D>();

        foreach (tmTextureDefenition td in collectionPlatform.textureDefenitions)
        {
            td.uv = new Rect();
            textures.Add(td.platformTexture);
        }

        Rect[] spritesRect = atlas.PackTextures(textures.ToArray(), collectionPlatform.importSettings.border, 4096, false);
        for (int i = 0; i < spritesRect.Length; i++)
        {
            collectionPlatform.textureDefenitions[i].uv = spritesRect[i];
            Rect offset = new Rect();
            offset.size   = collectionPlatform.textureDefenitions[i].offset.size * 1f / atlas.width;
            offset.center = collectionPlatform.textureDefenitions[i].offset.center * 1f / atlas.width;
            collectionPlatform.textureDefenitions[i].offset = offset;
        }

        collectionPlatform.AtlasAssetGUID = AssetDatabase.AssetPathToGUID(collectionAtlasPath);

        var bytes = atlas.EncodeToPNG();

        System.IO.File.WriteAllBytes(collectionAtlasPath, bytes);


        if (!collectionPlatform.useStreamingAssets)
        {
            int startIndex = collectionAtlasPath.IndexOf("Resources/", System.StringComparison.CurrentCulture) + 10;
            int lastIndex  = collectionAtlasPath.LastIndexOf('.');
            collectionPlatform.AssetPath = collectionAtlasPath.Substring(startIndex, lastIndex - startIndex);

            AssetDatabase.Refresh();

            TextureImporter tImporter = AssetImporter.GetAtPath(collectionAtlasPath) as TextureImporter;
            tImporter.textureType    = TextureImporterType.Default;
            tImporter.isReadable     = false;
            tImporter.maxTextureSize = 4096;
            tImporter.textureFormat  = collectionPlatform.importSettings.importFormat;
            tImporter.mipmapEnabled  = collectionPlatform.importSettings.generateMipMaps;
            AssetDatabase.ImportAsset(collectionAtlasPath, ImportAssetOptions.ForceUpdate);
        }
        else
        {
            collectionPlatform.AssetPath = atlasName;
        }

        if (collectionPlatform.Atlas != null)
        {
            TextureCache.UnloadTexture(collectionPlatform.Atlas);
            collectionPlatform.Atlas = null;
            collectionPlatform.LoadTexture();
        }

        Object.DestroyImmediate(atlas);

        EditorUtility.SetDirty(collectionPlatform);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }