- Home /
How to find the index of the clicked UI button?
Hi,
I want help with picking and adding materials to the Object dynamically
Here is my process:
Will click on a GameObject, The variants of the gameObject will appear in a UI called Customize. The UI will have Variant buttons and clicking on a particular Variant button, the material associated with that variant has to be applied to GameObject. The GameObject will have a script and here is what I'll define the variants:
and here is how the UI is
Can someone please help me with finding the associated variant materials on click and adding them to the game object?
It is also great if i can just know the index of element and variant
here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CSV_ObjectCustomizer1 : MonoBehaviour {
public List<ProductsYo> Elements = new List<ProductsYo> ();
private GameObject MainCanvas = null;
private GameObject CSVItems = null;
void OnMouseDown(){
if (Input.GetMouseButtonDown (0)) {
OnPupulate ();
}
}
void OnPupulate(){
MainCanvas = GameObject.Find ("CSV_Content");
if (MainCanvas.transform.childCount>0) {
ClearChildren ();
}
for (int i = 0; i < Elements.Count; i++) {
GameObject CSV_ItemPanel = Instantiate(Resources.Load ("CSV_ItemPanel") as GameObject);
if (CSV_ItemPanel.GetComponentInChildren<Text> ().transform.name == "ItemName") {
CSV_ItemPanel.GetComponentInChildren<Text> ().text = Elements [i].ItemName;
}
CSV_ItemPanel.transform.SetParent (MainCanvas.transform, false);
for (int j = 0; j < Elements [i].Variants.Count; j++) {
Debug.Log (Elements[i].ItemName+ Elements [i].Variants [j].VariantName);
GameObject CSV_ItemButton = Instantiate (Resources.Load ("CSV_ItemButton") as GameObject);
CSV_ItemButton.transform.Find("CSV_VariantName").gameObject.GetComponent<Text>().text = Elements[i].Variants[j].VariantName;
CSV_ItemButton.transform.Find ("CSV_VariantIcon").gameObject.GetComponent<Image> ().sprite = Elements [i].Variants [j].VariantIcon;
CSV_ItemButton.transform.SetParent (CSV_ItemPanel.transform.Find("CSV_Items"), false);
//Here is what i'm stuck:
CSV_ItemButton.GetComponent<Button> ().onClick.AddListener (() => OnVariantButtonClicked(i,j));
}
}
}
void ClearChildren(){
MainCanvas = GameObject.Find ("CSV_Content");
Transform[] Children = MainCanvas.GetComponentsInChildren<RectTransform> ();
while (MainCanvas.transform.childCount > 0)
{
Transform child = MainCanvas.transform.GetChild(0);
Destroy(child.gameObject);
}
}
//Here is what i'm stuck
void OnVariantButtonClicked(int ElementIndex, int VariantIndex){
Debug.Log (ElementIndex + "," + VariantIndex);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
[Serializable]
public struct ElementInfo{
public string VariantName;
public Sprite VariantIcon;
public Material VariantMaterial;
}
[Serializable]
public class ProductsYo{
public string ItemName;
public List<ElementInfo> Variants = new List<ElementInfo>();
}
Answer by sushillakra21 · Aug 29, 2018 at 09:06 AM
This is how I do it in C#, I write a handler for gameObject, handler implements interface such as OnTouch() or something, I have only one raycaster, which also has a hashtable, which is used to find the handler, you can write register/deregister methods in raycaster, hashtable maps GameObject InstanceID to Handler. now raycaster finds that a GameObject has been hit, it looks into hashtable if there's hadler for this game object if there is then calls OnTouch on handler.
Hope this helps.
Your answer
Follow this Question
Related Questions
Change Object Material With a button C# 1 Answer
GUI.Button not working fine on iphone when i press one button the other do not pressed 1 Answer
Gathering GameObjects and then creating a button for each GameObject?? 2 Answers
Button animation state stays Pressed or Highlighted after disabling GameObject 0 Answers
How to put an object as a button or a button that appears as a tracking model 1 Answer