Exemple #1
0
			public Texture2DArray CreateTextureArrayAsset (string filename, LandTypeList types, bool isBump)   
			{
				string savePath = UnityEditor.EditorUtility.SaveFilePanel("Create New Texture Array", "Assets", filename, "asset");
				if (savePath == null  ||  savePath.Length==0) return null;  //savefilepanel returns "" on cancel
				savePath = savePath.Replace(Application.dataPath, "Assets");

				Texture2DArray texArr = new Texture2DArray(1024, 1024, types.array.Length, TextureFormat.RGBA32, true, linear:isBump);
				AssetDatabase.CreateAsset(texArr, savePath);
				EditorUtility.SetDirty(texArr);
				AssetDatabase.SaveAssets();

				TextureArrayDecorator texArrDec = new TextureArrayDecorator(texArr);
				for (int i=0; i<types.array.Length; i++)
				{
					if (isBump) texArrDec.SetSource(types.array[i].bumpMap, i, isAlpha:false, saveSources:false);
					else
					{
						texArrDec.SetSource(types.array[i].mainTex, i, isAlpha:false, saveSources:false);
						//texArrDec.SetSource(types.array[i].alphaSource, i, isAlpha:true, saveSources:false);
					}
				}
				texArrDec.SaveSources();

				AssetDatabase.Refresh();
				return texArr;
			}
        public void OnEnable()
        {
            Texture2DArray texArr = (Texture2DArray)target;

            texArrDec = new TextureArrayDecorator(texArr);
            Repaint();
        }
        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();
                }
            }
        }