Esempio n. 1
0
            public void UpdateSpriteFrame(float time)
            {
                if (!_playing || !_activated || time < _nextUpdateTime)
                {
                    return;
                }
                _currentFrameIndex++;
                var cf = CurrentFrame;

                if (cf != null)
                {
                    StartNewTime(_animation.FrameTime * cf.Length, time);
                    _renderer.sprite = _animation.GetSprite(FrameIndex);
                    return;
                }
                if (!_animation.IsComplete(_currentFrameIndex))
                {
                    return;
                }
                if (_animation.Looping)
                {
                    _currentFrameIndex = 0;
                    StartNewTime(_animation.FrameTime * cf.Length, time);
                    _renderer.sprite = _animation.GetSprite(FrameIndex);
                    return;
                }
                _playing = false;
            }
 public void Play(SpriteAnimation anim)
 {
     if (_state != State.None)
     {
         if (!_animationQueue.Contains(anim))
         {
             _animationQueue.Enqueue(anim);
         }
         return;
     }
     if (_currentAnim == anim && !Finished)
     {
         return;
     }
     _currentAnim     = anim;
     _renderer.sprite = _currentAnim.GetSprite(0);
     if (_currentAnim.NormalMap != null)
     {
         _materialBlock.SetTexture("_BumpMap", _currentAnim.NormalMap);
     }
     _materialBlock.SetTexture("_MainTex", _renderer.sprite.texture);
     _renderer.SetPropertyBlock(_materialBlock);
     _currentFrameIndex    = 0;
     _eventFrame           = null;
     DefaultEventTriggered = Finished = false;
     _state = State.Playing;
     UpdateSpriteFrame();
 }
Esempio n. 3
0
            protected virtual void UpdateSprite()
            {
                if (_directionalAnimation == null)
                {
                    _renderer.SetSprite(
                        _animation.GetSprite(_animator.FrameIndex), _animation.NormalMap, _animation.EmissiveMap,
                        _animation.GetSpriteCollider(_animator.FrameIndex), _animNode.InstancedIndex, _animNode.ForceReverse);
                    return;
                }
                _lastOrientation = _billboard.Orientation;
                var facing = _billboard.Orientation;

                if (_billboard.Facing.RequiresFlipping())
                {
                    facing = _billboard.Orientation.GetFlippedSide();
                    _renderer.Flip(_billboard.Orientation.IsFlipped());
                }
                var animFacing = _directionalAnimation.GetFacing(facing);

                if (animFacing == null || _animator.FrameIndex >= animFacing.FrameIndices.Length)
                {
                    return;
                }
                var idx = animFacing.FrameIndices[_animator.FrameIndex];

                _renderer.SetSprite(_directionalAnimation.GetSprite(idx), _directionalAnimation.NormalMap, _directionalAnimation.EmissiveMap, _directionalAnimation.GetSpriteCollider(idx), _animNode.InstancedIndex, _animNode.ForceReverse);
            }
Esempio n. 4
0
            private void SetSprite()
            {
                var frame = _spriteNode.Frame;

                if (_spriteNode.InstancedIndex >= 0)
                {
                    var data = _simpleRenderer.Sprites[_spriteNode.InstancedIndex];
                    data.Sprite   = _spriteAnimation.GetSprite(frame);
                    data.Emissive = _spriteAnimation.EmissiveMap;
                    data.Normal   = _spriteAnimation.NormalMap;
                    data.Flip     = false;
                }
                else
                {
                    _spriteRenderer.SetSprite(_spriteAnimation.GetSprite(frame), _spriteAnimation.NormalMap, _spriteAnimation.EmissiveMap,
                                              _spriteAnimation.GetSpriteCollider(frame));
                }
            }
        private void UpdateSpriteFrame()
        {
            var sprite = _currentAnim != null?_currentAnim.GetSprite(_currentFrameIndex) : null;

            if (sprite == null)
            {
                return;
            }
            _renderer.sprite = sprite;
        }
