Ejemplo n.º 1
0
    protected virtual Material MaterialInstance(Material original, tmTextureCollectionPlatform mainCollection, tmTextureCollectionPlatform lightmapCollection)
    {
        string hashKey            = "mat" + original.GetHashCode();
        string materialUniqueName = "";

        if (mainCollection != null)
        {
            hashKey            += mainCollection.collectionGuid;
            materialUniqueName += mainCollection.name;
        }

        if (lightmapCollection != null)
        {
            hashKey            += lightmapCollection.collectionGuid;
            materialUniqueName += lightmapCollection.name;
        }

        Material copy;

        if (tmManager.Instance.GetSharedMaterial(original, mainCollection, lightmapCollection, hashKey, out copy))
        {
            copy.name      = original.name + "_" + materialUniqueName;
            copy.hideFlags = HideFlags.HideAndDontSave;
        }

        return(copy);
    }
Ejemplo n.º 2
0
    public static tmTextureCollectionPlatform CreatePlatformCollectionFromMainCollection(tmTextureCollection collection, tmPlatform platform)
    {
        string mainCollectionPath = AssetDatabase.GetAssetPath(collection);
        string directory          = System.IO.Path.GetDirectoryName(mainCollectionPath);
        string platformDirectory  = directory + "/" + collection.name + platform.postfix;
        string platformPath       = platformDirectory + "/" + collection.name + platform.postfix + ".prefab";

        if (!System.IO.Directory.Exists(platformDirectory))
        {
            System.IO.Directory.CreateDirectory(platformDirectory);
        }

        tmTextureCollectionPlatform collectionPlatform = AssetDatabase.LoadAssetAtPath(platformPath, typeof(tmTextureCollectionPlatform)) as tmTextureCollectionPlatform;

        if (collectionPlatform == null)
        {
            GameObject g = new GameObject();
            g.AddComponent <tmTextureCollectionPlatform>();
            GameObject prefab = PrefabUtility.CreatePrefab(platformPath, g);
            Object.DestroyImmediate(g);

            collectionPlatform                = prefab.GetComponent <tmTextureCollectionPlatform>();
            collectionPlatform.assetGuid      = tmEditorUtility.AssetToGUID(prefab);
            collectionPlatform.collectionGuid = collection.collectionGuid + platform.postfix;
        }

        return(collectionPlatform);
    }
Ejemplo n.º 3
0
    public static void CreatePlatformResourceLink(tmTextureCollectionPlatform collectionPlatform)
    {
        if (!System.IO.Directory.Exists(tmSettings.AssetsFolder))
        {
            System.IO.Directory.CreateDirectory(tmSettings.AssetsFolder);
        }

        string assetPath    = tmSettings.AssetsFolder + tmSettings.ResourceLinkPrefix + collectionPlatform.collectionGuid + ".asset";
        string resourcePath = tmSettings.ResourceFolder + tmSettings.ResourceLinkPrefix + collectionPlatform.collectionGuid;

        tmResourceCollectionLink link = Resources.Load(resourcePath, typeof(tmResourceCollectionLink)) as tmResourceCollectionLink;

        if (link == null)
        {
            link = ScriptableObject.CreateInstance(typeof(tmResourceCollectionLink)) as tmResourceCollectionLink;
            AssetDatabase.CreateAsset(link, assetPath);
        }

        link.collectionInEditor = collectionPlatform;
        UpdateResourceLink(link);


        EditorUtility.SetDirty(link);
        AssetDatabase.SaveAssets();
    }
