Author Topic: Adding buttons to a scrollview?  (Read 5186 times)

spil778

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Adding buttons to a scrollview?
« on: March 05, 2015, 04:55:35 PM »
Hai guys I just started using NGUI watched the tutorials and thought I was ready to go.
I came into the problem of adding new buttons to the scrollview.

My code for my button looks like this:
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class Test : MonoBehaviour
  5. {
  6.     /// <summary>
  7.     /// Different elements to use
  8.     /// </summary>
  9.     public enum Element
  10.     {
  11.         None = 0,
  12.         Fire = 1,
  13.         Water = 2,
  14.         Earth = 4,
  15.         Wind = 8
  16.     }
  17.  
  18.     /// <summary>
  19.     /// Effects
  20.     /// </summary>
  21.     public enum Effect
  22.     {
  23.         None,
  24.         Scorch,
  25.         Steam,
  26.         Lava,
  27.         Gas
  28.     }
  29.  
  30.  
  31.     /// <summary>
  32.     /// Elemet to Effect lookup table
  33.     /// </summary>
  34.     private readonly Dictionary<int, Effect> _lookUpTable = new Dictionary<int, Effect>()
  35.     {
  36.         { (int)Element.Fire, Effect.Lava },
  37.         { (int)( Element.Fire | Element.Water), Effect.Steam },
  38.     };
  39.  
  40.     public Effect YingYangMixing( Element element )
  41.     {
  42.         Effect ret = Effect.None;
  43.         if( !_lookUpTable.TryGetValue( (int)element, out ret ) )
  44.             Debug.LogError( "Element does not exist in Lookup table!" );
  45.  
  46.         return ret;
  47.     }
  48.  
  49.     private void Update()
  50.     {
  51.         // usage
  52.         Effect effect = YingYangMixing( Element.Fire | Element.Water);
  53.         if( effect == Effect.Steam )
  54.             DoSomething();
  55.     }
  56.  
  57. }
  58.  

What I want to do is if the effect var does not exist in a list of known combinations then add a button with that as a label. My second problem is that I have to get that bottons name somehow and change what that button to. I find this hard to explain what I kinda want it to do is:
  1. YingYangMixing(labelFirst.text | labelSecond.text)
  2. if(_knownCombination.Find(effect) != true){
  3.      Add.NewButton(Name=Effect, last position of button+offset);
  4. }
  5.  

And the button have to do this:
  1. OnClick(){
  2.   //labelFirst if its in first scrollview
  3.   //labelSecond if its in second scrollview
  4.   labelFirst/labelSecond.text = nameOfEffect
  5. }
  6.  

Does this make any sense?
« Last Edit: March 05, 2015, 05:08:33 PM by spil778 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding buttons to a scrollview?
« Reply #1 on: March 05, 2015, 10:21:03 PM »
I'm not sure what the first question is... Your code doesn't do any object instantiation via NGUITools.AddChild or otherwise, so where do you even try to add the buttons?

If you want to dynamically instantiate something then change its content, create that element first -- in your case it would be the button. Attach a script to it that will have public references to the elements you need -- sprites and labels for example. Then save this object as a prefab.

After instantiating (use NGUITools.AddChild(parent, prefab)), GetComponent<> of your custom class from the first part, then set its values.

spil778

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Adding buttons to a scrollview?
« Reply #2 on: March 07, 2015, 10:48:42 AM »
I dont really know where to begin thats my point. Well sorry let me give you some more info my gui setup looks like this:



The Onclick for my button looks like this:


