Hi!
I have some problems with buttons at runtime. I want to create a GUI where i can add some buttons at runtime if i find them ingame. When i click on a button, a text should appear on the right side. So far it works. But i want that the text is not visible until i click on the button. The other problem is that everytime i add a button to the GUI while a text (from another button) is open and if i click on a new button the text from the new button is not showing in a own window, it add the text to the text what is open. When i close one button and open a new one everything is alright. I hope you understand me, my english is not very well but i will add a image and the code, maybe you can see what my problem is. I worked many hours on this problem now, but i cant find the correct solution. Thank you!
Here the code from two scripts! Just for testing.
test Script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class test : MonoBehaviour {
public LogFileManager logFileManager;
public Button logFileFoundTitlePrefab;
public GameObject logEntryTextPrefab;
private bool isActive;
private bool showText;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnMouseDown(){
CreateLogFileEntry("LogfileButtons", "LogfileData", "Erster Eintrag", "Hallo ich bin ein Alien!");
}
//Erstelle einen neuen Logfileeintrag mit Prefabs
public void CreateLogFileEntry(string logFileFound, string logFileData, string logFileTitle, string entryText){
GameObject logEntryText = (GameObject)Instantiate(logEntryTextPrefab);
Button logFileFoundTitle = (Button)Instantiate(logFileFoundTitlePrefab);
logFileManager.LogFileSetup(logFileFound, logFileData, logEntryText, logFileFoundTitle, logFileTitle, entryText);
logFileFoundTitle.onClick.AddListener(() => OpenLogEntry(logEntryText));
}
public void OpenLogEntry(GameObject logEntryText){
if(isActive == false){
HideEntryText(logEntryText);
} else {
ShowEntryText(logEntryText);
}
}
public void HideEntryText(GameObject logText) {
logText.SetActive(false);
isActive = true;
}
public void ShowEntryText(GameObject logText) {
logText.SetActive(true);
isActive = false;
}
}
LogFileManager Script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LogFileManager : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//Setup für die Logfileeinträge um sie während der Laufzeit an die richtige Position zu setzen und richtig zu skalieren etc.
public void LogFileSetup(string logFileFound, string logFileData, GameObject logEntryText, Button logFileFoundTitle,
string logFileTitle, string entryText){
logFileFoundTitle.transform.SetParent(GameObject.Find(logFileFound).transform);
logEntryText.transform.SetParent(GameObject.Find(logFileData).transform);
logFileFoundTitle.transform.localScale = new Vector3 (1,1,1);
logEntryText.transform.localScale = new Vector3 (1,1,1);
}
}
Thank you very much!
Greetings