Fade In GUI on proximity

Hello, I have a similar problem to this post, but not quite: http://answers.unity3d.com/questions/28878/show-gui-text-when-distance-6

I have a bunch of paintings and I want a window to appear with information about each painting as the user gets near it. I'd like this to fade in and out as they approach and leave the proximity of each painting.

I have the proximity issue solved with a `OnTriggerEvent(other: Collider)` and I use a collision sphere for each painting (maybe there's a better way? Tried RayCasting but it didn't work).

Here are the questions:

  1. I'm using `GUI.Box(Rect(10, 50, 300, 200), "Details:");` to draw the background. Should I be using a GUITexture instead? If so, how to manage it?
  2. I need to have different text for each painting, how can I accomplish this? If I create GUIText objects, they're always there and I don't know how to hide them then call them up again.
  3. Is there a way to tell Unity to read the text from a file?
  4. Lastly, how can I make the whole thing (background and text) fade in and out when called?

I know these are a lot of questions in one, but mostly I'm looking for guidance on how to accomplish this properly, I've been looking at the documentation and examples and have not been able to figure things out successfully, very new to Unity.

Thanks in advance.

I've enhanced the code so that now I can get the GUITexture to hide and then display when I am near an object. However, the Fade function is not working properly. It will not display at all if I have this code (copied from the Wiki): `Fade.use.Alpha(myGuiTexture, 0.0, 1.0, 2.0, EaseType.In);` or any other use of Fade, even in the "else" statement below. Any ideas are welcome.

Full code here:

private var myWindow: boolean;
private var windowOn : boolean;
private var windowRect : Rect = Rect (150, 150, 120, 50);//window parameters

var cuadroText : GUIText;
var myGuiTexture : GUITexture;

myGuiTexture.enabled = false;

function OnTriggerEnter (other : Collider) {
    myWindow = true;
}

function OnTriggerExit (other : Collider) {
    myWindow = false; // hide window
}

function OnGUI () {

    if (myWindow == true) {

    // fade in del gui background
    myGuiTexture.enabled = true; // first we activate the guiTexture
    Fade.use.Alpha(myGuiTexture, 0.0, 1.0, 2.0, EaseType.In);
    GUI.Box(Rect(10, 50, 300, 200), "Details:"); // draw a box to make sure things are working.

    }  else {
            // fade out
        //Fade.use.Alpha(myGuiTexture, 1.0, 0.0, 3.0);
        myGuiTexture.enabled = false; // disable it.
        windowOn = false;
        }

}

  1. Using a GUI.Box is sufficient in most cases.
  2. You can create a script that is attached to the painting, with a public string variable you can set in the inspector.
  3. Yes, you can use TextAsset to get a text file.
  4. You have keep a boolean value which is set OnTriggerEnter and OnTriggerExit and display the GUI when it is set.

So, what I would have done would be to create a script that listen for OnTriggerEnter, OnTriggerExit and OnTriggerStay. In Enter/Exit I would toggle visibility of the GUI. For the proximity fade, I would either make it timer based so that if you have entered the trigger, it fades up regardless of how far away you are, or use OnTriggerStay to calculate the distance to the center of the object. From this distance, you can calculate the transparency easily with an AnimationCurve and call Evaluate on this to get the alpha value (with the distance as parameter). Make the AnimationCurve public, and you can set it in the designer, or you can use the class methods EaseInOut or Linear. For the text (or any images), I would make a public GUIContent, so you can set text, image and tooltip if you like.

Then, create a trigger and add the script to the trigger. Set the values as you please, and you should have a basic system working.

To make the fade, you can use GUI.color. Just remember to restore it at the last moment of your OnGUI callback so rest of GUI isn't affected. If you want to make the timed approach, I can recommend using Mathf.MoveTowards, toggling the alpha values you want to move toward over time.