Jump to content

Mouse over Building in SpaceCenter Scene


Recommended Posts

You could cast a ray, query the ScreenSafeGUIText directly, or add a component to the building colliders that will receive the MonoBehaviour mouse events. Here's an example that uses all three:

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
internal class BuildingMouseoverCheck : MonoBehaviour
{
void Start()
{
FindObjectsOfType<SpaceCenterBuildingCollider>()
.ToList()
.ForEach(c => c.gameObject.AddComponent<MouseOverInfo>());

ScreenSafeUI.fetch.GetComponentInChildren<ScreenSafeGUIText>().gameObject.AddComponent<SSTextReader>();
}

void Update()
{
RaycastHit hitInfo;

if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, float.MaxValue, 1 << 15))
{
var coll = hitInfo.transform.GetComponent<SpaceCenterBuildingCollider>();

if (coll != null)
print(string.Format("Raycast: Mouse is over: {0}", coll.building.buildingInfoName));
}
}
}

class MouseOverInfo : MonoBehaviour
{
void OnMouseEnter()
{
print(string.Format("Component: Moused over {0}", GetComponent<SpaceCenterBuildingCollider>().building.buildingInfoName));
}

void OnMouseExit()
{
print(string.Format("Component: Mouse left {0}", GetComponent<SpaceCenterBuildingCollider>().building.buildingInfoName));
}
}

class SSTextReader : MonoBehaviour
{
void Update()
{
print(string.Format("GUI Text: {0}", GetComponent<ScreenSafeGUIText>().text));
}
}

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...