public bool PEGI()
        {
            bool changed = false;

#if UNITY_EDITOR
            var painter = PlaytimePainter.inspectedPainter;
            inspectedAtlas = this;


            painter.SetOriginalShaderOnThis();

            Material mat = painter.GetMaterial(false);

            if ((mat != null) && ((mat != originalMaterial) || mat.shader != originalShader))
            {
                originalMaterial = mat;
                originalShader   = mat.shader;
                OnChangeMaterial(painter);
            }
            changed |= "Name".edit(50, ref name).nl();
            if ("Hint".foldout(ref showHint).nl())
            {
                ("If you don't set Atlased Material(Destination)  it will try to create a copy of current material and set isAtlased toggle on it, if it has one." +
                 " Below you can see: list of Texture Properties, for each you can select or create an atlas. Atlas is a class that holds all textures assigned to an atlas, and also creates and stores the atlas itself." +
                 "After this you can select a field from current Material, texture of which will be copied into an atlas. A bit confusing, I know)" +
                 "Also if stuff looks smudged, rebuild the light.").writeHint();
            }

            if (("Atlased Material:".edit(90, ref AtlasedMaterial).nl()) ||
                (AtlasedMaterial != null && AtlasedMaterial.shader != atlasedShader))
            {
                OnChangeMaterial(painter);
                changed = true;
            }

            if (painter != null)
            {
                var mats = painter.GetMaterials();
                if (mats != null)
                {
                    if (mats.Length > 1)
                    {
                        if ("Source Material:".select("Same as selecting a submesh, which will be converted", 90, ref painter.selectedSubmesh, mats))
                        {
                            changed = true;
                            OnChangeMaterial(painter);
                        }
                    }
                    else if (mats.Length > 0)
                    {
                        "Source Material".write("Submesh which will be converted", 90, mats[0]);
                    }
                }
                pegi.newLine();
                pegi.Space();
                pegi.newLine();
            }



            pegi.Space();
            pegi.newLine();

            foreach (var f in fields)
            {
                changed |= f.Nested_Inspect();
            }

            changed |= "Mesh Profile".select(110, ref matAtlasProfile, PainterCamera.Data.meshPackagingSolutions).nl();

            if ((DestinationMaterial != null) && (!DestinationMaterial.HasProperty(PainterDataAndConfig.isAtlasedProperty)))
            {
                if (AtlasedMaterial == null)
                {
                    pegi.writeHint("Original Material doesn't have isAtlased property, change shader or add Destination Atlased Material");
                }
                else
                {
                    pegi.writeHint("Atlased Material doesn't have isAtlased property");
                }
            }
            else if (originalMaterial != null)
            {
                string names = "";
                foreach (var f in fields)
                {
                    if (f.enabled && f.AtlasCreator == null)
                    {
                        names += f.atlasedField + ", ";
                    }
                }

                if (names.Length > 0)
                {
                    pegi.writeHint("Fields " + names + " don't have atlases assigned to them, create some");
                }
                else if ("Convert to Atlased".Click())
                {
                    ConvertToAtlased(painter);
                }
            }
#endif

            inspectedAtlas = null;

            return(changed);
        }
        public override bool Inspect()
        {
            var changed = false;

#if UNITY_EDITOR
            var painter = PlaytimePainter.inspected;
            inspectedAtlas = this;


            painter.SetOriginalShaderOnThis();

            var mat = painter.Material;

            if ((mat) && ((mat != originalMaterial) || mat.shader != _originalShader))
            {
                originalMaterial = mat;
                _originalShader  = mat.shader;
                OnChangeMaterial(painter);
            }

            "Name".edit(50, ref name).nl(ref changed);

            if ("Hint".foldout(ref _showHint).nl())
            {
                ("If you don't set Atlased Material(Destination)  it will try to create a copy of current material and set isAtlased toggle on it, if it has one." +
                 " Below you can see: list of Texture Properties, for each you can select or create an atlas. Atlas is a class that holds all textures assigned to an atlas, and also creates and stores the atlas itself." +
                 "After this you can select a field from current Material, texture of which will be copied into an atlas. A bit confusing, I know)" +
                 "Also if light looks smudged, rebuild the light.").writeHint();
            }

            if (("Atlased Material:".edit(90, ref _atlasedMaterial).nl() ||
                 (_atlasedMaterial && _atlasedMaterial.shader != _atlasedShader)).changes(ref changed))
            {
                OnChangeMaterial(painter);
            }


            if (painter)
            {
                var mats = painter.Materials;
                if (mats != null)
                {
                    if (mats.Length > 1)
                    {
                        if ("Source Material:".select_Index("Same as selecting a sub Mesh, which will be converted", 90, ref painter.selectedSubMesh, mats).changes(ref changed))
                        {
                            OnChangeMaterial(painter);
                        }
                    }
                    else if (mats.Length > 0)
                    {
                        "Source Material".writeUobj("Sub Mesh which will be converted", 90, mats[0]);
                    }
                }
                pegi.nl();
                pegi.space();
                pegi.nl();
            }



            pegi.space();
            pegi.nl();

            foreach (var f in _fields)
            {
                f.Nested_Inspect().nl(ref changed);
            }

            "Mesh Profiles [{0}]".F(PainterCamera.Data.meshPackagingSolutions.Count)
            .select_Index(140, ref _matAtlasProfile, PainterCamera.Data.meshPackagingSolutions).nl(ref changed);

            if (DestinationMaterial && !DestinationMaterial.HasProperty(PainterDataAndConfig.isAtlasedProperty))
            {
                if (!_atlasedMaterial)
                {
                    "Original Material doesn't have isAtlased property, change shader or add Destination Atlased Material".writeHint();
                }
                else
                {
                    "Atlased Material doesn't have isAtlased property".writeHint();
                }
            }
            else if (originalMaterial)
            {
                var names = "";
                foreach (var f in _fields)
                {
                    if (f.enabled && f.AtlasCreator == null)
                    {
                        names += f.atlasedField + ", ";
                    }
                }

                if (names.Length > 0)
                {
                    ("Fields " + names + " don't have atlases assigned to them, create some").writeHint();
                }
                else if ("Convert to Atlased".Click())
                {
                    ConvertToAtlased(painter);
                }
            }
#endif

            inspectedAtlas = null;

            return(changed);
        }
        public void OnChangeMaterial(PlaytimePainter painter)
        {
#if UNITY_EDITOR
            if (originalMaterial != null)
            {
                originalTextures = originalMaterial.GetTextures();
            }

            if ((DestinationMaterial != null) && (DestinationMaterial.HasProperty(PainterDataAndConfig.isAtlasedProperty)))
            {
                List <string> aTextures = DestinationMaterial.GetTextures();
                fields.Clear();
                for (int i = 0; i < aTextures.Count; i++)
                {
                    FieldAtlas ac = new FieldAtlas();
                    fields.Add(ac);
                    ac.atlasedField = aTextures[i];
                }

#if PEGI
                atlasedShader = DestinationMaterial.shader;
#endif

                foreach (var p in MaterialEditor.GetMaterialProperties(new Material[] { DestinationMaterial }))
                {
                    if (p.displayName.Contains(PainterDataAndConfig.isAtlasableDisaplyNameTag))
                    {
                        foreach (var f in fields)
                        {
                            if (f.atlasedField.SameAs(p.name))
                            {
                                f.enabled = true;
                                continue;
                            }
                        }
                    }
                }

                if (AtlasedMaterial == null)
                {
                    for (int i = 0; i < fields.Count; i++)
                    {
                        fields[i].originField = i;
                    }
                }
                else if (originalMaterial != null)
                {
                    var orTexs = originalMaterial.GetTextures();
                    foreach (var f in fields)
                    {
                        for (int i = 0; i < orTexs.Count; i++)
                        {
                            if (orTexs[i].SameAs(f.atlasedField))
                            {
                                f.originField = i;
                            }
                        }
                    }
                }
            }

            if (originalMaterial != null)
            {
                for (int i = 0; i < fields.Count; i++)
                {
                    FindAtlas(i);
                }
            }
#endif
        }
        private void OnChangeMaterial(PlaytimePainter painter)
        {
            #if UNITY_EDITOR
            if (originalMaterial)
            {
                originalTextures = originalMaterial.MyGetTextureProperties();
            }

            if ((DestinationMaterial) && (DestinationMaterial.HasProperty(PainterDataAndConfig.isAtlasedProperty)))
            {
                var aTextures = DestinationMaterial.MyGetTextureProperties();
                _fields.Clear();
                foreach (var t in aTextures)
                {
                    var ac = new FieldAtlas();
                    _fields.Add(ac);
                    ac.atlasedField = t.NameForDisplayPEGI();
                }

                _atlasedShader = DestinationMaterial.shader;


                foreach (var p in MaterialEditor.GetMaterialProperties(new Object[] { DestinationMaterial }))
                {
                    if (p.displayName.Contains(PainterDataAndConfig.isAtlasableDisaplyNameTag))
                    {
                        foreach (var f in _fields)
                        {
                            if (f.atlasedField.SameAs(p.name))
                            {
                                f.enabled = true;
                            }
                        }
                    }
                }


                if (!_atlasedMaterial)
                {
                    for (var i = 0; i < _fields.Count; i++)
                    {
                        _fields[i].originField = i;
                    }
                }
                else if (originalMaterial)
                {
                    var orTexts = originalMaterial.MyGetTextureProperties();
                    foreach (var f in _fields)
                    {
                        for (var i = 0; i < orTexts.Count; i++)
                        {
                            if (orTexts[i].NameForDisplayPEGI().SameAs(f.atlasedField))
                            {
                                f.originField = i;
                            }
                        }
                    }
                }
            }

            if (!originalMaterial)
            {
                return;
            }

            for (var i = 0; i < _fields.Count; i++)
            {
                FindAtlas(i);
            }
#endif
        }