I have created a class to “extend” the functionality of GUIText
My CustomGUIText class adds a few strings to represent on and off clicked states, the name of animation to be triggered by them and bool values to represent whether the text is in the on state or off state and whether it should be displayed or not.
Each CustomGUIText class is instantiated by another script that puts them all into a list, so they can be positioned where needed.
The mouseover event is handled by a third script that changes the text color when moused over.
When the text is clicked I would like it to call the ToggleToggle() function. This should change the toggle state and either start or stop the animation and change the name appropriately.
I can’t work out how to access the parent variable from this child component script however.
Any ideas?
using UnityEngine;
using System.Collections;
public class CustomGUIText {
private string onText;
private string offText;
private string animName;
private bool toggle;
private bool display;
private GameObject myGUIText;
// Constructors
public CustomGUIText(){
onText = "";
offText = "";
animName = "";
toggle = false;
display = false;
myGUIText = new GameObject();
myGUIText.AddComponent (typeof(GUIText));
myGUIText.guiText.enabled = false;
myGUIText.AddComponent (typeof(OnMouseOverText));
}
public CustomGUIText(string onValue, string offValue,string animNameValue, bool toggleValue, bool show){
onText = onValue;
offText = offValue;
animName = animNameValue;
toggle = toggleValue;
display = show;
myGUIText = new GameObject();
myGUIText.AddComponent (typeof(GUIText));
myGUIText.AddComponent (typeof(OnMouseOverText));
myGUIText.guiText.name = animName;
if (toggle) {
myGUIText.guiText.text = onText;
} else {myGUIText.guiText.text = offText;
}
if (!display) {
myGUIText.guiText.enabled = display;
}
}
// Getters
public string GetOnText(){
return onText;}
public string GetOffText(){
return offText;
}
public string GetAnimName(){
return animName;
}
public bool GetToggle(){
return toggle;
}
public bool GetDisplay(){
return display;
}
// Setters
public void SetOnText(string onValue){
onText = onValue;
}
public void SetOffText(string offValue){
offText = offValue;
}
public void SetAnimName (string animNameValue){
animName = animNameValue;
}
public void SetToggle (bool toggleValue){
toggle = toggleValue;
}
public void SetDisplay (bool displayValue){
display = displayValue;
}
// Toggles
public void ToggleDisplay(){
display = !display;
}
public void ToggleToggle(){
toggle = !toggle;
}
// setTextLocation
public void SetTextLocation(Vector3 target) {
myGUIText.transform.position = target;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (toggle) {
myGUIText.guiText.text = onText;
} else {myGUIText.guiText.text = offText;
}
if (!display) {
myGUIText.guiText.enabled = display;
}
}
}
// CreateTextPrefabs attaches to empty gameobject to create CustomGUITexts
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CreateTextPrefabs : MonoBehaviour {
private List<CustomGUIText> myCustomGUITextArray;
// Use this for initialization
void Start () {
// Here we create a series of CustomGUIText objects
myCustomGUITextArray = new List<CustomGUIText>();
myCustomGUITextArray.Add(new CustomGUIText ("On1", "Off1", "Anim1", false, true));
myCustomGUITextArray.Add(new CustomGUIText("On2", "Off2", "Anim2", false, true));
myCustomGUITextArray.Add (new CustomGUIText ("On3", "Off3", "Anim3", false, false));
myCustomGUITextArray.Add (new CustomGUIText ("On4", "Off4", "Anim4", true, true));
// put in location
int index = 0;
foreach (CustomGUIText item in myCustomGUITextArray) {
if (item.GetDisplay()){
index++;
float yValue = 0.5f + (0.04f * index);
item.SetTextLocation(new Vector3(0.5f,yValue,0f));
}
}
}
}
// The problem is accessing the parent of this component script for mouse click events to toggle the bool parameters.
using UnityEngine;
using System.Collections;
public class OnMouseOverText : MonoBehaviour {
private CustomGUIText myCustomGUIText;
// Use this for initialization
void Start () {
myCustomGUIText = GetComponent<CustomGUIText>(); // Doesn't like this
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter() {
this.guiText.material.color = Color.red;
}
void OnMouseExit(){
this.guiText.material.color = Color.white;
}
void OnMouseDown() {
myCustomGUIText.ToggleToggle(); //
print ("Button Pressed");
}
}