public void ConvertToAtlased(PlaytimePainter painter)
        {
#if UNITY_EDITOR
            if (AtlasedMaterial == null)
            {
                AtlasedMaterial = painter.InstantiateMaterial(true);
            }

            painter.SetOriginalShaderOnThis();

            painter.UpdateOrSetTexTarget(TexTarget.Texture2D);

            Material      mat     = painter.GetMaterial(false);
            List <string> tfields = mat.GetTextures();

            int index = 0;
            List <FieldAtlas> passedFields   = new List <FieldAtlas>();
            List <Texture2D>  passedTextures = new List <Texture2D>();
            List <Color>      passedColors   = new List <Color>();

            foreach (var f in fields)
            {
                if ((f.enabled) && (f.AtlasCreator != null) && (tfields.Contains(originalTextures[f.originField])))
                {
                    string original = originalTextures[f.originField];

                    Texture tex = mat.GetTexture(original);

                    Texture2D texture = null;

                    if (tex == null)
                    {
                        var note = painter.name + " no " + original + " texture. Using Color.";
#if PEGI
                        note.showNotification();
#endif
                        Debug.Log(note);
                    }
                    else
                    {
                        if (tex.GetType() != typeof(Texture2D))
                        {
                            Debug.Log("Not a Texture 2D: " + original);
                            return;
                        }

                        texture = (Texture2D)tex;
                    }

                    var aTexes = f.AtlasCreator.textures;

                    bool added = false;

                    for (int i = index; i < aTexes.Count; i++)
                    {
                        if ((aTexes[i] == null) || (!aTexes[i].used) || (aTexes[i].texture == texture))
                        {
                            index = i;
                            passedFields.Add(f);
                            passedTextures.Add(texture);
                            passedColors.Add(f.col);
                            added = true;
                            break;
                        }
                    }

                    if (!added)
                    {
                        Debug.Log("Could not find a place for " + original);
                        return;
                    }
                }
            }

            if (passedFields.Count > 0)
            {
                bool firstAtlasing = false;

                var atlPlug = painter.GetPlugin <TileableAtlasingPainterPlugin>();

                if (atlPlug.preAtlasingMaterials == null)
                {
                    atlPlug.preAtlasingMaterials = painter.GetMaterials();
                    atlPlug.preAtlasingMesh      = painter.GetMesh();
                    firstAtlasing = true;
                }

                var MainField = passedFields[0];

                atlPlug.atlasRows = MainField.AtlasCreator.Row;

                Vector2 tyling = mat.GetTextureScale(originalTextures[MainField.originField]);
                Vector2 offset = mat.GetTextureOffset(originalTextures[MainField.originField]);

                for (int i = 0; i < passedFields.Count; i++)
                {// var f in passedFields){
                    var f  = passedFields[i];
                    var ac = f.AtlasCreator;

                    ac.textures[index] = new AtlasTextureField(passedTextures[i], passedColors[i]);

                    ac.AddTargets(f, originalTextures[f.originField]);
                    ac.ReconstructAsset();
                    AtlasedMaterial.SetTexture(f.atlasedField, ac.a_texture);
                }

                MeshManager.Inst.EditMesh(painter, true);

                if (firstAtlasing)
                {
                    atlPlug.preAtlasingSavedMesh = MeshManager.Inst.edMesh.Encode().ToString();
                }

                painter.selectedMeshProfile = matAtlasProfile;

                if ((tyling != Vector2.one) || (offset != Vector2.zero))
                {
                    MeshManager.Inst.edMesh.TileAndOffsetUVs(offset, tyling, 0);
                    Debug.Log("offsetting " + offset + " tyling " + tyling);
                }

                TriangleAtlasTool.Inst.SetAllTrianglesTextureTo(index, 0, painter.selectedSubmesh);
                MeshManager.Inst.Redraw();
                MeshManager.Inst.DisconnectMesh();

                AtlasedMaterial.SetFloat(PainterDataAndConfig.atlasedTexturesInARow, atlPlug.atlasRows);
                painter.Material = AtlasedMaterial;

                if (firstAtlasing)
                {
                    var m = painter.GetMesh();
                    m.name = m.name + "_Atlased_" + index;
                }
                //painter.getMaterial(false).SetTextureOffset(1,Vector2.zero);

                AtlasedMaterial.EnableKeyword(PainterDataAndConfig.UV_ATLASED);
            }
#endif
        }
        private void ConvertToAtlased(PlaytimePainter painter)
        {
            #if UNITY_EDITOR
            if (!_atlasedMaterial)
            {
                _atlasedMaterial = painter.InstantiateMaterial(true);
            }

            painter.SetOriginalShaderOnThis();

            painter.UpdateOrSetTexTarget(TexTarget.Texture2D);

            var mat           = painter.Material;
            var texProperties = mat.MyGetTextureProperties_Editor();

            var index          = 0;
            var passedFields   = new List <FieldAtlas>();
            var passedTextures = new List <Texture2D>();
            var passedColors   = new List <Color>();

            foreach (var f in _fields)
            {
                if (f.enabled && f.AtlasCreator != null)
                {
                    var contains = false;

                    foreach (var p in texProperties)
                    {
                        if (p.NameForDisplayPEGI().Equals(originalTextures[f.originField].NameForDisplayPEGI()))
                        {
                            contains = true;
                            break;
                        }
                    }

                    if (!contains)
                    {
                        continue;
                    }

                    var original = originalTextures[f.originField];

                    var tex = mat.Get(original);

                    Texture2D texture = null;

                    if (!tex)
                    {
                        var note = painter.name + " no " + original + " texture. Using Color.";

                        pegi.GameView.ShowNotification(note);

                        Debug.Log(note);
                    }
                    else
                    {
                        if (tex.GetType() != typeof(Texture2D))
                        {
                            Debug.Log("Not a Texture 2D: " + original);
                            return;
                        }

                        texture = (Texture2D)tex;
                    }

                    var aTextures = f.AtlasCreator.textures;

                    var added = false;

                    for (var i = index; i < aTextures.Count; i++)
                    {
                        if ((aTextures[i] == null) || (!aTextures[i].used) || (aTextures[i].texture == texture))
                        {
                            index = i;
                            passedFields.Add(f);
                            passedTextures.Add(texture);
                            passedColors.Add(f.col);
                            added = true;
                            break;
                        }
                    }

                    if (added)
                    {
                        continue;
                    }

                    Debug.Log("Could not find a place for " + original);

                    return;
                }
            }

            if (passedFields.Count <= 0)
            {
                return;
            }

            var firstAtlasing = false;

            var atlPlug = painter.GetModule <TileableAtlasingComponentModule>();

            if (atlPlug.preAtlasingMaterials == null)
            {
                atlPlug.preAtlasingMaterials = painter.Materials.ToList();
                atlPlug.preAtlasingMesh      = painter.GetMesh();
                firstAtlasing = true;
            }

            var mainField = passedFields[0];

            atlPlug.atlasRows = mainField.AtlasCreator.Row;

            var tiling = mat.GetTiling(originalTextures[mainField.originField]);
            var offset = mat.GetOffset(originalTextures[mainField.originField]);

            for (var i = 0; i < passedFields.Count; i++)
            {
                var f  = passedFields[i];
                var ac = f.AtlasCreator;

                ac.textures[index] = new AtlasTextureField(passedTextures[i], passedColors[i]);

                ac.AddTargets(f, originalTextures[f.originField]);
                ac.ReconstructAsset();
                _atlasedMaterial.SetTexture(f.atlasedField, ac.aTexture);
            }

            MeshEditorManager.Inst.EditMesh(painter, true);

            if (firstAtlasing)
            {
                atlPlug.preAtlasingSavedMesh = MeshEditorManager.editedMesh.Encode().ToString();
            }

            painter.selectedMeshProfile = _matAtlasProfile;

            if (tiling != Vector2.one || offset != Vector2.zero)
            {
                MeshEditorManager.editedMesh.TileAndOffsetUVs(offset, tiling, 0);
                Debug.Log("offsetting " + offset + " tiling " + tiling);
            }

            TriangleAtlasTool.Inst.SetAllTrianglesTextureTo(index, 0, painter.selectedSubMesh);
            MeshEditorManager.Inst.Redraw();
            MeshEditorManager.Inst.StopEditingMesh();

            _atlasedMaterial.SetFloat(PainterShaderVariables.ATLASED_TEXTURES, atlPlug.atlasRows);
            painter.Material = _atlasedMaterial;

            if (firstAtlasing)
            {
                var m = painter.GetMesh();
                m.name = m.name + "_Atlased_" + index;
            }

            _atlasedMaterial.EnableKeyword(PainterShaderVariables.UV_ATLASED);
#endif
        }