public override void OnInspectorGUI()
    {
        UFTSelectTextureFromAtlas targetObj = (UFTSelectTextureFromAtlas)target;

        targetObj.atlasMetadata = (UFTAtlasMetadata)EditorGUILayout.ObjectField(targetObj.atlasMetadata, typeof(UFTAtlasMetadata), false);

        if (targetObj.atlasMetadata != null)
        {
            int newValue = EditorGUILayout.IntSlider(targetObj.textureIndex, 0, targetObj.atlasMetadata.entries.Length - 1);
            if (newValue != targetObj.textureIndex)
            {
                targetObj.textureIndex = newValue;
                if (atlasEntryMetadata != null)
                {
                    serializedObject.Update();
                }
            }
        }

        GUI.enabled = false;
        if (atlasEntryMetadata != null)
        {
            EditorGUILayout.PropertyField(atlasEntryMetadata, true);
        }
        GUI.enabled = true;


        if (!targetObj.isUV2Empty())
        {
            if (GUILayout.Button("restore original uv"))
            {
                targetObj.restoreOriginalUVS();
            }
        }

        //debug
        debug = EditorGUILayout.Toggle("UV Debug mode", debug);

        if (debug)
        {
            EditorGUILayout.LabelField("==========================================");
            EditorGUILayout.LabelField("uv (actual uv):");
            Vector2[] uv = UFTMeshUtil.getObjectMesh(targetObj.gameObject).uv;
            foreach (Vector2 v in uv)
            {
                EditorGUILayout.Vector2Field("", v);
            }

            EditorGUILayout.LabelField("==========================================");
            EditorGUILayout.LabelField("uv2 (original uv):");
            Vector2[] uv2 = UFTMeshUtil.getObjectMesh(targetObj.gameObject).uv2;
            foreach (Vector2 v in uv2)
            {
                EditorGUILayout.Vector2Field("", v);
            }
        }
    }
    public void restoreOriginalUVS()
    {
        Mesh mesh = UFTMeshUtil.getObjectMesh(gameObject);

        if (isUV2Empty())
        {
            throw new System.Exception("mesh.uv2 of your object is wrong, can't restore original values");
        }

        mesh.uv = (Vector2[])mesh.uv2.Clone();
    }
    public void storeOriginalUV()
    {
        Mesh mesh = UFTMeshUtil.getObjectMesh(gameObject);

        if (isUV2Empty())
        {
            mesh.uv2 = (Vector2[])mesh.uv.Clone();
        }
        else
        {
            mesh.uv = (Vector2[])mesh.uv2.Clone();
        }
    }
    /// <summary>
    /// Updates mesh uv, we will take original mesh.uv from uv2 coordinates and then multiply to atlas position
    /// </summary>
    public void updateUV()
    {
        Rect rect = atlasMetadata.entries[textureIndex].uvRect;
        Mesh mesh = UFTMeshUtil.getObjectMesh(gameObject);

        Vector2[] uvs = new Vector2[mesh.uv2.Length];
        for (int i = 0; i < uvs.Length; i++)
        {
            uvs[i].x = mesh.uv2[i].x * rect.width + rect.x;
            uvs[i].y = mesh.uv2[i].y * rect.height + rect.y;
        }
        mesh.uv = uvs;
    }
    public override void OnInspectorGUI()
    {
        UFTSelectTextureFromAtlas script = ((UFTSelectTextureFromAtlas)target);

        if (script.atlasMetadata != null)
        {
            EditorGUILayout.IntSlider(textureIndex, 0, script.atlasMetadata.entries.Length - 1);
        }

        EditorGUILayout.PropertyField(atlasMetadata);
        serializedObject.ApplyModifiedProperties();

        if (GUI.changed)
        {
            ((UFTSelectTextureFromAtlas)target).updateUV();
        }

        if (!script.isUV2Empty())
        {
            if (GUILayout.Button("restore original uv"))
            {
                script.restoreOriginalUVS();
            }
        }

        //debug
        debug = EditorGUILayout.Toggle("UV Debug mode", debug);

        if (debug)
        {
            EditorGUILayout.LabelField("==========================================");
            EditorGUILayout.LabelField("uv (actual uv):");
            Vector2[] uv = UFTMeshUtil.getObjectMesh(script.gameObject).uv;
            foreach (Vector2 v in uv)
            {
                EditorGUILayout.Vector2Field("", v);
            }

            EditorGUILayout.LabelField("==========================================");
            EditorGUILayout.LabelField("uv2 (original uv):");
            Vector2[] uv2 = UFTMeshUtil.getObjectMesh(script.gameObject).uv2;
            foreach (Vector2 v in uv2)
            {
                EditorGUILayout.Vector2Field("", v);
            }
        }
    }
    public bool isUV2Empty()
    {
        Mesh mesh = UFTMeshUtil.getObjectMesh(gameObject);

        return(mesh.uv2.Length == 0 || mesh.uv2 == null);
    }