- Home /
The question is answered, right answer was accepted
How to get this string to change values for each button at runtime?
Hello there! Im a llitle bit stucked with this piece of code, actually i'm programming an OBJ Viewer where you can load elements dynamically. The problem comes when i need to load the OBJ, because i get an array of the OBJ PATHS (it has to be an array, cannot be a list) but i cant give it a value accord to the button they are implemented in.
This is my code for instantiate the buttons (is on a Scene manager where I run all the code)
void Start()
{
i = 0;
x = 0;
exportPath = Application.dataPath + "/StreamingAssets";
objPaths = Directory.GetFiles(exportPath, "*.obj");
UnityEngine.Object[] allMeshFilters = UnityEngine.Object.FindObjectsOfType<MeshFilter>();
foreach (UnityEngine.Object o in allMeshFilters)
{
MeshFilter meshFilter = (MeshFilter)o; //force o to get typecast to a MeshFilter
if (meshFilter != null)
{
initialTriCount += meshFilter.mesh.triangles.Length / 3;
}
}
initialPos = NVRPlayer.transform.position;
foreach (string objPath in objPaths)
{
GameObject buttonClone = Instantiate(buttonChoose);
RectTransform rtClone = buttonClone.GetComponent<RectTransform>();
buttonClone.transform.SetParent(scrollContent);
buttonClone.GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
buttonClone.GetComponent<RectTransform>().localScale = new Vector3(0.3030485f, 0.2993734f, 0.4927109f);
buttonClone.GetComponent<RectTransform>().localRotation = new Quaternion(0, 0, 0, 0);
buttonNumber.Add(buttonClone);
Debug.Log(buttonNumber.Count);
buttonClone.GetComponent<Text>().text = Path.GetFileName(objPaths[i++]);
//objChosen = objPaths[x++]; This is my problem right now
Debug.Log(objChosen);
buttonClone.GetComponent<Button>().onClick.AddListener(LocalObj);
}
As you see I instantiate a copy of a prefab button where i change the text to put the obj name. My problem comes when i need to instantiate my selected obj:
public void LocalObj()
{
Destroy(objInstance);
triCount = 0;
objInstance = OBJLoader.LoadOBJFile(objChosen);
OBJLoader.LoadMTLFile(objChosen);
chooseMenu.SetActive(false);
UnityEngine.Object[] allMeshFilters = UnityEngine.Object.FindObjectsOfType<MeshFilter>();
foreach (UnityEngine.Object o in allMeshFilters)
{
MeshFilter meshFilter = (MeshFilter)o; //force o to get typecast to a MeshFilter
if (meshFilter != null)
{
triCount += meshFilter.mesh.triangles.Length/3;
}
}
Debug.Log((triCount - initialTriCount) + " tris");
//objInstance.transform.localScale = new Vector3(1F, 1F, 1);
objInstance.transform.localPosition = new Vector3(0f, 0.5f, 7.22f);
objInstance.transform.localRotation = Quaternion.Euler(0f, 90f, 0f);
objSize = new System.IO.FileInfo(objChosen).Length;
CalculateLocalBounds();
if (objSize > 1000000)
{
objSize = new System.IO.FileInfo(objChosen).Length / 10000000;
Debug.Log(objSize + " MB");
ObjInfo.text = "El objeto pesa " + objSize + " MB, y tiene " + (triCount - initialTriCount) + " triángulos";
}
else
{
objSize = new System.IO.FileInfo(objChosen).Length / 1000;
Debug.Log(objSize + " kB");
ObjInfo.text = "El objeto pesa " + objSize + " KB, tiene " + (triCount - initialTriCount) + " triángulos y su escala es " + bounds.extents;
}
}
There, objChosen shall be selected using UI button at runtime, but don't know how to make that first button's objChosen options is related to the first obj in objPaths[]
Any suggestions! Thanks!
Answer by andrew-lukasik · Jun 06, 2018 at 11:42 AM
string path = objPaths[i++];
buttonClone.GetComponent<Text>().text = Path.GetFileName(path);
buttonClone.GetComponent<Button>().onClick.AddListener(
()=>{ LocalObj( path ); }
);
void LocalObj ( string filePath) {/*code*/}//change LocalObj method so it accepts this file path
Also
UnityEngine.Object[] all$$anonymous$$eshFilters = UnityEngine.Object.FindObjectsOfType<$$anonymous$$eshFilter>();
foreach (UnityEngine.Object o in all$$anonymous$$eshFilters)
should be:
$$anonymous$$eshFilter[] all$$anonymous$$eshFilters = FindObjectsOfType<$$anonymous$$eshFilter>();
foreach ($$anonymous$$eshFilter meshFilter in all$$anonymous$$eshFilters)
or:
var all$$anonymous$$eshFilters = FindObjectsOfType<$$anonymous$$eshFilter>();
foreach (var meshFilter in all$$anonymous$$eshFilters)
//var will figure out the type
so you won't have to cast
Such a great answer! Worked just like that, thank you!!!!