Exemple #1
0
        private static string SliceSprite(string asset)
        {
            var directoryName = Path.GetFileName(Path.GetDirectoryName(asset));
            var directoryPath = Path.Combine(EditorUtil.GetBaumSpritesPath(), directoryName);
            var fileName      = Path.GetFileName(asset);
            var noSlice       = fileName.EndsWith("-noslice.png", StringComparison.Ordinal);

            fileName = fileName.Replace("-noslice.png", ".png");
            var newPath = Path.Combine(directoryPath, fileName);

            var texture       = AssetDatabase.LoadAssetAtPath <Texture2D>(asset);
            var slicedTexture = new SlicedTexture(texture, new Boarder(0, 0, 0, 0));

            if (!noSlice)
            {
                slicedTexture = TextureSlicer.Slice(texture);
            }
            if (PreprocessTexture.SlicedTextures == null)
            {
                PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>();
            }
            PreprocessTexture.SlicedTextures[fileName] = slicedTexture;
            byte[] pngData = slicedTexture.Texture.EncodeToPNG();
            File.WriteAllBytes(newPath, pngData);
            if (!noSlice)
            {
                Object.DestroyImmediate(slicedTexture.Texture);
            }

            // Debug.LogFormat("[Baum2] Slice: {0} -> {1}", EditorUtil.ToUnityPath(asset), EditorUtil.ToUnityPath(newPath));
            return(EditorUtil.ToUnityPath(newPath));
        }
Exemple #2
0
    public void Create()
    {
        //テクスチャのスライス
        TextureSlicer slice = new TextureSlicer();

        texture = slice.createReadableTexture2D(texture);
        slice.Slice_Sprite(texture, new Vector2Int(1, 1));

        Vector2Int range;   //テクスチャの1マスグリッドのうち連続で透明ではないところの範囲(x, 始点 / y, 終点)
        Sprite     sprite;  //タイル一枚のスプライト
        Vector2Int size;    //rangeのサイズ
        Vector2    offset;  //rangeの位置

        //縦に切るとき
        tiles = new Tile[texture.width];
        //縦一列のうち、透明になっていない範囲を取得
        for (int x = 0; x < texture.width; x++)
        {
            range = new Vector2Int(-1, -1);
            for (int y = 0; y < texture.height; y++)
            {
                if (slice.Is_Tranceparent(texture, new Vector2Int(x, y), 0.01f))
                {
                    if (range.y == -1 && range.x != -1)
                    {
                        range.y = y;
                    }
                }
                else if (range.x == -1)
                {
                    range.x = y;
                }
            }
            //すべて透明の時
            if (range.x == -1)
            {
                range = new Vector2Int(0, 0);
            }
            //すべて透明じゃないとき
            if (range.y == -1)
            {
                range = new Vector2Int(0, texture.height);
            }

            sprite   = slice.Slice_Sprite(texture, new Vector2Int(1, texture.height))[x];
            size     = new Vector2Int(1, range.y - range.x);
            offset   = new Vector2(x, range.x + size.y / 2.0f) - center;
            tiles[x] = new Tile(this, sprite, offset, size, x);
            tiles[x].Create();
            Attach_Collision(tiles[x]);
        }
    }
Exemple #3
0
        private static void SliceSprite(string asset)
        {
            var directoryName = Path.GetFileName(Path.GetDirectoryName(asset));
            var directoryPath = Path.Combine(EditorUtil.GetBaumSpritesPath(), directoryName);
            var fileName      = Path.GetFileName(asset);
            var newPath       = Path.Combine(directoryPath, fileName);

            var texture       = AssetDatabase.LoadAssetAtPath <Texture2D>(asset);
            var slicedTexture = TextureSlicer.Slice(texture);

            if (PreprocessTexture.SlicedTextures == null)
            {
                PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>();
            }
            PreprocessTexture.SlicedTextures[fileName] = slicedTexture;
            byte[] pngData = slicedTexture.Texture.EncodeToPNG();
            File.WriteAllBytes(newPath, pngData);
            Object.DestroyImmediate(slicedTexture.Texture);

            // Debug.LogFormat("[Baum2] Slice: {0} -> {1}", EditorUtil.ToUnityPath(asset), EditorUtil.ToUnityPath(newPath));
        }
