Example #1
0
    private IEnumerator DoSetupRoutine(Texture2D originalTexture, string originalTexturePath,
                                       SlicedTextureInfo savedTextureInfo = null)
    {
        BackButtonManager.Instance.Suspend();
        PrepareUIForSetup();

        var originalSize = new Vector2(originalTexture.width, originalTexture.height);
        var slicingInfo  = savedTextureInfo == null?ImgSlicer.GetSliceInfo(originalSize, startMenu.SelectedDifficulty)
                               : new SlicingInfo(savedTextureInfo);

        InitPlayFieldAndContainers(slicingInfo, originalTexture);

        Texture2D slicedTexture;
        var       couldLoadSavedTexture = TextureUtility.TryLoadSavedTexture(originalSize, slicingInfo.rows, slicingInfo.columns,
                                                                             savedTextureInfo, originalTexturePath, out slicedTexture);

        if (!couldLoadSavedTexture)
        {
            var pieceConnections = ImgSlicer.SetupConnections(slicingInfo);
            yield return(ImgSlicer.CreateAndSaveSlicedTextureRoutine(slicingInfo, originalTexture, slicedTexture,
                                                                     originalTexturePath, maskContainer, pieceConnections,
                                                                     loadingScreen.SetLoadStatus));
        }

        slicedTexture.wrapMode = TextureWrapMode.Clamp;
        slicedTexture.Apply(true, true);

        SetupPrefabs(slicingInfo.rows, slicingInfo.columns, slicedTexture, couldLoadSavedTexture);

        Resources.UnloadUnusedAssets();
        DoStartGame();
    }
Example #2
0
    public static bool TryGetSavedTextureInfo(out SlicedTextureInfo savedTextureInfo)
    {
        var pathToSavedFileInfo = Paths.GetFullPathToSavedTextureInfo();

        savedTextureInfo = null;

        if (System.IO.File.Exists(pathToSavedFileInfo))
        {
            string asJson = "";

            try
            {
                asJson = System.IO.File.ReadAllText(pathToSavedFileInfo);
                if (!string.IsNullOrEmpty(asJson))
                {
                    savedTextureInfo = JsonUtility.FromJson <SlicedTextureInfo>(asJson);
                }
            }
            catch (System.Exception e)
            {
                Debug.LogWarning(e.Message);
            }
        }

        return(savedTextureInfo != null);
    }
Example #3
0
    public static void SaveTextureInfo(int rows, int columns, Vector2 originalSize, string originalTexturePath)
    {
        var info = new SlicedTextureInfo
        {
            rows          = rows,
            columns       = columns,
            originalPath  = originalTexturePath,
            originalSizeX = (int)originalSize.x,
            originalSizeY = (int)originalSize.y
        };

        var asJson = JsonUtility.ToJson(info, true);
        var path   = Paths.GetFullPathToSavedTextureInfo();

        try
        {
            System.IO.File.WriteAllText(path, asJson);
        }
        catch (System.Exception e)
        {
            Debug.LogWarning(e.Message);
        }

        Debug.LogWarning("Saved info to: " + path);
    }
Example #4
0
    public SlicingInfo(SlicedTextureInfo textureInfo)
    {
        imgWidthInPixels  = textureInfo.originalSizeX;
        imgHeightInPixels = textureInfo.originalSizeY;

        rows    = textureInfo.rows;
        columns = textureInfo.columns;
    }
Example #5
0
    public static bool TryLoadSavedTexture(Vector2 originalSize, int rows, int columns,
                                           SlicedTextureInfo slicedTextureInfo, string originalTexturePath,
                                           out Texture2D slicedTexture)
    {
        var slicedTextureSizeFactor = 1f + (ImgSlicer.PADDING_RATIO * 2f);

        var slicedTextureSize = originalSize * slicedTextureSizeFactor;
        var slicedTexSizeX    = Mathf.CeilToInt(slicedTextureSize.x);
        var slicedTexSizeY    = Mathf.CeilToInt(slicedTextureSize.y);

        slicedTexture = new Texture2D(slicedTexSizeX, slicedTexSizeY);

        var pathToSavedTexture = Paths.GetFullPathToSavedTexture();

        if (System.IO.File.Exists(pathToSavedTexture) && slicedTextureInfo != null && slicedTextureInfo.originalPath == originalTexturePath)
        {
            var success = true;
            try
            {
                var rawTextureData = System.IO.File.ReadAllBytes(pathToSavedTexture);
                ImageConversion.LoadImage(slicedTexture, rawTextureData);
            }
            catch (System.Exception e)
            {
                Debug.LogWarning(e.Message);
                success = false;
            }

            if (success)
            {
                return(true);
            }
        }

        slicedTexture.SetPixels(new Color[slicedTexSizeX * slicedTexSizeY], 0);
        return(false);
    }