Пример #1
0
 /// <summary>
 /// Note that if the startWeight is not defined and if this is the first time the tweener starts, last value will be 0! You may want to define the startWeight, if you are tweening to 0.
 /// </summary>
 public void TweenLayerTo(string layerName, float targetWeight, float?startWeight = null, float duration = 1, Tween.EasingMode easing = Tween.EasingMode.Smooth, Action readyCallback = null, Action abortCallback = null)
 {
     TweenLayerTo(Animator.GetLayerIndex(layerName), targetWeight, startWeight, duration, easing, readyCallback, abortCallback);
 }
Пример #2
0
 /// <summary>
 /// Note that if the startWeight is not defined and if this is the first time the tweener starts, last value will be 0! You may want to define the startWeight, if you are tweening to 0.
 /// </summary>
 public void TweenLayerTo(int layerIndex, float targetWeight, float?startWeight = null, float duration = 1, Tween.EasingMode easing = Tween.EasingMode.Smooth, Action readyCallback = null, Action abortCallback = null)
 {
     TweenTo(
         tweenId: layerIndex,
         to: targetWeight,
         from: startWeight,
         duration: duration,
         easing: easing,
         updateCallback: value => Animator.SetLayerWeight(layerIndex, value),
         readyCallback: readyCallback,
         abortCallback: abortCallback);
 }
        private Text SetupFloatingText(bool worldSpace, Vector3 position, string text, Color?color, int size, Vector3?translation, Canvas canvas, Camera camera, float fadeTime, float fullAlphaTime, Tween.EasingMode fadeEasing, Tween.EasingMode translationEasing, Action callback)
        {
            if (canvas == null)
            {
                canvas = GUIManager.DefaultCanvas;
            }
            // Parent
            GameObject parent = GameObject.Find(parentName);

            if (parent == null)
            {
                Debug.LogFormat("[FloatingTextFactory] Could not find a floating text parent with the name {0}. Creating one...", parentName);
                parent = new GameObject(parentName);
                parent.AddComponent <RectTransform>();
                parent.transform.SetParent(canvas.transform, worldPositionStays: false);
            }
            parent.transform.SetAsLastSibling();
            // Instance
            var go    = Instantiate(floatingTextPrefab, parent.transform);
            var rectT = go.transform as RectTransform;

            rectT.SetAsLastSibling();
            rectT.Center();
            if (!worldSpace)
            {
                rectT.position = position;
            }
            // Text
            var textComponent = go.GetOrAddComponent <Text>();

            textComponent.text     = text;
            textComponent.fontSize = size;
            textComponent.color    = color.HasValue ? color.Value : Color.white;
            // Follower
            var follower = worldSpace ? go.GetOrAddComponent <UIFollowWorldTarget>() : go.GetComponent <UIFollowWorldTarget>();

            if (worldSpace)
            {
                follower.enabled     = true;
                follower.targetPos   = position;
                follower.worldCamera = camera == null ? Camera.main : camera;
                follower.updateMode  = followerUpdateMode;
                follower.Init(canvas);
            }
            else if (follower != null)
            {
                follower.enabled = false;
            }
            // Fading
            var fader = go.GetOrAddComponent <UIFader>();

            fader.fullAlphaTime = fullAlphaTime;
            fader.pingPong      = true;
            fader.easing        = fadeEasing;
            Action cb = () => Destroy(go);

            fader.FadeTo(1, fadeTime, cb, cb);
            // Translation
            if (translation.HasValue)
            {
                if (callback == null)
                {
                    callback = () => { };
                }
                var positionTweener = go.GetOrAddComponent <LinearPositionTweener>();
                positionTweener.easing = translationEasing;
                float time = fadeTime * 2 + fullAlphaTime;
                if (follower != null && follower.enabled)
                {
                    positionTweener.TweenPosition(position, translation.Value, translation.Value.magnitude, time, value => follower.targetPos = positionTweener.CurrentPoint, callbackWhenReady: callback);
                }
                else
                {
                    positionTweener.TweenPosition(position, translation.Value, translation.Value.magnitude, time, value => rectT.position = positionTweener.CurrentPoint, callbackWhenReady: callback);
                }
            }
            return(textComponent);
        }
        public Text CreateNotification(string msg, Color?color = null, int size = 20, bool onlyOneInstanceAllowed = false, Canvas canvas = null, float fullAlphaTime = 5, float fadeTime = 0.5f, Tween.EasingMode easing = Tween.EasingMode.Smooth)
        {
            if (canvas == null)
            {
                canvas = GUIManager.DefaultCanvas;
            }
            if (onlyOneInstanceAllowed)
            {
                Text existingNote = notifications.Where(n => n.text.Equals(msg)).FirstOrDefault();
                if (existingNote != null)
                {
                    Debug.Log("NotificationFactory: An existing notification with the same string found.");
                    return(existingNote);
                }
            }
            // Instantiate and setup transforms
            GameObject notificationParent = GameObject.Find(parentName);

            if (notificationParent == null)
            {
                Debug.Log(string.Format("NotificationFactory: Could not find a notification parent with the name {0}. Creating one...", parentName));
                notificationParent = new GameObject(parentName);
                var parentTransform = notificationParent.AddComponent <RectTransform>();
                parentTransform.SetParent(canvas.transform, worldPositionStays: false);
            }
            notificationParent.transform.SetAsLastSibling();
            var noteGO = Instantiate(prefab, notificationParent.transform);
            var rectT  = noteGO.transform as RectTransform;

            rectT.SetAsLastSibling();
            if (!positionsCalculated)
            {
                CalculatePositions(rectT.anchoredPosition);
            }
            if (reservedPositions.Count == allPositions.Count)
            {
                Debug.LogWarning("NotificationFactory: Too many notifications! Please increase the maxNumberOfNotifications or spawn less notifications at a time.");
            }
            // Get the first free position
            var pos = allPositions.FirstOrDefault(p => !reservedPositions.ContainsValue(p));

            rectT.anchoredPosition = pos;
            reservedPositions.Add(rectT, pos);
            // Set colors and effects
            var note = noteGO.GetComponent <Text>();

            color         = color ?? Color.white;
            note.color    = color.Value;
            note.fontSize = size;
            // Set text
            note.text = msg;
            // Set fading and define the destructor as a callback
            var fader = noteGO.GetOrAddComponent <UIFader>();

            fader.fullAlphaTime = fullAlphaTime;
            fader.pingPong      = true;
            fader.easing        = easing;
            fader.fullAlphaTime = fullAlphaTime;
            fader.FadeTo(1, fadeTime, () =>
            {
                notifications.Remove(note);
                reservedPositions.Remove(rectT);
                Destroy(noteGO);
            });
            notifications.Add(note);
            return(note);
        }
 public Text CreateFloatingText(Vector3 worldPosition, string text, Color?color = null, int size = 20, Vector3?translation = null, Canvas canvas = null, Camera camera = null, float fadeTime = 0.5f, float fullAlphaTime = 1.5f, Tween.EasingMode fadeEasing = Tween.EasingMode.Smooth, Tween.EasingMode translationEasing = Tween.EasingMode.Linear, Action callback = null)
 {
     return(SetupFloatingText(true, worldPosition, text, color, size, translation, canvas, camera, fadeTime, fullAlphaTime, fadeEasing, translationEasing, callback));
 }