Esempio n. 6
0
        public void Set(SpriteAnimation anim)
        {
            _animation = anim;
            var texture = anim.GetSprite(0).texture;

            SetTexture(texture);
            var size = FindSize(texture.width, texture.height, position.width, position.height);

            position = new Rect(position.x, position.y, size.x, size.y);
        }
        private void DrawWithCamera(Camera cam)
        {
            if (!cam)
            {
                return;
            }
            if (_animation == null || _sprite == null)
            {
                return;
            }
            if (_mesh == null)
            {
                _mesh = ProceduralMeshUtility.GenerateQuad(new Vector2(20.48f, 10.24f), new Vector2(0.5f, 0));
            }
            if (_sprite != null)
            {
                DrawSprite();
                return;
            }
            if (!_drawTimer.IsActive)
            {
                _frame++;
                if (_frame >= _animation.Frames.Length)
                {
                    _frame = 0;
                }
                _drawTimer.StartNewTime(_animation.FrameTime * _animation.GetFrame(_frame).Length);
            }
            var sprite   = _animation.GetSprite(_frame);
            var matBlock = new MaterialPropertyBlock();

            matBlock.SetTexture(_shaderPropertyTexture, sprite.texture);
            matBlock.SetTexture(_shaderPropertyNormal, _animation.NormalMap);
            matBlock.SetFloat(_shaderPropertyEmissivePower, _animation.EmissiveMap != null ? 1 : 0);
            if (_animation.EmissiveMap != null)
            {
                matBlock.SetTexture(_shaderPropertyEmissive, _animation.EmissiveMap);
            }
            matBlock.SetColor(_shaderPropertyColor, Color.white);
            matBlock.SetVector(_shaderPropertyUv, SpriteRenderingSystem.GetUv(sprite));
            Graphics.DrawMesh(_mesh, GetMatrix(), _mat, 0, null, 0, matBlock, ShadowCastingMode.On);
        }
Esempio n. 8
0
            private void SetSprite()
            {
                var frame = _spriteNode.Frame;

                if (_spriteNode.InstancedIndex >= 0)
                {
                    _simpleRenderer.SetSprite(
                        _spriteAnimation.GetSprite(frame),
                        _spriteAnimation.NormalMap,
                        _spriteAnimation.EmissiveMap,
                        null,
                        _spriteNode.InstancedIndex,
                        false);
                }
                else
                {
                    _spriteRenderer.SetSprite(_spriteAnimation.GetSprite(frame), _spriteAnimation.NormalMap, _spriteAnimation.EmissiveMap,
                                              _spriteAnimation.GetSpriteCollider(frame));
                }
            }
 public void PlayAnimation(SpriteParticle particle)
 {
     Animation         = particle.Animation;
     CurrentFrameIndex = 0;
     NextUpdateTime    = Animation.FrameTime * CurrentFrame.Length + TimeManager.Time;
     _matBlock.SetColor("_TintColor", Color.white * particle.Glow);
     Renderer.SetPropertyBlock(_matBlock);
     Renderer.color           = particle.Color;
     Renderer.sprite          = Animation.GetSprite(CurrentFrameIndex);
     Renderer.gameObject.name = particle.Animation.name;
 }
 private Sprite GetCurrentSprite()
 {
     if (_nextFrameTime < _currentTime)
     {
         _frame++;
         if (_frame >= _animation.LengthFrames)
         {
             _frame = 0;
         }
         _nextFrameTime = _currentTime + (_animation.FrameTime * _animation.GetFrame(_frame).Length);
     }
     return(_animation.GetSprite(_frame));
 }
Esempio n. 11
0
 public void Setup(SpriteRenderer renderer, SpriteAnimation animation, bool unscaled, float time)
 {
     _renderer  = renderer;
     _animation = animation;
     _unscaled  = unscaled;
     //_renderer.GetPropertyBlock(_materialBlock);
     _renderer.sprite = _animation.GetSprite(0);
     //if (_animation.NormalMap != null) {
     //    _materialBlock.SetTexture("_BumpMap", _animation.NormalMap);
     //}
     //_materialBlock.SetTexture("_MainTex", _renderer.sprite.texture);
     //_renderer.SetPropertyBlock(_materialBlock);
     _currentFrameIndex = 0;
     _playing           = true;
     _activated         = true;
     StartNewTime(_animation.FrameTime * CurrentFrame.Length, time);
     _animationStarted = time;
 }
        private void OnClipChange(bool resetPreview = true)
        {
            if (_clip == null)
            {
                return;
            }
            _frames = new List <AnimFrame>();
            float time = 0;

            for (int i = 0; i < _clip.Frames.Length; i++)
            {
                _frames.Add(new AnimFrame()
                {
                    Sprite          = _clip.GetSprite(i),
                    Event           = _clip.Frames[i].Event,
                    LengthMulti     = _clip.Frames[i].Length,
                    RealLength      = MathEx.Max(GetFrameTime(), SnapTimeToFrameRate(_clip.Frames[i].Length)),
                    EventName       = _clip.Frames[i].EventName,
                    EventPosition   = _clip.Frames[i].EventPosition,
                    EventDataFloat  = _clip.Frames[i].EventDataFloat,
                    EventDataString = _clip.Frames[i].EventDataString,
                    EventDataObject = _clip.Frames[i].EventDataObject,
                });
                _frames.LastElement().Time = time;
                time += _clip.FrameTime * _frames.LastElement().RealLength;
            }
            RecalcFrameLengths();

            _framesReorderableList.list = _frames;
            if (resetPreview)
            {
                _previewResetScale = true;
                //_previewloop = _clip.Looping;
                _animTime       = 0;
                _playing        = _autoPlay;
                _scrollPosition = Vector2.zero;
                //_selectedEvents.Clear();
                _selectedFrames.Clear();
                _previewOffset  = Vector2.zero;
                _timelineOffset = -_timelineOffsetMin;
            }
            Repaint();
        }
        public SpriteAnimationPreview(SpriteAnimation animation)
        {
            _animation = animation;
            var type = Assembly.Load("UnityEditor.dll").GetType("UnityEditor.SpriteInspector");

            for (int i = 0; i < animation.Frames.Length; i++)
            {
                var sprite = animation.GetSprite(i);
                if (sprite == null)
                {
                    continue;
                }
                Editor editor = null;
                Editor.CreateCachedEditor(sprite, type, ref editor);
                if (editor != null)
                {
                    _spriteEditors.Add(editor);
                }
            }
            Speed = 1;
            EditorApplication.update += Update;
        }