Ejemplo n.º 4
0
//
//	protected override void OnEnable()
//	{
//		base.OnEnable();
//
//		#if UNITY_EDITOR
//		if(Application.isPlaying)
//		#endif
//		{
//			MainTexCollection += this;
//			LightmapCollection += this;
//		}
//	}
//
//
//	protected override void OnDisable()
//	{
//		#if UNITY_EDITOR
//		if(Application.isPlaying)
//		#endif
//		{
//			MainTexCollection -= this;
//			LightmapCollection -= this;
//		}
//
//		base.OnDisable();
//	}

    #endregion



    #region Private

    protected override Material MaterialInstance(Material original, tmTextureCollectionPlatform mainCollection, tmTextureCollectionPlatform lightmapCollection)
    {
        string hashKey            = "mat" + original.GetHashCode();
        string materialUniqueName = "";

        if (mainCollection != null)
        {
            hashKey            += mainCollection.collectionGuid;
            materialUniqueName += mainCollection.name;
        }

        if (lightmapCollection != null)
        {
            hashKey            += lightmapCollection.collectionGuid;
            materialUniqueName += lightmapCollection.name;
        }

        hashKey += UseRenderQueue ? RenderQueue : Material.renderQueue;

        Vector2 offset = Vector2.zero;
        Vector2 scale  = Vector2.zero;

        if (MainTextureDefenition != null)
        {
            Rect uvRect   = MainTextureDefenition.uv;
            Rect uvOffset = MainTextureDefenition.offset;
            uvRect.center += uvOffset.center;
            uvRect.size   += uvOffset.size;

            offset = new Vector2(
                uvRect.x,
                uvRect.y
                );

            scale = new Vector2(
                uvRect.width,
                uvRect.height
                );
        }

        hashKey += "" + offset.x + offset.y + scale.x + scale.y;

        Material copy;

        if (tmManager.Instance.GetSharedMaterial(original, mainCollection, lightmapCollection, hashKey, out copy))
        {
            copy.name      = original.name + "_" + materialUniqueName;
            copy.hideFlags = HideFlags.HideAndDontSave;

            copy.mainTextureOffset = offset;
            copy.mainTextureScale  = scale;

            if (UseRenderQueue)
            {
                copy.renderQueue = RenderQueue;
            }
        }

        return(copy);
    }
Ejemplo n.º 5
0
    public static void UpdatePlatformCollection(tmTextureCollectionPlatform collectionPlatform, List <Texture2D> originalTextures)
    {
        collectionPlatform.Textures.Clear();
        collectionPlatform.textureDefenitions.RemoveAll(def => !originalTextures.Contains(def.texture));
        foreach (var texture in originalTextures)
        {
            if (texture != null)
            {
                tmTextureDefenition textureRef = collectionPlatform.textureDefenitions.Find(f => f.texture == texture);
                if (textureRef == null)
                {
                    textureRef             = new tmTextureDefenition();
                    textureRef.texture     = texture;
                    textureRef.textureName = tmUtility.PlatformlessPath(texture.name);
                    textureRef.textureGuid = tmEditorUtility.AssetToGUID(texture);
                    textureRef.assetGuid   = tmEditorUtility.AssetToGUID(texture);
                    collectionPlatform.textureDefenitions.Add(textureRef);
                }

                string originalPath = AssetDatabase.GetAssetPath(texture);
                ConfigureSpriteTextureImporter(originalPath);

                bool  rebuild           = false;
                ulong originalTimestamp = tmEditorUtility.Hash(texture);
                rebuild |= (originalTimestamp != textureRef.originalTimestamp);
                if (textureRef.platformTexture != null)
                {
                    ulong platformTimestamp = tmEditorUtility.Hash(textureRef.platformTexture);
                    rebuild |= (platformTimestamp != textureRef.platformTimestamp);
                }

                Texture2D asset = PlatformSpecifiedTexture(textureRef.texture, collectionPlatform.platform, rebuild);
                if (asset != null)
                {
                    string platformTexturePath = AssetDatabase.GetAssetPath(asset);
                    ConfigureSpriteTextureImporter(platformTexturePath);

                    textureRef.platformTexture   = asset;
                    textureRef.textureName       = tmUtility.PlatformlessPath(textureRef.platformTexture.name);
                    textureRef.assetGuid         = tmEditorUtility.AssetToGUID(textureRef.platformTexture);
                    textureRef.platformTimestamp = tmEditorUtility.Hash(textureRef.platformTexture);
                    textureRef.originalTimestamp = originalTimestamp;
                }
            }
        }

        collectionPlatform.textureDefenitions.Sort((a, b) => (string.Compare(a.textureName, b.textureName, System.StringComparison.OrdinalIgnoreCase)));

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

        tmCollectionBuilder.BuildAtlas(collectionPlatform);

        CreatePlatformResourceLink(collectionPlatform);

        collectionPlatform = tmEditorUtility.GUIDToAsset(collectionPlatform.assetGuid, typeof(tmTextureCollectionPlatform)) as tmTextureCollectionPlatform;
        tmIndex.Instance.RegisterPlatformCollection(collectionPlatform);
    }
Ejemplo n.º 6
0
 public void TextureWillUnloadForCollection(tmTextureCollectionPlatform collection)
 {
     foreach (Material mat in materialMap.Values)
     {
         if (mat.mainTexture == collection.Atlas)
         {
             mat.mainTexture = null;
         }
         if (mat.GetLightmapTexture() == collection.Atlas)
         {
             mat.SetLightmapTexture(null);
         }
     }
 }
