Exemplo n.º 1
0
        public void Start()
        {
            var bytes = File.ReadAllBytes(LargeSample);
            var gif   = Gif.Decode(bytes);

            AnimatedImage.Play(gif);
        }
Exemplo n.º 2
0
        public static void EncodeDecodeSaveTest()
        {
            var gif       = DecodeExample();
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var binary = gif.Encode();

            stopwatch.Stop();

            Console.WriteLine("GIF encoded in {0:n2}s to binary.", stopwatch.Elapsed.TotalSeconds);

            stopwatch.Reset();
            stopwatch.Start();

            Gif.Decode(binary);

            Console.WriteLine("GIF loaded from binary in {0:n2}s.", stopwatch.Elapsed.TotalSeconds);

            var path = Path.Replace(".gif", "_.gif");

            File.WriteAllBytes(path, binary);

            Console.WriteLine("GIF saved as {0}.", path);
            Console.WriteLine("Test passed!");
        }
Exemplo n.º 3
0
        public void Start()
        {
            var path = "Assets/GifAssets/PowerGif/Examples/Samples/Small.gif";

            if (path == "")
            {
                return;
            }

            var bytes = File.ReadAllBytes(path);

            _stopwatch.Reset();
            _stopwatch.Start();

            var gif = Gif.Decode(bytes);

            UnityEngine.Debug.LogFormat("Decoded in {0:N2}s", _stopwatch.Elapsed.TotalSeconds);

            _stopwatch.Reset();
            _stopwatch.Start();

            gif.Encode();

            UnityEngine.Debug.LogFormat("Encoded in {0:N2}s", _stopwatch.Elapsed.TotalSeconds);

            AnimatedImage.Play(gif);
        }
Exemplo n.º 4
0
    public IEnumerator playGIFAnimation()
    {
        animatedImage.gameObject.SetActive(true);
        //var bytes = File.ReadAllBytes("Assets/Resources/Skills/抓.gif");
        var bytes = File.ReadAllBytes("Assets/GifAssets/PowerGif/Examples/Samples/Large.gif");
        var gif   = Gif.Decode(bytes);

        yield return(animatedImage.ExPlay(gif));
        //gameObject.SetActive(false);
    }
        public void Start()
        {
            var path = "Assets/GifAssets/PowerGif/Examples/Samples/Large.gif";

            if (path == "")
            {
                return;
            }

            var bytes = File.ReadAllBytes(path);
            var gif   = Gif.Decode(bytes);

            AnimatedImage.Play(gif);
        }
Exemplo n.º 6
0
        public static Gif DecodeExample()
        {
            var bytes     = File.ReadAllBytes(Path);
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var gif = Gif.Decode(bytes);

            stopwatch.Stop();

            Console.WriteLine("GIF loaded in {0:n2}s, size: {1}x{2}, frames: {3}.", stopwatch.Elapsed.TotalSeconds,
                              gif.Frames[0].Texture.width, gif.Frames[0].Texture.height, gif.Frames.Count);

            return(gif);
        }
Exemplo n.º 7
0
        public void Start()
        {
            var bytes = File.ReadAllBytes(SmallSample);

            _stopwatch.Reset();
            _stopwatch.Start();

            var gif = Gif.Decode(bytes);

            UnityEngine.Debug.LogFormat("Decoded in {0:N2}s", _stopwatch.Elapsed.TotalSeconds);

            _stopwatch.Reset();
            _stopwatch.Start();

            gif.Encode();

            UnityEngine.Debug.LogFormat("Encoded in {0:N2}s", _stopwatch.Elapsed.TotalSeconds);

            AnimatedImage.Play(gif);
        }
Exemplo n.º 8
0
        public IEnumerator Start()
        {
            var path = UnityEditor.EditorUtility.SaveFilePanel("Save", SampleFolder, "Encoded", "gif");

            if (path == "")
            {
                yield break;
            }

            var bytes        = File.ReadAllBytes(LargeSample);
            var gif          = Gif.Decode(bytes);
            var iterator     = gif.EncodeIterator();
            var iteratorSize = gif.GetEncodeIteratorSize();
            var parts        = new List <byte>();
            var index        = 0;      // 0 = first frame, 2 = second frame, penultimate index = GIF header with global color table, last index = GIF trailer

            foreach (var part in iterator)
            {
                if (index == iteratorSize - 1)                 // GIF header should be placed to sequence start!
                {
                    parts.InsertRange(0, part);
                }
                else
                {
                    parts.AddRange(part);
                }

                parts.AddRange(part);
                ProgressFill.fillAmount = ++index / (float)iteratorSize;
                yield return(null);
            }

            bytes = parts.ToArray();
            File.WriteAllBytes(path, bytes);
            Debug.LogFormat("Saved to: {0}", path);
            AnimatedImage.Play(gif);
        }
