/// <summary> /// On Start, creates or sets the health bar up /// </summary> protected virtual void Start() { if (HealthBarType == HealthBarTypes.Prefab) { if (HealthBarPrefab == null) { Debug.LogWarning(this.name + " : the HealthBar has no prefab associated to it, nothing will be displayed."); return; } _progressBar = Instantiate(HealthBarPrefab, transform.position + HealthBarOffset, transform.rotation) as MMProgressBar; _progressBar.transform.SetParent(this.transform); _progressBar.gameObject.name = "HealthBar"; } if (HealthBarType == HealthBarTypes.Drawn) { DrawHealthBar(); UpdateDrawnColors(); } if (!AlwaysVisible) { _progressBar.gameObject.SetActive(false); } if (_progressBar != null) { _progressBar.UpdateBar(100f, 0f, 100f); } }
public virtual void Initialization() { _finalHideStarted = false; if (_progressBar != null) { _progressBar.gameObject.SetActive(AlwaysVisible); return; } if (HealthBarType == HealthBarTypes.Prefab) { if (HealthBarPrefab == null) { Debug.LogWarning(this.name + " : the HealthBar has no prefab associated to it, nothing will be displayed."); return; } _progressBar = Instantiate(HealthBarPrefab, transform.position + HealthBarOffset, transform.rotation) as MMProgressBar; SceneManager.MoveGameObjectToScene(_progressBar.gameObject, this.gameObject.scene); _progressBar.transform.SetParent(this.transform); _progressBar.gameObject.name = "HealthBar"; } if (HealthBarType == HealthBarTypes.Drawn) { DrawHealthBar(); UpdateDrawnColors(); } if (!AlwaysVisible) { _progressBar.gameObject.SetActive(false); } if (_progressBar != null) { _progressBar.SetBar(100f, 0f, 100f); } }
/// <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; }
protected virtual void Initialization() { _progressBar = GetComponent <MMProgressBar> (); }