Ejemplo n.º 7
0
    public static void BuildPlatformCollections(tmTextureCollection collection)
    {
        if (collection != null)
        {
            List <tmTextureCollectionPlatform> modifiedCollections = new List <tmTextureCollectionPlatform>();

            foreach (tmPlatform platform in tmSettings.allPlatfrorms)
            {
                if (collection.forcedScale > 0 && Mathf.Abs(platform.scale - collection.forcedScale) > float.Epsilon)
                {
                    continue;
                }

                tmTextureCollectionPlatformRef collectionPlatformRef = collection.platforms.Find(f => (f != null) && f.platform.Equals(platform));
                if (collectionPlatformRef == null)
                {
                    collectionPlatformRef          = new tmTextureCollectionPlatformRef();
                    collectionPlatformRef.platform = platform;
                    collection.platforms.Add(collectionPlatformRef);
                }

                tmTextureCollectionPlatform collectionPlatform = collectionPlatformRef.collectionPlatform;

                if (collectionPlatform == null)
                {
                    collectionPlatform = tmCollectionBuilder.CreatePlatformCollectionFromMainCollection(collection, platform);
                }

                collectionPlatform.platform           = platform;       // just update platform info
                collectionPlatform.importSettings     = collection.importSettings;
                collectionPlatform.useStreamingAssets = collection.useStreamingAssets;

                collectionPlatformRef.platform           = platform;
                collectionPlatformRef.collectionName     = collectionPlatform.name;
                collectionPlatformRef.collectionGuid     = collectionPlatform.collectionGuid;
                collectionPlatformRef.assetnGuid         = collectionPlatform.assetGuid;
                collectionPlatformRef.collectionPlatform = collectionPlatform;

                tmCollectionBuilder.UpdatePlatformCollection(collectionPlatform, collection.Textures);
                modifiedCollections.Add(collectionPlatform);
            }

            UpdateRendersInScene(modifiedCollections);

            EditorUtility.SetDirty(collection);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
Ejemplo n.º 8
0
    public void TextureLoadedForCollection(tmTextureCollectionPlatform collection)
    {
        List <Material> mats;

        if (mainTextureMap.TryGetValue(collection, out mats))
        {
            mats.ForEach((m) =>
            {
                m.mainTexture = collection.Atlas;
            }
                         );
        }
        else if (lightmapMap.TryGetValue(collection, out mats))
        {
            mats.ForEach((m) => m.SetLightmapTexture(collection.Atlas));
        }
    }
Ejemplo n.º 9
0
    public tmTextureCollectionPlatform GetPlatformCollectionWithFallback(string collectionGuid, tmPlatform platform)
    {
        tmTextureCollectionPlatform plTexColl = GetPlatformCollection(collectionGuid, platform);

        if (plTexColl == null)
        {
            foreach (var pl in tmSettings.allPlatfrorms)
            {
                plTexColl = GetPlatformCollection(collectionGuid, pl);
                if (plTexColl != null)
                {
                    return(plTexColl);
                }
            }
        }

        return(plTexColl);
    }
Ejemplo n.º 10
0
    /// <summary>
    /// Gets the shared material.
    /// </summary>
    /// <returns><c>true</c>, if shared material was created, <c>false</c> if already created.</returns>
    /// <param name="original">Original.</param>
    /// <param name="mainCollection">Main collection.</param>
    /// <param name="lightmapCollection">Lightmap collection.</param>
    /// <param name="hashKey">Hash key.</param>
    /// <param name="copy">Shared material.</param>
    public bool GetSharedMaterial(Material original, tmTextureCollectionPlatform mainCollection, tmTextureCollectionPlatform lightmapCollection, string hashKey, out Material copy)
    {
        bool wasCreated = false;

        if (!materialMap.TryGetValue(hashKey, out copy))
        {
            copy = Instantiate(original);
            materialMap.Add(hashKey, copy);

            if (mainCollection != null)
            {
                copy.mainTexture = mainCollection.Atlas;

                List <Material> mats;
                if (!mainTextureMap.TryGetValue(mainCollection, out mats))
                {
                    mats = new List <Material>();
                    mainTextureMap.Add(mainCollection, mats);
                }

                mats.Add(copy);
            }

            if (lightmapCollection != null)
            {
                copy.SetLightmapTexture(lightmapCollection.Atlas);

                List <Material> mats;
                if (!lightmapMap.TryGetValue(lightmapCollection, out mats))
                {
                    mats = new List <Material>();
                    lightmapMap.Add(lightmapCollection, mats);
                }

                mats.Add(copy);
            }

            wasCreated = true;
        }

        return(wasCreated);
    }
    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");
    }
Ejemplo n.º 12
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();
    }