Esempio n. 14
0
        protected override void OnGUI()
        {
            DisplayCurrent();
            var    mousePos      = Event.current.mousePosition;
            Rect   currentRect   = new Rect();
            Sprite currentSprite = null;
            SavedSpriteCollider spriteCollider = null;
            var cnt = _animation.LengthSprites;

            Handles.color = Color.red;
            var basePosition = TextureRect.position;
            int spriteCnt    = 0;

            for (int i = 0; i < cnt; i++)
            {
                var sprite = _animation.GetSprite(i);
                if (sprite == null)
                {
                    continue;
                }
                var collider = _animation.GetSpriteCollider(i);
                spriteCnt++;
                var xPos = sprite.rect.position.x * Multiple;
                var yPos = (CurrentTexture.height - sprite.rect.position.y) * Multiple;
                var size = sprite.rect.size * Multiple;
                yPos -= size.y;
                var rect = new Rect(basePosition + new Vector2(xPos, yPos), size);
                //rect.position += (rect.size * 0.5f);
                //EditorGUI.DrawRect(rect, Color.red);
                Handles.color = Color.blue;
                Handles.DrawWireCube(rect.center, rect.size);
                if (collider == null)
                {
                    GUI.Label(rect, "No Collider");
                }
                else
                {
                    //Handles.DrawWireCube(rect.center + (collider.CriticalRect.center * Multiple), collider.CriticalRect.size * Multiple);
                    var critPos = new Vector2(Mathf.Lerp(rect.xMin, rect.xMax, collider.CriticalRect.x),
                                              Mathf.Lerp(rect.yMax, rect.yMin, collider.CriticalRect.y));
                    var critSize = new Vector2(Mathf.Lerp(0, size.x, collider.CriticalRect.size.x),
                                               Mathf.Lerp(0, size.y, collider.CriticalRect.size.y));
                    Handles.color = Color.red;
                    Handles.DrawWireCube(critPos, critSize);
                }
                if (rect.Contains(mousePos))
                {
                    currentRect    = rect;
                    currentSprite  = sprite;
                    spriteCollider = collider;
                }
            }
            Vector3 spriteNormalizedPosition = Vector3.zero;

            if (currentSprite != null)
            {
                spriteNormalizedPosition.x = Mathf.InverseLerp(currentRect.xMin, currentRect.xMax, mousePos.x);
                spriteNormalizedPosition.y = Mathf.InverseLerp(currentRect.yMax, currentRect.yMin, mousePos.y);
                EditorGUILayout.LabelField(currentSprite.name + " " + spriteNormalizedPosition);
            }
            else
            {
                EditorGUILayout.LabelField("No sprite " + spriteCnt + "/" + cnt + " " + mousePos + " / " + (mousePos - TextureRect.position));
            }
            if (currentSprite == null || spriteCollider == null)
            {
                return;
            }
            bool dirty = false;

            if (Event.current.type == EventType.MouseDown)
            {
                spriteCollider.CriticalRect = new Rect(spriteNormalizedPosition, spriteCollider.CriticalRect.size);
                dirty = true;
            }
            if (Event.current.type == EventType.ScrollWheel)
            {
                var sizeChange = -Event.current.delta.y * 0.01f;
                var size       = spriteCollider.CriticalRect.size;
                if (Event.current.control)
                {
                    size.x += sizeChange;
                }
                else if (Event.current.alt)
                {
                    size.y += sizeChange;
                }
                else
                {
                    size += Vector2.one * sizeChange;
                }
                size = new Vector2(Mathf.Clamp01(size.x), Mathf.Clamp01(size.y));
                spriteCollider.CriticalRect = new Rect(spriteCollider.CriticalRect.position, size);
                dirty = true;
            }
            if (dirty)
            {
                EditorUtility.SetDirty(_animation);
                Repaint();
            }
        }
Esempio n. 15
0
 protected virtual void UpdateSprite()
 {
     Renderer.SetSprite(Animation.GetSprite(Animator.FrameIndex), Animation.NormalMap, Animation.EmissiveMap,
                        Animation.GetSpriteCollider(Animator.FrameIndex));
 }