public static void LinkTexture(string texGuid, string arrGuid, bool isAlpha = false)
        {
                        #if UNITY_EDITOR
            string userDataTag = "TexArr_textureArray_as" + (isAlpha? "Source" : "Alpha");
            UnityEditor.AssetImporter importer = AssetsExtensions.GetImporter(texGuid);

            if (importer == null)
            {
                Debug.Log("Could not get importer for " + UnityEditor.AssetDatabase.GUIDToAssetPath(texGuid) + " to assign texture");
                return;
            }

            string[] texData = AssetsExtensions.GetUserData(importer, userDataTag);
            if (!texData.Contains(arrGuid))             //not already signed as used
            {
                ArrayTools.Add(ref texData, arrGuid);
                AssetsExtensions.SetUserData(texGuid, userDataTag, texData, reload: false);
            }

            //writing hash for the newly assigned textures
            string    path    = UnityEditor.AssetDatabase.GUIDToAssetPath(texGuid);
            Texture2D tex     = UnityEditor.AssetDatabase.LoadAssetAtPath <Texture2D>(path);
            string    texHash = tex.GetHash().ToString();                                            //tex.imageContentsHash.ToString();
            AssetsExtensions.SetUserData(importer, "Hash", new string[] { texHash }, reload: false); //will write only if hash changed
                        #endif
        }
        public static void UnlinkTexture(string texGuid, string arrGuid, bool isAlpha = false)
        {
                        #if UNITY_EDITOR
            string userDataTag = "TexArr_textureArray_as" + (isAlpha? "Source" : "Alpha");
            UnityEditor.AssetImporter importer = AssetsExtensions.GetImporter(texGuid);

            if (importer == null)
            {
                Debug.Log("Could not get importer for " + UnityEditor.AssetDatabase.GUIDToAssetPath(texGuid) + " to unassign texture");
                return;
            }

            string[] prevTexData = AssetsExtensions.GetUserData(importer, userDataTag);
            if (prevTexData == null || prevTexData.Length == 0)
            {
                //Debug.Log("Previous texture has no data assigned");
                return;
            }
            ArrayTools.RemoveAll(ref prevTexData, arrGuid);
            AssetsExtensions.SetUserData(importer, userDataTag, prevTexData, reload: false);
                        #endif
        }
예제 #3
0
        static void ReassignTexture(Texture2D tex, string assetPath)
        {
            HashSet <Texture2DArray> usedTexArrs = new HashSet <Texture2DArray>();

            //finding if it was a real change or just user data
            string[] prevHashArr = tex.GetUserData("Hash");
            if (prevHashArr.Length != 0)
            {
                string prevHash = prevHashArr[0];
                string newHash  = tex.GetHash().ToString();                //tex.imageContentsHash.ToString();

                if (prevHash == newHash)
                {
                    //Debug.Log("Skipping " + System.IO.Path.GetFileName(assetPath));
                    return;
                }
                else
                {
                    AssetsExtensions.SetUserData(tex, "Hash", new string[] { newHash }, reload: false);                 //will be reloaded on App
                }
            }

            //sources
            string[] texarrGuids = tex.GetUserData("TexArr_textureArray_asSource");
            for (int t = 0; t < texarrGuids.Length; t++)
            {
                Texture2DArray texArr = texarrGuids[t].GUIDtoObj <Texture2DArray>();
                if (!usedTexArrs.Contains(texArr))
                {
                    usedTexArrs.Add(texArr);
                }
                TextureArrayDecorator texArrDec = new TextureArrayDecorator(texArr);

                Debug.Log("Refreshing " + texArr + " since texture " + System.IO.Path.GetFileName(assetPath) + " is used as it's source", texArr);

                texArrDec.FindAndSetSource(tex, assetPath, isAlpha: false);
            }

            //alphas
            texarrGuids = tex.GetUserData("TexArr_textureArray_asAlpha");
            for (int t = 0; t < texarrGuids.Length; t++)
            {
                Texture2DArray texArr = texarrGuids[t].GUIDtoObj <Texture2DArray>();
                if (!usedTexArrs.Contains(texArr))
                {
                    usedTexArrs.Add(texArr);
                }
                TextureArrayDecorator texArrDec = new TextureArrayDecorator(texArr);

                Debug.Log("Refreshing " + texArr + " since texture " + System.IO.Path.GetFileName(assetPath) + " is used as it's source", texArr);

                texArrDec.FindAndSetSource(tex, assetPath, isAlpha: true);
            }

            //refresh preview
            TextureArrayInspector[] editors = Resources.FindObjectsOfTypeAll <TextureArrayInspector>();
            for (int i = 0; i < editors.Length; i++)
            {
                Texture2DArray editorTexArr = editors[i].texArrDec.texArr;
                if (usedTexArrs.Contains(editorTexArr))
                {
                    editors[i].OnEnable();
                }
            }
        }