Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - rahares

Pages: [1]
1
This might help, its how I update a UISlider as a healthbar

using UnityEngine;
using System.Collections;

public class VitalBar : MonoBehaviour {

   public UILabel healthBarLabel;

   private UISlider vitalSlider;
   private float _maxWidth;
   private bool _displayText = false;

   void Awake (){
      vitalSlider = GetComponent<UISlider> ();

      if (vitalSlider == null) {
         Debug.LogError("Could not find the UISlider Component!");
         return;
            }
      _maxWidth = vitalSlider.foregroundWidget.localSize.x;
      _displayText =true;
   }


   // Use this for initialization
   void Start () {
      UpdateDisplay (- 5f, "100/200");
   
   }
   //vitalSlider.sliderValue = health/healthBarLabel;
   public void UpdateDisplay(float x ){
      if (x > 0)
         x = 0;
      else if (x > 1)
         x = 1;
      vitalSlider.value = 50;
      DisplayText = false;
   }

   public void UpdateDisplay(float x, string str ){
      UpdateDisplay (x);
      if (str != "") healthBarLabel.text = str;
   }

   public bool DisplayText {
      get { return _displayText;}
      set { _displayText = value;
          if (!_displayText)
            healthBarLabel.text = ""; }
   }
}

2
NGUI 3 Support / Re: Add new labels to scrollable view at runtime
« on: May 18, 2014, 05:21:39 PM »
I use this to create the objects on scrollview grid ....Should be able to create your label as a prefab and do the same

GameObject addnewitem = NGUITools.AddChild(YOURGRID.gameObject, YOURLABELPREFAB); // creates a new Grid Item
            UILabel[] labels = addnewItem.GetComponentsInChildren<UILabel>();
            foreach (UILabel label in labels) { // Checks each label and assigns the values
               switch (label.name)   {
               case "MYLABELNAME":
                  label.text = STUFFTOADD;
               break;

then call YOURGRID.Reposition();

i have multiple labels and not just one so theres probally a better way for that then using a switch but the basic idea is the same i would think

3
Alright with everything you have said I finally understand how to make this work.

Thank you very much.  Sorry if my questions sounded stupid but as you've pointed out I'm noob.

For anyone doing the same its actually simple HA!  Just like he said, but I dumbed it way down for us noobs! HA HA!

-----------------------------------------------------------------------------------------------------------
Usage: Add this script to the Prefab that will get added to your UIGrid/UIScrollView.  Whenever this object is clicked it will set the values in the other script that needs to access to these values.

Script: ObjectOnClick.cs
----------------------------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections;

public class ObjectOnClick : MonoBehaviour {

   public UILabel theLabel1; // assign this from the inspector using the UILabel in the prefab
   public UILabel theLabel2; // repeat for however many you need to access
       
        // This runs whenever the object gets clicked on
    void OnClick () {
                // This will access whatever the gameobject is that has the script
                // It will then assign the values from this object to those of the other script
                GameObject target = GameObject.Find("GameObjectName which has the script/values to access");
                SCRIPTNAME script = target.GetComponent<SCRIPTNAME>();
                script.MYVALUE = theLabel1.text;
                script.MYVALUE2 = theLabel2.text;
   }
}

Pages: [1]