Пример #1
0
        /// <summary>
        /// Draws the health bar.
        /// </summary>
        protected virtual void DrawHealthBar()
        {
            GameObject newGameObject = new GameObject();

            SceneManager.MoveGameObjectToScene(newGameObject, this.gameObject.scene);
            newGameObject.name = "HealthBar|" + this.gameObject.name;

            if (NestDrawnHealthBar)
            {
                newGameObject.transform.SetParent(this.transform);
            }

            _progressBar = newGameObject.AddComponent <MMProgressBar>();

            _followTransform                     = newGameObject.AddComponent <MMFollowTarget>();
            _followTransform.Offset              = HealthBarOffset;
            _followTransform.Target              = this.transform;
            _followTransform.FollowRotation      = false;
            _followTransform.InterpolatePosition = false;
            _followTransform.InterpolateRotation = false;
            _followTransform.UpdateMode          = FollowTargetMode;

            Canvas newCanvas = newGameObject.AddComponent <Canvas>();

            newCanvas.renderMode           = RenderMode.WorldSpace;
            newCanvas.transform.localScale = Vector3.one;
            newCanvas.GetComponent <RectTransform>().sizeDelta = Size;
            if (!string.IsNullOrEmpty(SortingLayerName))
            {
                newCanvas.sortingLayerName = SortingLayerName;
            }

            GameObject container = new GameObject();

            container.transform.SetParent(newGameObject.transform);
            container.name = "MMProgressBarContainer";
            container.transform.localScale = Vector3.one;

            GameObject borderImageGameObject = new GameObject();

            borderImageGameObject.transform.SetParent(container.transform);
            borderImageGameObject.name = "HealthBar Border";
            _borderImage = borderImageGameObject.AddComponent <Image>();
            _borderImage.transform.position   = Vector3.zero;
            _borderImage.transform.localScale = Vector3.one;
            _borderImage.GetComponent <RectTransform>().sizeDelta        = Size;
            _borderImage.GetComponent <RectTransform>().anchoredPosition = Vector3.zero;

            GameObject bgImageGameObject = new GameObject();

            bgImageGameObject.transform.SetParent(container.transform);
            bgImageGameObject.name = "HealthBar Background";
            _backgroundImage       = bgImageGameObject.AddComponent <Image>();
            _backgroundImage.transform.position   = Vector3.zero;
            _backgroundImage.transform.localScale = Vector3.one;
            _backgroundImage.GetComponent <RectTransform>().sizeDelta        = Size - BackgroundPadding * 2;
            _backgroundImage.GetComponent <RectTransform>().anchoredPosition = -_backgroundImage.GetComponent <RectTransform>().sizeDelta / 2;
            _backgroundImage.GetComponent <RectTransform>().pivot            = Vector2.zero;

            GameObject delayedImageGameObject = new GameObject();

            delayedImageGameObject.transform.SetParent(container.transform);
            delayedImageGameObject.name = "HealthBar Delayed Foreground";
            _delayedImage = delayedImageGameObject.AddComponent <Image>();
            _delayedImage.transform.position   = Vector3.zero;
            _delayedImage.transform.localScale = Vector3.one;
            _delayedImage.GetComponent <RectTransform>().sizeDelta        = Size - BackgroundPadding * 2;
            _delayedImage.GetComponent <RectTransform>().anchoredPosition = -_delayedImage.GetComponent <RectTransform>().sizeDelta / 2;
            _delayedImage.GetComponent <RectTransform>().pivot            = Vector2.zero;

            GameObject frontImageGameObject = new GameObject();

            frontImageGameObject.transform.SetParent(container.transform);
            frontImageGameObject.name             = "HealthBar Foreground";
            _foregroundImage                      = frontImageGameObject.AddComponent <Image>();
            _foregroundImage.transform.position   = Vector3.zero;
            _foregroundImage.transform.localScale = Vector3.one;
            _foregroundImage.color                = ForegroundColor.Evaluate(1);
            _foregroundImage.GetComponent <RectTransform>().sizeDelta        = Size - BackgroundPadding * 2;
            _foregroundImage.GetComponent <RectTransform>().anchoredPosition = -_foregroundImage.GetComponent <RectTransform>().sizeDelta / 2;
            _foregroundImage.GetComponent <RectTransform>().pivot            = Vector2.zero;

            _progressBar.LerpDecreasingDelayedBar         = LerpDelayedBar;
            _progressBar.LerpForegroundBar                = LerpFrontBar;
            _progressBar.LerpDecreasingDelayedBarSpeed    = LerpDelayedBarSpeed;
            _progressBar.LerpForegroundBarSpeedIncreasing = LerpFrontBarSpeed;
            _progressBar.ForegroundBar           = _foregroundImage.transform;
            _progressBar.DelayedBarDecreasing    = _delayedImage.transform;
            _progressBar.DecreasingDelay         = Delay;
            _progressBar.BumpScaleOnChange       = BumpScaleOnChange;
            _progressBar.BumpDuration            = BumpDuration;
            _progressBar.BumpScaleAnimationCurve = BumpAnimationCurve;
            _progressBar.TimeScale = (TimeScale == TimeScales.Time) ? MMProgressBar.TimeScales.Time : MMProgressBar.TimeScales.UnscaledTime;
            container.transform.localEulerAngles = InitialRotationAngles;
            _progressBar.Initialization();
        }
        /// <summary>
        /// Draws the health bar.
        /// </summary>
        protected virtual void DrawHealthBar()
        {
            GameObject newGameObject = new GameObject();

            newGameObject.name = "HealthBar|" + this.gameObject.name;

            _progressBar = newGameObject.AddComponent <MMProgressBar>();

            _followTransform        = newGameObject.AddComponent <MMFollowTarget>();
            _followTransform.Offset = HealthBarOffset;
            _followTransform.Target = this.transform;
            _followTransform.InterpolatePosition = false;
            _followTransform.InterpolateRotation = false;
            _followTransform.UpdateMode          = FollowTargetMode;

            Canvas newCanvas = newGameObject.AddComponent <Canvas>();

            newCanvas.renderMode           = RenderMode.WorldSpace;
            newCanvas.transform.localScale = Vector3.one;
            newCanvas.GetComponent <RectTransform>().sizeDelta = Size;
            if (SortingLayerName != "")
            {
                newCanvas.sortingLayerName = SortingLayerName;
            }

            GameObject borderImageGameObject = new GameObject();

            borderImageGameObject.transform.SetParent(newGameObject.transform);
            borderImageGameObject.name = "HealthBar Border";
            _borderImage = borderImageGameObject.AddComponent <Image>();
            _borderImage.transform.position   = Vector3.zero;
            _borderImage.transform.localScale = Vector3.one;
            _borderImage.GetComponent <RectTransform>().sizeDelta        = Size;
            _borderImage.GetComponent <RectTransform>().anchoredPosition = Vector3.zero;

            GameObject bgImageGameObject = new GameObject();

            bgImageGameObject.transform.SetParent(newGameObject.transform);
            bgImageGameObject.name = "HealthBar Background";
            _backgroundImage       = bgImageGameObject.AddComponent <Image>();
            _backgroundImage.transform.position   = Vector3.zero;
            _backgroundImage.transform.localScale = Vector3.one;
            _backgroundImage.GetComponent <RectTransform>().sizeDelta        = Size - BackgroundPadding * 2;
            _backgroundImage.GetComponent <RectTransform>().anchoredPosition = -_backgroundImage.GetComponent <RectTransform>().sizeDelta / 2;
            _backgroundImage.GetComponent <RectTransform>().pivot            = Vector2.zero;

            GameObject delayedImageGameObject = new GameObject();

            delayedImageGameObject.transform.SetParent(newGameObject.transform);
            delayedImageGameObject.name = "HealthBar Delayed Foreground";
            _delayedImage = delayedImageGameObject.AddComponent <Image>();
            _delayedImage.transform.position   = Vector3.zero;
            _delayedImage.transform.localScale = Vector3.one;
            _delayedImage.GetComponent <RectTransform>().sizeDelta        = Size - BackgroundPadding * 2;
            _delayedImage.GetComponent <RectTransform>().anchoredPosition = -_delayedImage.GetComponent <RectTransform>().sizeDelta / 2;
            _delayedImage.GetComponent <RectTransform>().pivot            = Vector2.zero;

            GameObject frontImageGameObject = new GameObject();

            frontImageGameObject.transform.SetParent(newGameObject.transform);
            frontImageGameObject.name             = "HealthBar Foreground";
            _foregroundImage                      = frontImageGameObject.AddComponent <Image>();
            _foregroundImage.transform.position   = Vector3.zero;
            _foregroundImage.transform.localScale = Vector3.one;
            _foregroundImage.GetComponent <RectTransform>().sizeDelta        = Size - BackgroundPadding * 2;
            _foregroundImage.GetComponent <RectTransform>().anchoredPosition = -_foregroundImage.GetComponent <RectTransform>().sizeDelta / 2;
            _foregroundImage.GetComponent <RectTransform>().pivot            = Vector2.zero;

            _progressBar.LerpDelayedBar         = LerpDelayedBar;
            _progressBar.LerpForegroundBar      = LerpFrontBar;
            _progressBar.LerpDelayedBarSpeed    = LerpDelayedBarSpeed;
            _progressBar.LerpForegroundBarSpeed = LerpFrontBarSpeed;
            _progressBar.ForegroundBar          = _foregroundImage.transform;
            _progressBar.DelayedBar             = _delayedImage.transform;
            _progressBar.Delay              = Delay;
            _progressBar.BumpScaleOnChange  = BumpScaleOnChange;
            _progressBar.BumpDuration       = BumpDuration;
            _progressBar.BumpAnimationCurve = BumpAnimationCurve;
        }