Exemple #4
0
    //箱を開ける
    private IEnumerator Open_Cor()
    {
        //効果音
        GetComponent <AudioSource>().Play();
        //画像をスライスして、アニメーション再生する
        SpriteRenderer _sprite   = GetComponent <SpriteRenderer>();
        TextureSlicer  tex_Slice = new TextureSlicer();

        Sprite[] sprites     = tex_Slice.Slice_Sprite(option_Box_Texture, new Vector2Int(35, 26));
        int      start_Index = 5 * (int)kind;

        for (int i = start_Index; i < start_Index + 5; i++)
        {
            _sprite.sprite = sprites[i];
            yield return(new WaitForSeconds(0.08f));
        }
        //オプションを出す
        if (kind == Kind.random)
        {
            PlayerManager.Option po = new PlayerManager.Option();
            do
            {
                int r = Random.Range(0, 4);
                switch (r)
                {
                case 0: po = PlayerManager.Option.bee; break;

                case 1: po = PlayerManager.Option.butterfly; break;

                case 2: po = PlayerManager.Option.mantis; break;

                case 3: po = PlayerManager.Option.spider; break;
                }
            } while (po == PlayerManager.Instance.Get_Option());
            transform.GetChild(0).GetComponent <OptionItem>().option = po;
        }
        transform.GetChild(0).gameObject.SetActive(true);
        transform.GetChild(0).SetParent(null);
    }
        /// <summary>
        ///     アセットのイメージをスライスする
        ///     戻り地は、変換リザルトメッセージ
        /// </summary>
        /// <param name="outputPath"></param>
        /// <param name="sourceImagePath"></param>
        /// <returns></returns>
        public static string SliceSprite(string outputPath, string sourceImagePath)
        {
            // オプションJSONの読み込み
            Dictionary <string, object> json = null;
            var imageJsonPath = sourceImagePath + ".json";

            if (File.Exists(imageJsonPath))
            {
                var text = File.ReadAllText(imageJsonPath);
                json = Json.Deserialize(text) as Dictionary <string, object>;
            }

            // PNGを読み込み、同じサイズのTextureを作成する
            var sourceTexture   = CreateTextureFromPng(sourceImagePath);
            var optionJson      = json.GetDic("copy_rect");
            var readableTexture = CreateReadableTexture2D(sourceTexture,
                                                          optionJson?.GetInt("offset_x"),
                                                          optionJson?.GetInt("offset_y"),
                                                          optionJson?.GetInt("width"),
                                                          optionJson?.GetInt("height")
                                                          );

            if (readableTexture == null)
            {
                Debug.LogError($"readableTextureがNULLです{sourceImagePath}");
            }

            // LoadAssetAtPathをつかったテクスチャ読み込み サイズが2のべき乗になる JPGも読める
            // var texture = CreateReadableTexture2D(AssetDatabase.LoadAssetAtPath<Texture2D>(asset));
            if (PreprocessTexture.SlicedTextures == null)
            {
                PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>();
            }

            var slice = json?.Get("slice").ToLower();

            switch (slice)
            {
            case null:
            case "auto":
            {
                var slicedTexture = TextureSlicer.Slice(readableTexture);
                return(CheckWriteSpriteFromTexture(outputPath, slicedTexture.Texture, slicedTexture.Boarder));
            }

            case "none":
            {
                return(CheckWriteSpriteFromTexture(outputPath, readableTexture, new Boarder(0, 0, 0, 0)));
            }

            case "border":
            {
                var border = json.GetDic("slice_border");
                if (border == null)
                {
                    break;                     // borderパラメータがなかった
                }
                // 上・右・下・左の端から内側へのオフセット量
                var top    = border.GetInt("top") ?? 0;
                var right  = border.GetInt("right") ?? 0;
                var bottom = border.GetInt("bottom") ?? 0;
                var left   = border.GetInt("left") ?? 0;

                return(CheckWriteSpriteFromTexture(outputPath, readableTexture, new Boarder(left, bottom, right, top)));
            }
            }
            Debug.LogError($"[{Importer.NAME}] SliceSpriteの処理ができませんでした");
            return(null);
        }
