Q:

How to detect the state of an Unity3D app -- Visible or not

Is there any solution to detect the visibility of an Unity3D game? especially do some specific thing while it's switched to the foreground/background?

A:

MonoBehaviours support two useful hooks: 
OnApplicationFocus will be called any time the player gains or loses focus.
OnApplicationPause will be called any time the application is paused or unpaused. Note that it will only be paused if "run in background" is false in player settings, and that most code execution will stop until the app regains focus.
With the above caveats in mind, I usually prefer to use OnApplicationFocus, but both can be useful depending on what exactly you're trying to do.
As far as handling those events, you could make sure that some object in every scene is listening for them. Either attach some sort of singleton object that persists between scenes, or keep an object per scene that handles them appropriately.
Unity's scripting reference provides this example. By attaching this script to an object in some scene, it will throw up a GUI message while the game has lost focus:
public class Example : MonoBehaviour {
    public bool paused;

    void OnGUI() {
        if (paused) {
            GUI.Label(new Rect(100, 100, 50, 30), "Game paused");
        }
    }

    void OnApplicationFocus(bool focusStatus) {
        paused = focusStatus;
    }
}

