private void LoadDataLoad()
 {
     if (File.Exists(GetSavePath(assetName) + "Load.xml"))
     {
         LoadData d = ReadParams <LoadData>(assetName, "Load.xml");
         xoff            = d.xoff;
         yoff            = d.yoff;
         doSeamless      = d.seamless;
         periodize.vars  = d.periodize;
         factorTwo       = (FactorTwo)d.factorTwo;
         seamlessMaskIdx = (SeamLessMaskIndices)d.seamlessMaskIdx;
         outputDir       = d.outputDir;
     }
     else    //reset everything
     {
         xoff       = 0f;
         yoff       = 0f;
         doSeamless = true;
         periodize.ResetPara();
         factorTwo       = FactorTwo.One;
         seamlessMaskIdx = SeamLessMaskIndices.s1;
         outputDir       = "";
     }
 }
    private void UpdateLoad(float y2, float x1, float x2, bool changedState)
    {
        bool doUpdate       = false;
        bool reloadSeamless = false;

        Texture2D texInput_ = EditorGUI.ObjectField(new Rect(x2, y2, 110, 110), texInput, typeof(Texture2D), true) as Texture2D;

        doUpdate = OnTextureLoad(texInput_);//change the import settings if needed

        string outputDir_ = EditorGUI.TextField(new Rect(x2, y2 + 120, 110, 20), new GUIContent("", "Name of the texture"), outputDir);
        bool   savePara   = HasChanged(ref outputDir, outputDir_, doUpdate);

        if (outputDir == "" && texInput)
        {
            outputDir = Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(texInput));
        }

        if (texInput && outputDir != "")
        {
            GUI.Label(new Rect(160, position.width - 140, 400, 20), "Output directory:");
            GUI.Label(new Rect(160, position.width - 120, 400, 20), "Assets/Textures/" + outputDir + "/");

            FactorTwo factorTwo_ = (FactorTwo)EditorGUI.EnumPopup(new Rect(x2, y2 + 171, 60, 20), factorTwo);

            if (factorTwo_ != factorTwo)
            {
                factorTwo = factorTwo_;
                doUpdate  = true;
            }

            int N = GetTextureSize();//compute target size

            int divFac = (int)factorTwo;
            divFac = (int)Mathf.Pow(2f, (float)divFac);

            GUI.Label(new Rect(x2, y2 + 190, 120, 20), new GUIContent("X offset", "Horizontal offset in the texture"));
            float xoff2 = EditorGUI.Slider(new Rect(x2, y2 + 205, 120, 20), new GUIContent("", "Horizontal offset in the texture"), xoff, 0f, texInput == null ? 1f : Mathf.Max(0f, texInput.width - N));

            GUI.Label(new Rect(x2, y2 + 225, 120, 20), new GUIContent("Y offset", "Vertical offset in the texture"));
            float yoff2 = EditorGUI.Slider(new Rect(x2, y2 + 240, 120, 20), new GUIContent("", "Vertical offset in the texture"), yoff, 0f, texInput == null ? 1f : Mathf.Max(0f, texInput.height - N));

            doUpdate = HasChanged(ref xoff, xoff2, doUpdate);
            doUpdate = HasChanged(ref yoff, yoff2, doUpdate);

            //RNG
            if (GUI.Button(new Rect(x2, y2 + 380, 60, 20), new GUIContent("RNG", "Randomize parameters")))
            {
                seamlessMaskIdx = GetRandomEnum <SeamLessMaskIndices>();
                xoff            = Mathf.Floor(UnityEngine.Random.Range(0, Mathf.Max(0f, texInput.width - N)));
                yoff            = Mathf.Floor(UnityEngine.Random.Range(0, Mathf.Max(0f, texInput.height - N)));
                doUpdate        = true;
                reloadSeamless  = true;
            }

            //Crop
            GUI.Label(new Rect(x2, y2 + 150, 120, 20), new GUIContent("Down scale", "Reduce resolution"));
            if (doUpdate || changedState)
            {
                if (!texCropped || texCropped.width != N)
                {
                    DestroyImmediate(texCropped);
                    texCropped           = new Texture2D(N, N);
                    texCropped.hideFlags = HideFlags.HideAndDontSave;
                }

                if (tex2 == null || tex2.width != texInput.width || tex2.height != texInput.height)
                {
                    DestroyImmediate(tex2);
                    tex2           = new RenderTexture(texInput.width, texInput.height, 16);
                    tex2.hideFlags = HideFlags.HideAndDontSave;
                }

                Graphics.Blit(texInput, tex2);

                RenderTexture.active = tex2;
                texCropped.ReadPixels(new Rect(xoff, yoff, N, N), 0, 0);
                texCropped.Apply();
                RenderTexture.active = null;

                DestroyImmediate(tex2);
                tex2           = new RenderTexture(N / divFac, N / divFac, 16);
                tex2.hideFlags = HideFlags.HideAndDontSave;

                Graphics.Blit(texCropped, tex2);

                ToTexture2D(tex2, ref texCropped);
                ToTexture2D(tex2, ref tex);//copy in case we don't use seamless version

                //Debug.Log("Target size: " + texCropped.width);

                //Graphics.Blit(texCropped, tex2);
            }



            GUI.Label(new Rect(x2, y2 + 280, 80, 20), "Seamless");
            bool doSeamless2 = EditorGUI.Toggle(new Rect(x2 + 85, y2 + 280, 60, 20), doSeamless);

            SeamLessMaskIndices seamlessMaskIdx_ = (SeamLessMaskIndices)EditorGUI.EnumPopup(new Rect(x2, y2 + 300, 60, 20), seamlessMaskIdx);

            if (seamlessMaskIdx_ != seamlessMaskIdx || reloadSeamless)
            {
                seamlessMaskIdx = seamlessMaskIdx_;
                doUpdate        = true;
                LoadSeamLessMask();
            }

            //Make seamless
            DrawSliders(x2, y2 + 315, periodize);

            if (periodize.CheckChange() || doUpdate || changedState || HasChanged(ref doSeamless, doSeamless2, doUpdate))
            {
                doUpdate = true;

                if (doSeamless)
                {
                    periodize.mat.SetTexture("_MaskX", seamless1);
                    periodize.mat.SetTexture("_MaskY", seamless2);

                    periodize.ApplyShader(texCropped, tex2, 1);

                    ToTexture2D(tex2, ref tex);
                }
                else   //just copy he cropped version
                {
                    Graphics.Blit(texCropped, tex2);
                }
            }

            //Save parameters
            if (doUpdate || savePara)
            {
                SaveDataLoad();
            }
        }
    }