Exemple #6
0
        private static void Setup
        (
            IEnumerable <Texture2D> textureList,
            DisplayProgressBarCallback onDisplayProgressBarPreprocess = default,
            DisplayProgressBarCallback onDisplayProgressBarProcessing = default,
            ClearProgressBarCallback onClearProgressBar = default,
            Action onComplete = default
        )
        {
            var list = textureList
                       .Select(c => new TextureData(c))
                       .ToArray()
            ;

            var count = list.Length;

            try
            {
                AssetDatabase.StartAssetEditing();

                foreach (var(index, val) in list.Select((val, index) => (index, val)))
                {
                    onDisplayProgressBarPreprocess?.Invoke(index + 1, count, val.Path);

                    var importer = val.Importer;
                    importer.isReadable = true;
                    importer.SaveAndReimport();
                }
            }
            finally
            {
                AssetDatabase.StopAssetEditing();
                onClearProgressBar?.Invoke();
            }

            // isReadable を true にしてからじゃないと TextureSlicer.Slice が使用できないため
            // isReadable を true にして 1 フレーム待機してから
            // TextureSlicer.Slice を使用しています
            EditorApplication.delayCall += () =>
            {
                try
                {
                    AssetDatabase.StartAssetEditing();

                    foreach (var(index, val) in list.Select((val, index) => (index, val)))
                    {
                        onDisplayProgressBarProcessing?.Invoke(index + 1, count, val.Path);

                        var slicedTexture = TextureSlicer.Slice(val.Texture);
                        var importer      = val.Importer;
                        importer.spriteBorder = slicedTexture.Boarder.ToVector4();
                        importer.isReadable   = false;
                        importer.SaveAndReimport();
                    }
                }
                finally
                {
                    AssetDatabase.StopAssetEditing();
                    onClearProgressBar?.Invoke();
                    onComplete?.Invoke();
                }
            };
        }
Exemple #7
0
        public static string SliceSprite(string assetPath)
        {
            var directoryName = Path.GetFileName(Path.GetDirectoryName(assetPath));
            var directoryPath = Path.Combine(EditorUtil.GetOutputSpritesPath(), directoryName);
            var fileName      = Path.GetFileName(assetPath);
            // PNGを読み込み、同じサイズのTextureを作成する
            var texture = CreateReadableTexture2D(CreateTextureFromPng(assetPath));

            // LoadAssetAtPathをつかったテクスチャ読み込み サイズが2のべき乗になる JPGも読める
            //var texture = CreateReadabeTexture2D(AssetDatabase.LoadAssetAtPath<Texture2D>(asset));
            if (PreprocessTexture.SlicedTextures == null)
            {
                PreprocessTexture.SlicedTextures = new Dictionary <string, SlicedTexture>();
            }

            var noSlice = fileName.EndsWith("-noslice.png", StringComparison.Ordinal);

            if (noSlice)
            {
                var slicedTexture = new SlicedTexture(texture, new Boarder(0, 0, 0, 0));
                fileName = fileName.Replace("-noslice.png", ".png");
                var newPath = Path.Combine(directoryPath, fileName);
                PreprocessTexture.SlicedTextures[fileName] = slicedTexture;
                var pngData   = texture.EncodeToPNG();
                var imageHash = texture.imageContentsHash;
                Object.DestroyImmediate(slicedTexture.Texture);
                return(CheckWrite(newPath, pngData, imageHash));
            }

            const string pattern = "-9slice,([0-9]+)px,([0-9]+)px,([0-9]+)px,([0-9]+)px\\.png";
            var          matches = Regex.Match(fileName, pattern);

            if (matches.Length > 0)
            {
                // 上・右・下・左の端から内側へのオフセット量
                var top    = Int32.Parse(matches.Groups[1].Value);
                var right  = Int32.Parse(matches.Groups[2].Value);
                var bottom = Int32.Parse(matches.Groups[3].Value);
                var left   = Int32.Parse(matches.Groups[4].Value);

                var slicedTexture = new SlicedTexture(texture, new Boarder(left, bottom, right, top));
                fileName = Regex.Replace(fileName, pattern, ".png");
                var newPath = Path.Combine(directoryPath, fileName);

                PreprocessTexture.SlicedTextures[fileName] = slicedTexture;
                var pngData   = texture.EncodeToPNG();
                var imageHash = texture.imageContentsHash;
                Object.DestroyImmediate(slicedTexture.Texture);
                return(CheckWrite(newPath, pngData, imageHash));
            }

            {
                var slicedTexture = TextureSlicer.Slice(texture);
                var newPath       = Path.Combine(directoryPath, fileName);

                PreprocessTexture.SlicedTextures[fileName] = slicedTexture;
                var pngData   = slicedTexture.Texture.EncodeToPNG();
                var imageHash = texture.imageContentsHash;
                Object.DestroyImmediate(slicedTexture.Texture);
                return(CheckWrite(newPath, pngData, imageHash));
            }
            // Debug.LogFormat("[XdUnityUI] Slice: {0} -> {1}", EditorUtil.ToUnityPath(asset), EditorUtil.ToUnityPath(newPath));
        }