示例#1
0
        public static TextureArrayConfig FindConfig(Texture2DArray diffuse)
        {
            var path = UnityEditor.AssetDatabase.GetAssetPath(diffuse);

            path = path.Replace("_diff_tarray", "");
            TextureArrayConfig cfg = UnityEditor.AssetDatabase.LoadAssetAtPath <TextureArrayConfig> (path);

            return(cfg);
        }
        static int GetNewHash(TextureArrayConfig cfg)
        {
            unchecked
            {
                var settings = TextureArrayConfigEditor.GetSettingsGroup(cfg, UnityEditor.EditorUserBuildSettings.activeBuildTarget);
                int h        = 17;

                h = h * TextureArrayConfigEditor.GetTextureFormat(cfg, settings.diffuseSettings.compression).GetHashCode() * 7;
                h = h * TextureArrayConfigEditor.GetTextureFormat(cfg, settings.normalSettings.compression).GetHashCode() * 13;
                h = h * TextureArrayConfigEditor.GetTextureFormat(cfg, settings.emissiveSettings.compression).GetHashCode() * 17;
                h = h * TextureArrayConfigEditor.GetTextureFormat(cfg, settings.antiTileSettings.compression).GetHashCode() * 31;
                h = h * TextureArrayConfigEditor.GetTextureFormat(cfg, settings.smoothSettings.compression).GetHashCode() * 37;
                h = h * Application.unityVersion.GetHashCode() * 43;
                return(h);
            }
        }
        private static void CheckConfigForUpdates(TextureArrayConfig cfg)
        {
            int hash = GetNewHash(cfg);

            if (hash != cfg.hash)
            {
                cfg.hash = hash;
                EditorUtility.SetDirty(cfg);
                try
                {
                    sIsPostProcessing = true;
                    TextureArrayConfigEditor.CompileConfig(cfg);
                }
                finally
                {
                    sIsPostProcessing = false;
                    AssetDatabase.Refresh();
                    AssetDatabase.SaveAssets();
                    MicroSplatTerrain.SyncAll();
                }
            }
        }
        public static int DrawTextureSelector(int textureIndex, Texture2DArray ta, bool compact = false)
        {
            if (ta == null)
            {
                return(textureIndex);
            }
            int count = ta.depth;

            if (count > 32)
            {
                count = 32;
            }

            if (cachedArray != ta)
            {
                cachedArray = ta;
                var path = AssetDatabase.GetAssetPath(ta);
                path         = path.Replace("_diff_tarray", "");
                cachedConfig = AssetDatabase.LoadAssetAtPath <TextureArrayConfig> (path);
            }

            Texture2D disp = Texture2D.blackTexture;

            if (ta != null)
            {
                int       hash   = ta.GetHashCode() * (textureIndex + 7);
                Texture2D hashed = FindInPreviewCache(hash);
                if (hashed == null)
                {
                    hashed = new Texture2D(ta.width, ta.height, ta.format, false);
                    Graphics.CopyTexture(ta, textureIndex, 0, hashed, 0, 0);
                    hashed.Apply(false, false);
                    //FixUnityEditorLinearBug(ref hashed);
                    var hd = new TextureArrayPreviewCache();
                    hd.hash    = hash;
                    hd.texture = hashed;
                    previewCache.Add(hd);
                    if (previewCache.Count > 20)
                    {
                        hd = previewCache[0];
                        previewCache.RemoveAt(0);
                        if (hd.texture != null)
                        {
                            GameObject.DestroyImmediate(hd.texture);
                        }
                    }
                }
                disp = hashed;
            }
            if (compact)
            {
                EditorGUILayout.BeginVertical();
                EditorGUI.DrawPreviewTexture(EditorGUILayout.GetControlRect(GUILayout.Width(110), GUILayout.Height(96)), disp);
                textureIndex = EditorGUILayout.IntSlider(textureIndex, 0, count - 1, GUILayout.Width(120));
                EditorGUILayout.EndVertical();
            }
            else
            {
                string name = "";
                if (cachedConfig != null)
                {
                    if (textureIndex < cachedConfig.sourceTextures.Count && textureIndex >= 0)
                    {
                        var conf = cachedConfig.sourceTextures [textureIndex];
                        if (conf != null && conf.diffuse != null)
                        {
                            name = conf.diffuse.name;
                        }
                    }
                }

                Rect r = EditorGUILayout.GetControlRect(false, GUILayout.Height(128));
                r.width -= 2;
                float width = r.width;


                r.width = 128;
                EditorGUI.DrawPreviewTexture(r, disp);
                r.height = 20;
                r.width  = width - 148;
                r.x     += 148;
                r.y     += 36;

                Rect ridx = r;
                ridx.y += 20;

                ridx.width  -= 44;
                textureIndex = EditorGUI.IntSlider(ridx, textureIndex, 0, count - 1);
                if (textureIndex <= 0)
                {
                    textureIndex = 0;
                    GUI.enabled  = false;
                }
                else
                {
                    GUI.enabled = true;
                }
                ridx.x    += ridx.width;
                ridx.x    += 4;
                ridx.width = 22;
                if (GUI.Button(ridx, "<"))
                {
                    textureIndex -= 1;
                }
                if (textureIndex >= count - 1)
                {
                    textureIndex = count - 1;
                    GUI.enabled  = false;
                }
                else
                {
                    GUI.enabled = true;
                }
                ridx.x += 22;
                if (GUI.Button(ridx, ">"))
                {
                    textureIndex += 1;
                }
                GUI.enabled = true;

                EditorGUI.LabelField(r, name);
            }
            return(textureIndex);
        }