Пример #6
0
        /// <summary>
        /// Id can be any integer that is not yet used by this Tweener. If a used id is provided, the old tween will be aborted and replaced with the new tween.
        /// From value is optional. If it is not provided, the latest value of the current tween is used as the start value.
        /// </summary>
        public void TweenTo(int tweenId, float to, float?from = null, float duration = 1, bool pingPong = false, float waitBetweenTime = 0, int pingPongCount = 1, Tween.EasingMode easing = Tween.EasingMode.Linear, Action <float> updateCallback = null, Action readyCallback = null, Action abortCallback = null, bool useUnscaledTime = false)
        {
            Tween tween;

            if (tweens.TryGetValue(tweenId, out tween))
            {
                StopTween(tween);
            }
            else
            {
                tween = new Tween();
                tweens.Add(tweenId, tween);
                //Debug.LogFormat("[Tweener] tween added with the id {0}", tweenId);
            }
            from = from ?? tween.currentValue;   // If the start value is not defined, we will use the current value. If the id matches another tween, the value is inherited from it.
            SetupTween(tween, tweenId, from.Value, to, duration, pingPong, waitBetweenTime, pingPongCount, easing, updateCallback, readyCallback, abortCallback, useUnscaledTime);
        }
Пример #7
0
 private void SetupTween(Tween tween, int id, float from, float to, float duration, bool pingPong, float waitBetweenTime, int pingPontCount, Tween.EasingMode easing, Action <float> updateCallback, Action readyCallback, Action abortCallback, bool useUnscaledTime)
 {
     tween.tweenId         = id;
     tween.startTime       = Time.timeSinceLevelLoad;
     tween.duration        = duration;
     tween.endTime         = tween.startTime + tween.duration;
     tween.from            = from;
     tween.to              = to;
     tween.pingPong        = pingPong;
     tween.waitBetweenTime = waitBetweenTime;
     tween.pingPongCount   = pingPontCount;
     tween.easing          = easing;
     tween.useUnscaledTime = useUnscaledTime;
     tween.updateCallback  = updateCallback ?? delegate { };
     tween.readyCallback   = readyCallback ?? delegate { };
     tween.abortCallback   = abortCallback ?? delegate { };
     tween.UpdateValue(tween.from);
     if (gameObject.activeInHierarchy)
     {
         tween.coroutine = StartCoroutine(TweeningRoutine(tween));
     }
     else
     {
         Debug.LogWarning("[Tweener] " + name + " is not active, aborting the tween.");
         StopTween(tween);
     }
 }
 public static Text CreateFloatingText(Vector2 screenPosition, string text, Color?color = null, int size = 20, Vector2?translation = null, Canvas canvas = null, float fadeTime = 0.5f, float fullAlphaTime = 1.5f, Tween.EasingMode fadeEasing = Tween.EasingMode.Smooth, Tween.EasingMode translationEasing = Tween.EasingMode.Linear, Action callback = null)
 {
     return(Instance.FloatingTextFactory.CreateFloatingText(screenPosition, text, color, size, translation, canvas, fadeTime, fullAlphaTime, fadeEasing, translationEasing, callback));
 }
 public static Text CreateNotification(string msg, Color?color = null, int size = 30, bool onlyOneInstance = false, Canvas canvas = null, float fullAlphaTime = 5, float fadeTime = 0.5f, Tween.EasingMode easing = Tween.EasingMode.Smooth)
 {
     return(Instance.NotificationFactory.CreateNotification(msg, color, size, onlyOneInstance, canvas, fullAlphaTime, fadeTime, easing));
 }