Search Unity

How to adapt C# script for UGUI Health Globe

Discussion in 'Scripting' started by jessejarvis, May 30, 2016.

  1. jessejarvis

    jessejarvis

    Joined:
    Aug 9, 2013
    Posts:
    303
    Hello all! I have right here my so far adapted code for a script (came with a GUI I bought) that I am trying to get to work for the UI.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.UI.Tweens;
    5. using System.Collections;
    6.  
    7. public class FillHP : MonoBehaviour {
    8.    
    9.     public Image imageComponent;
    10.     public float Duration = 5f;
    11.     public TweenEasing Easing = TweenEasing.InOutQuint;
    12.     public float Health = 0f;
    13.     public float MaxHealth = 0f;
    14.    
    15.     // Tween controls
    16.     [NonSerialized] private readonly TweenRunner<FloatTween> m_FloatTweenRunner;
    17.    
    18.     // Called by Unity prior to deserialization,
    19.     // should not be called by users
    20.     protected FillHP()
    21.     {
    22.         if (this.m_FloatTweenRunner == null)
    23.             this.m_FloatTweenRunner = new TweenRunner<FloatTween>();
    24.        
    25.         this.m_FloatTweenRunner.Init(this);
    26.     }
    27.    
    28.     protected void Start()
    29.     {
    30.         if (this.imageComponent == null)
    31.             return;
    32.        
    33.         this.StartTween(0f, (this.imageComponent.fillAmount * this.Duration));
    34.     }
    35.    
    36.     protected void SetFillAmount(float amount)
    37.     {
    38.         if (this.imageComponent == null)
    39.             return;
    40.        
    41.         this.imageComponent.fillAmount = amount;
    42.     }
    43.    
    44.     protected void OnTweenFinished()
    45.     {
    46.         if (this.imageComponent == null)
    47.             return;
    48.        
    49.         this.StartTween((this.imageComponent.fillAmount == 0f ? 1f : 0f), this.Duration);
    50.     }
    51.    
    52.     protected void StartTween(float targetFloat, float duration)
    53.     {
    54.         if (this.imageComponent == null)
    55.             return;
    56.        
    57.         var floatTween = new FloatTween { duration = duration, startFloat = this.imageComponent.fillAmount, targetFloat = targetFloat };
    58.         floatTween.AddOnChangedCallback(SetFillAmount);
    59.         floatTween.AddOnFinishCallback(OnTweenFinished);
    60.         floatTween.ignoreTimeScale = true;
    61.         floatTween.easing = this.Easing;
    62.         this.m_FloatTweenRunner.StartTween(floatTween);
    63.     }
    64. }
    Essentially all I got to so far as to add public variables Health and MaxHealth. I'm actually using Playmaker, so Health and MaxHealth are being filled every frame.

    I tried changing 0f ? 1f : 0f to Health ? MaxHealth : Health but that never worked. It was filled the entire time.

    Thanks in advance, going to be tinkering around with this in the meantime :)

    Sorry if this is such a simple answer, never dealth with "Filling" and image before or tweening.
     
  2. jessejarvis

    jessejarvis

    Joined:
    Aug 9, 2013
    Posts:
    303
    I'd like if this could get deleted, but yeah it was super simple all I ended up doing was making a completely new script and doing this:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class FillHP : MonoBehaviour {
    7.    
    8.     public Image imageComponent;
    9.     public float Health = 0f;
    10.     public float MaxHealth = 0f;
    11.  
    12.     void Update() {
    13.         float calc_Health = Health / MaxHealth;
    14.         imageComponent.fillAmount = calc_Health;
    15.     }
    16. }
    So essentially once I learn more Playermaker I'll probably just replace this. Since I don't think putting that in update is efficient.