Exemplo n.º 9
0
    private void OnGUI()
    {
        if (string.IsNullOrEmpty(TextureURL))
        {
            saveURL    = EditorPrefs.GetString(LineTool.GetProjectKey("AnimationSaveURL_FromGif"));
            TextureURL = EditorPrefs.GetString(LineTool.GetProjectKey("AnimationTextureURL_FromGif"));
            BorderURL  = EditorPrefs.GetString(LineTool.GetProjectKey("AnimationBorderURL_FromGif"));
            GifDataURL = EditorPrefs.GetString(LineTool.GetProjectKey("AnimationGifDataURLURL_FromGif"));
        }

        //Select folder contain gif data
        EditorGUILayout.BeginHorizontal();
        GifDataURL = EditorGUILayout.TextField("GifData URL ", GifDataURL);
        if (GUILayout.Button("Browser...", GUILayout.Width(100)))
        {
            GifDataURL = EditorUtility.OpenFolderPanel("Choose GifData File", GifDataURL, "");
            EditorPrefs.SetString(LineTool.GetProjectKey("AnimationGifDataURLURL_FromGif"), GifDataURL);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();

        //gif part

        List <Gif> gifs = new List <Gif>();

        if (!string.IsNullOrEmpty(GifDataURL))
        {
            string[] folder_paths = System.IO.Directory.GetDirectories(GifDataURL);
            foreach (string folder_path in folder_paths)
            {
                //Ignor FX folder cuz this file not right config name
                if (Path.GetFileName(folder_path) == "FX" || Path.GetFileName(folder_path) == "items")
                {
                    continue;
                }

                var files = System.IO.Directory.GetFiles(folder_path, "*.gif");

                foreach (var f in files)
                {
                    var bytes = File.ReadAllBytes(f);
                    var _gif  = Gif.Decode(bytes);
                    _gif.Name = Path.GetFileName(folder_path) + "_" + Path.GetFileName(f);

                    gifs.Add(_gif);
                }
            }

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.TextArea("gif Count: " + gifs.Count);
            GUILayout.EndHorizontal();
        }

        EditorGUILayout.BeginHorizontal();
        BorderURL = EditorGUILayout.TextField("BorderURL ", BorderURL);
        if (GUILayout.Button("Browser...", GUILayout.Width(100)))
        {
            if (string.IsNullOrEmpty(BorderURL))
            {
                BorderURL = EditorUtility.OpenFilePanel("Choose Border File Path", Application.dataPath, "txt");
            }
            else
            {
                BorderURL = EditorUtility.OpenFilePanel("Choose Border File Path", BorderURL, "txt");
            }

            EditorPrefs.SetString(LineTool.GetProjectKey("AnimationBorderURL_FromGif"), BorderURL);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Get Info", GUILayout.Width(100), GUILayout.Height(30)))
        {
            string changeBorderContent = "";
            foreach (var g in gifs)
            {
                var s = "";
                s += g.Name + "|";
                foreach (var f in g.Frames)
                {
                    s += f.Delay + " ";
                }

                changeBorderContent += s + "\n";
            }

            //Save borderData
            if (!string.IsNullOrEmpty(BorderURL) && !string.IsNullOrEmpty(changeBorderContent))
            {
                System.IO.File.WriteAllText(BorderURL, changeBorderContent);
                EditorUtility.DisplayDialog("save gif info", "save Success", "OK");
            }
            else
            {
                EditorUtility.DisplayDialog("Error", "pls select boder file path", "Gotta");
            }
        }
        GUILayout.EndHorizontal();

        //======== end read data from gif

        EditorGUILayout.Space(); EditorGUILayout.Space();
        EditorGUILayout.LabelField("======================================");
        EditorGUILayout.Space(); EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();
        TextureURL = EditorGUILayout.TextField("Textures URL", TextureURL);
        if (GUILayout.Button("Browser...", GUILayout.Width(100)))
        {
            TextureURL = EditorUtility.OpenFolderPanel("Choose Textures File", TextureURL, "");
            EditorPrefs.SetString(LineTool.GetProjectKey("AnimationTextureURL_FromGif"), TextureURL);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space();


        var dataPath = Application.dataPath;
        //New one
        List <string> animatorNames = new List <string>();

        List <string> fxTriggers = new List <string>();
        List <Sprite> sprites    = new List <Sprite>();

        if (!string.IsNullOrEmpty(TextureURL))
        {
            var files = System.IO.Directory.GetFiles(TextureURL, "*.png");
            foreach (var f in files)
            {
                string shortPath = "";
                if (f.Contains(dataPath))
                {
                    shortPath = f.Replace(dataPath, "Assets");
                }
                var objects  = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(shortPath);
                var _sprites = objects.Where(q => q is Sprite).Cast <Sprite>();
                sprites.AddRange(_sprites);
            }
            //Thêm trigger cho anim
            foreach (var s in sprites)
            {
                // "name" + "-" + "trigger"

                var spriteName  = s.name;
                var spriteName1 = spriteName.Replace('-', '_');
                var strlist     = spriteName1.Split('_');

                if (!animatorNames.Contains(strlist[0].ToLower()))
                {
                    animatorNames.Add(strlist[0].ToLower());
                }

                if (strlist.Length >= 2)
                {
                    var triggerName = strlist[1].ToLower();
                    if (!fxTriggers.Contains(triggerName))
                    {
                        fxTriggers.Add(triggerName);
                    }
                }
            }
            EditorGUILayout.TextArea("Animator Count: " + animatorNames.Count);
            EditorGUILayout.TextArea("Sprite Count: " + sprites.Count);
            EditorGUILayout.TextArea("Trigger Count: " + fxTriggers.Count);
        }

        GUILayout.Space(10);
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Generate", GUILayout.Width(100), GUILayout.Height(30)))
        {
            if (string.IsNullOrEmpty(BorderURL))
            {
                EditorUtility.DisplayDialog("Error", "pls select boder file path", "Gotta");
                return;
            }

            EmojiGif2AnimationData borderData = new EmojiGif2AnimationData(BorderURL);

            string existURL = string.IsNullOrEmpty(saveURL) ? Application.dataPath : saveURL;
            saveURL = EditorUtility.OpenFolderPanel("Choose Animator Save Folder", existURL, "");
            //saveURL = EditorUtility.SaveFilePanel("Save Animation File", existURL, "EffectSprite2DName", "controller");
            if (saveURL.Contains(dataPath))
            {
                saveURL = saveURL.Replace(dataPath, "Assets");
            }
            EditorPrefs.SetString(LineTool.GetProjectKey("AnimationSaveURL_FromGif"), saveURL);

            foreach (var a in animatorNames)
            {
                var newSaveURL = saveURL + "/" + a + ".controller";
                var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(newSaveURL);
                if (controller != null)
                {
                    var rootStateMachine = controller.layers[0].stateMachine;
                    var emptyState       = rootStateMachine.AddState("empty");
                    foreach (var trigger in fxTriggers)
                    {
                        var foundSprites = sprites.FindAll(xx => xx.name.ToLower().Contains(trigger) && xx.name.ToLower().Contains(a));
                        if (foundSprites == null || foundSprites.Count == 0)
                        {
                            continue;
                        }
                        string clipName = trigger;

                        //select info from gif
                        string info     = "";
                        var    gif_name = a + "_" + trigger + ".gif";
                        if (borderData.Borders.ContainsKey(gif_name))
                        {
                            info = borderData.Borders[gif_name];
                            Debug.Log("info " + info);

                            controller.AddParameter(trigger, AnimatorControllerParameterType.Trigger);

                            var clip = createAnimationClip(foundSprites, clipName, info, true);

                            AssetDatabase.AddObjectToAsset(clip, controller);
                            var newState = rootStateMachine.AddState(trigger);
                            newState.motion = clip;
                            var trans = rootStateMachine.AddAnyStateTransition(newState);
                            trans.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, trigger);
                            trans.duration    = 0;
                            trans.hasExitTime = false;
                            sprites.RemoveAll(xx => foundSprites.Contains(xx));
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Close();
        }
        GUILayout.EndHorizontal();
    }