The change label script is just this (I will add the new elements onto it)
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ChangeLabels : MonoBehaviour {
  5.         public UILabel labelRef;
  6.  
  7.         public string fire{
  8.                 get{return "Fire";}
  9.         }
  10.         public string water{
  11.                 get{return "Water";}
  12.         }
  13.         public string earth{
  14.                 get{return "Earth";}
  15.         }
  16.         public string wind{
  17.                 get{return "Wind";}
  18.         }
  19.        
  20.         public void ChangeLabel (string name){
  21.                 GetComponent<UILabel> ().text = name;
  22.         }
  23. }
  24.  

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System;
  5. using System.IO;
  6. using System.Xml;
  7.  
  8. public class addmagic : MonoBehaviour
  9. {
  10.         public GameObject frstLabel;
  11.         public GameObject scdLabel;
  12.         public GameObject mixLabel;
  13.         public GameObject name;
  14.         public string ele1;
  15.         public string ele2;
  16.        
  17.         public UILabel label1;
  18.         public UILabel label2;
  19.         public UILabel mixlabelRef;
  20.         public UILabel nameRef;
  21.  
  22.         private int temp;
  23.         private string magicPath;
  24.        
  25.         List<KnownElements> known = new List<KnownElements>();
  26.        
  27.         public Element fire{
  28.                 get{return Element.Fire;}
  29.         }
  30.         public Element water{
  31.                 get{return Element.Fire;}
  32.         }
  33.         public Element earth{
  34.                 get{return Element.Earth;}
  35.         }
  36.         public Element wind{
  37.                 get{return Element.Wind;}
  38.         }
  39.         /// <summary>
  40.         /// Different elements to use
  41.         /// </summary>
  42.         public enum Element
  43.         {
  44.                 None = 1<<0,
  45.                 Fire = 1<<1,
  46.                 Water = 1<<2,
  47.                 Earth = 1<<3,
  48.                 Wind = 1<<4,
  49.                 Scorch = 1<<5,
  50.                 Steam = 1<<6,
  51.                 Lava = 1<<7,
  52.                 Gas = 1<<8,
  53.                 Fluid = 1<<9,
  54.                 Tsunami = 1<<10,
  55.                 Ice = 1<<11,
  56.                 Magma = 1<<12,
  57.                 Metal = 1<<13,
  58.                 Wood = 1<<14,
  59.                 Sand = 1<<15,
  60.                 Lightning = 1<<16,
  61.                 Stream = 1<<17,
  62.                 Tornado = 1<<18,
  63.                 Poison = 1<<19,
  64.         }
  65.        
  66.         /// <summary>
  67.         /// Effects
  68.         /// </summary>
  69.         public enum Effect
  70.         {
  71.                 None,
  72.                 Fire,
  73.                 Water,
  74.                 Earth,
  75.                 Wind,
  76.                 Scorch,
  77.                 Steam,
  78.                 Lava,
  79.                 Gas,
  80.                 Fluid,
  81.                 Tsunami,
  82.                 Ice,
  83.                 Magma,
  84.                 Metal,
  85.                 Wood,
  86.                 Sand,
  87.                 Lightning,
  88.                 Tornado,
  89.                 Stream,
  90.                 Poison,
  91.         }
  92.        
  93.         void Awake() {
  94.                 known.Add (new KnownElements("Fire"));
  95.                 known.Add (new KnownElements("Water"));
  96.                 known.Add (new KnownElements("Wind"));
  97.                 known.Add (new KnownElements("Earth"));
  98.  
  99.                 frstLabel = GameObject.FindWithTag ("FrstEleLabel");
  100.                 scdLabel = GameObject.FindWithTag ("ScdEleLabel");
  101.                 mixLabel = GameObject.FindWithTag ("mixlabel");
  102.                 name = GameObject.FindWithTag ("name");
  103.  
  104.                
  105.                 label1 = frstLabel.GetComponent<UILabel> ();
  106.                 label2 = scdLabel.GetComponent<UILabel> ();
  107.                 mixlabelRef = mixLabel.GetComponent<UILabel> ();
  108.                 nameRef = name.GetComponent<UILabel> ();
  109.  
  110.                 label1.text = "None";
  111.                 label2.text = "None";
  112.                 magicPath = Application.dataPath.ToString() + "/Magic";
  113.         }
  114.         /// <summary>
  115.         /// Elemet to Effect lookup table
  116.         /// </summary>
  117.         private readonly Dictionary<int, Effect> _lookUpTable = new Dictionary<int, Effect>()
  118.         {
  119.                 { (int)( Element.Fire | Element.Fire), Effect.Magma },
  120.                 { (int)(Element.Fire | Element.Water), Effect.Steam },
  121.                 { (int)(Element.Fire | Element.Earth), Effect.Lava },
  122.                 { (int)(Element.Fire | Element.Wind), Effect.Gas },
  123.                 { (int)(Element.Water | Element.Water), Effect.Fluid},
  124.                 { (int)(Element.Water | Element.Earth), Effect.Tsunami },
  125.                 { (int)(Element.Water | Element.Wind), Effect.Ice },
  126.                 { (int)(Element.Wind | Element.Earth), Effect.Sand },
  127.                 { (int)(Element.Wind | Element.Wind), Effect.Tornado },
  128.                 { (int)(Element.Earth | Element.Earth), Effect.Wood },
  129.                
  130.         };
  131.        
  132.         public void YingYangMixing()
  133.         {
  134.        
  135.                 Element element = (Element)System.Enum.Parse( typeof( Element ), label1.text);
  136.                 Element element2 = (Element)System.Enum.Parse( typeof( Element ), label2.text);
  137.  
  138.  
  139.                 //ADD NEW BUTTON FUNCTION HERE
  140.  
  141.                 if (mixlabelRef.text == "Mixing is: on") {
  142.                         Effect ret = Effect.None;
  143.                         if (!_lookUpTable.TryGetValue ((int)element | (int)element2, out ret)) {
  144.                                 Debug.LogError ("Element does not exist in Lookup table!");
  145.                         }
  146.                         else {
  147.                                 if (System.IO.Directory.Exists (magicPath) != true) {
  148.                                         Debug.Log("Didn't find it so creating it now");
  149.                                         System.IO.Directory.CreateDirectory (magicPath);
  150.                                 }
  151.                                
  152.                         }
  153.                 }
  154.         }
  155. }

I just finished my school exam and I am a bit rusten in C# and unity so is there anyone who can clearify:
the problem is that I have my list called "known" if the created effect is not on that list it should make a new button

spil778

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Adding buttons to a scrollview?
« Reply #3 on: March 11, 2015, 03:42:47 PM »
None? :c

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding buttons to a scrollview?
« Reply #4 on: March 12, 2015, 08:21:21 PM »
"I dont really know where to begin" isn't a question I can answer. This forum is for NGUI-related support. I can answer questions related to NGUI usage, but I can't tell you how to get started with some school project of yours. Generic questions are better suited for Unity forums. Your question needs to be pretty specific for me to be able to answer it. Adding buttons to something is as trivial as instantiating game objects. Create a prefab out of what your button should be like, then use NGUITools.AddChild(parent, prefab) to create new instances of it.