private void Initialize() { if (isInitialized) { return; } if (tooltipStyle != null) { // Check with the TooltipManager to see if there is already a tooltip object instantiated for this style. // If not, instantiate it and add it to the TooltipManager. if (!TooltipManager.Instance.Tooltips.ContainsKey(tooltipStyle)) { TooltipStyle ttStyle = Instantiate(tooltipStyle); ttStyle.name = tooltipStyle.name; ttStyle.transform.SetParent(TooltipManager.Instance.TooltipContainer.transform, false); Tooltip newTooltip = new Tooltip() { GameObject = ttStyle.gameObject }; newTooltip.Initialize(); newTooltip.Deactivate(); TooltipManager.Instance.Tooltips.Add(tooltipStyle, newTooltip); } Tooltip = TooltipManager.Instance.Tooltips[tooltipStyle]; } isInitialized = true; }
/// <summary> /// This is an example of creating a button at runtime from a prefab that does NOT already have a tooltip, /// programmatically adding a tooltip to it that does not already exist in the scene, and modifying it to /// display whatever dynamic message we want. /// </summary> private void AddNewTooltip() { // Load the button from Resources. GameObject buttonPrefab = Resources.Load <GameObject>("DynamicObjWithoutTooltip"); if (buttonPrefab != null) { // Assuming the prefab exists, instantiate the button and set its parent. GameObject button = Instantiate(buttonPrefab); button.transform.SetParent(transform, false); // Add the TooltipTrigger component to the button. TooltipTrigger tooltipTrigger = button.gameObject.AddComponent <TooltipTrigger>(); TooltipStyle tooltipStyle = Resources.Load <TooltipStyle>("MetroSimple"); tooltipTrigger.tooltipStyle = tooltipStyle; // Set the tooltip text. tooltipTrigger.SetText("BodyText", String.Format("This object was created at <b><color=#F3B200>runtime</color></b> from a prefab that <b><color=#F3B200>did not</color></b> already have a tooltip on it. The tooltip was added programmatically and the message and other parameters modified through code. This \"metro\" tooltip style was also added dynamically to the scene.\n\nObject created and tooltip text assigned at {0}.", DateTime.Now)); // Set some extra style properties on the tooltip tooltipTrigger.maxTextWidth = 250; tooltipTrigger.backgroundTint = Color.white; tooltipTrigger.tipPosition = TipPosition.BottomRightCorner; } }
/// <summary>Triggers a tooltip immediately on the game object specified.</summary> /// <param name="onObject">The game object to pop a tooltip over.</param> public void PopupTooltip(GameObject onObject, string bodyText, string buttonText) { // Add the TooltipTrigger component to the object we want to pop a tooltip up for. TooltipTrigger tooltipTrigger = onObject.GetComponent <TooltipTrigger>(); // if the tooltip trigger is already being remotely controlled by this game object, exit. //if (tooltipTrigger != null && tooltipTrigger.remoteTrigger == gameObject) return; if (tooltipTrigger == null) { tooltipTrigger = onObject.AddComponent <TooltipTrigger>(); } TooltipStyle tooltipStyle = Resources.Load <TooltipStyle>("CleanSimpleCloseButton"); tooltipTrigger.tooltipStyle = tooltipStyle; // Set the tooltip text and properties. tooltipTrigger.SetText("BodyText", bodyText); tooltipTrigger.SetText("ButtonText", String.IsNullOrEmpty(buttonText) ? "Continue" : buttonText); tooltipTrigger.tipPosition = TipPosition.TopRightCorner; tooltipTrigger.maxTextWidth = 300; tooltipTrigger.staysOpen = true; // make this a tooltip that stays open... tooltipTrigger.isBlocking = true; // ...and is blocking (no other tooltips allowed while this one is active). // Popup the tooltip and give it the object that triggered it (the Canvas in this case). tooltipTrigger.Popup(8f, gameObject); }
private void Initialize() { if (isInitialized) { return; } RectTransform = gameObject.GetComponent <RectTransform>(); if (tooltipStyle != null) { // Check with the TooltipManager to see if there is already a tooltip object instantiated for this style. // If there is already a tooltip object in the scene for this style, use it. Otherwise, instantiate it and add it to the TooltipManager. if (!TooltipManager.Instance.Tooltips.ContainsKey(tooltipStyle)) { TooltipStyle ttStyle = Instantiate(tooltipStyle); ttStyle.name = tooltipStyle.name; ttStyle.transform.SetParent(TooltipManager.Instance.RootCanvas.transform, false); Tooltip newTooltip = new Tooltip() { GameObject = ttStyle.gameObject }; newTooltip.Initialize(); newTooltip.Deactivate(); TooltipManager.Instance.Tooltips.Add(tooltipStyle, newTooltip); } Tooltip = TooltipManager.Instance.Tooltips[tooltipStyle]; } isInitialized = true; }
private static void SetTooltipPosition(TipPosition tipPosition, GameObject tooltipObj, TooltipStyle style, Vector3[] triggerCorners, Image tooltipImage, RectTransform tooltipRectTrans, bool useMousePosition) { Vector3 pos = Vector3.zero; Vector3 offsetVector; switch (tipPosition) { case TipPosition.TopRightCorner: case TipPosition.MouseTopRightCorner: offsetVector = new Vector3(-1 * style.tipOffset, -1 * style.tipOffset); if (useMousePosition) { pos = Input.mousePosition + offsetVector; } else { pos = triggerCorners[2] + offsetVector; } tooltipRectTrans.pivot = new Vector2(0, 0); tooltipImage.sprite = style.bottomLeftCorner; break; case TipPosition.BottomRightCorner: case TipPosition.MouseBottomRightCorner: offsetVector = new Vector3(-1 * style.tipOffset, style.tipOffset); if (useMousePosition) { pos = Input.mousePosition + offsetVector; } else { pos = triggerCorners[3] + offsetVector; } tooltipRectTrans.pivot = new Vector2(0, 1); tooltipImage.sprite = style.topLeftCorner; break; case TipPosition.TopLeftCorner: case TipPosition.MouseTopLeftCorner: offsetVector = new Vector3(style.tipOffset, -1 * style.tipOffset); if (useMousePosition) { pos = Input.mousePosition + offsetVector; } else { pos = triggerCorners[1] + offsetVector; } tooltipRectTrans.pivot = new Vector2(1, 0); tooltipImage.sprite = style.bottomRightCorner; break; case TipPosition.BottomLeftCorner: case TipPosition.MouseBottomLeftCorner: offsetVector = new Vector3(style.tipOffset, style.tipOffset); if (useMousePosition) { pos = Input.mousePosition + offsetVector; } else { pos = triggerCorners[0] + offsetVector; } tooltipRectTrans.pivot = new Vector2(1, 1); tooltipImage.sprite = style.topRightCorner; break; } // Set the position of the tooltip container. tooltipObj.transform.position = pos; }
private static void SetTooltipPosition(TipPosition tipPosition, GameObject tooltipObj, TooltipStyle style, Vector3[] triggerCorners, Image tooltipImage, RectTransform tooltipRectTrans, bool useMousePosition) { Vector3 pos = Vector3.zero; Vector3 offsetVector; switch (tipPosition) { case TipPosition.TopRightCorner: case TipPosition.MouseTopRightCorner: offsetVector = new Vector3(-1 * style.tipOffset, -1 * style.tipOffset); if (useMousePosition) pos = Input.mousePosition + offsetVector; else pos = triggerCorners[2] + offsetVector; tooltipRectTrans.pivot = new Vector2(0, 0); tooltipImage.sprite = style.bottomLeftCorner; break; case TipPosition.BottomRightCorner: case TipPosition.MouseBottomRightCorner: offsetVector = new Vector3(-1 * style.tipOffset, style.tipOffset); if (useMousePosition) pos = Input.mousePosition + offsetVector; else pos = triggerCorners[3] + offsetVector; tooltipRectTrans.pivot = new Vector2(0, 1); tooltipImage.sprite = style.topLeftCorner; break; case TipPosition.TopLeftCorner: case TipPosition.MouseTopLeftCorner: offsetVector = new Vector3(style.tipOffset, -1 * style.tipOffset); if (useMousePosition) pos = Input.mousePosition + offsetVector; else pos = triggerCorners[1] + offsetVector; tooltipRectTrans.pivot = new Vector2(1, 0); tooltipImage.sprite = style.bottomRightCorner; break; case TipPosition.BottomLeftCorner: case TipPosition.MouseBottomLeftCorner: offsetVector = new Vector3(style.tipOffset, style.tipOffset); if (useMousePosition) pos = Input.mousePosition + offsetVector; else pos = triggerCorners[0] + offsetVector; tooltipRectTrans.pivot = new Vector2(1, 1); tooltipImage.sprite = style.topRightCorner; break; } // Set the position of the tooltip container. tooltipObj.transform.position = pos; }
/// <summary>This method does the heavy lifting for setting the correct tooltip position.</summary> private static void SetPosition(this Tooltip tooltip, TipPosition tipPosition, TooltipStyle style, Vector3[] triggerCorners, Image bkgImage, RectTransform tooltipRectTrans, Canvas canvas, Camera camera) { // Tooltip Trigger Corners: // 0 = bottom left // 1 = top left // 2 = top right // 3 = bottom right Vector3 pos = Vector3.zero; Vector3 offsetVector = Vector3.zero; bool useMousePos = tipPosition == TipPosition.MouseBottomLeftCorner || tipPosition == TipPosition.MouseTopLeftCorner || tipPosition == TipPosition.MouseBottomRightCorner || tipPosition == TipPosition.MouseTopRightCorner || tipPosition == TipPosition.MouseTopMiddle || tipPosition == TipPosition.MouseLeftMiddle || tipPosition == TipPosition.MouseRightMiddle || tipPosition == TipPosition.MouseBottomMiddle; Vector3 mousePos = Input.mousePosition; if (canvas.renderMode == RenderMode.ScreenSpaceCamera) { mousePos.z = canvas.planeDistance; mousePos = camera.ScreenToWorldPoint(mousePos); } switch (tipPosition) { case TipPosition.TopRightCorner: case TipPosition.MouseTopRightCorner: offsetVector = new Vector3(-1 * style.tipOffset, -1 * style.tipOffset); pos = useMousePos ? mousePos : triggerCorners[2]; tooltipRectTrans.pivot = new Vector2(0, 0); bkgImage.sprite = style.bottomLeftCorner; break; case TipPosition.BottomRightCorner: case TipPosition.MouseBottomRightCorner: offsetVector = new Vector3(-1 * style.tipOffset, style.tipOffset); pos = useMousePos ? mousePos : triggerCorners[3]; tooltipRectTrans.pivot = new Vector2(0, 1); bkgImage.sprite = style.topLeftCorner; break; case TipPosition.TopLeftCorner: case TipPosition.MouseTopLeftCorner: offsetVector = new Vector3(style.tipOffset, -1 * style.tipOffset); pos = useMousePos ? mousePos : triggerCorners[1]; tooltipRectTrans.pivot = new Vector2(1, 0); bkgImage.sprite = style.bottomRightCorner; break; case TipPosition.BottomLeftCorner: case TipPosition.MouseBottomLeftCorner: offsetVector = new Vector3(style.tipOffset, style.tipOffset); pos = useMousePos ? mousePos : triggerCorners[0]; tooltipRectTrans.pivot = new Vector2(1, 1); bkgImage.sprite = style.topRightCorner; break; case TipPosition.TopMiddle: case TipPosition.MouseTopMiddle: offsetVector = new Vector3(0, -1 * style.tipOffset); pos = useMousePos ? mousePos : triggerCorners[1] + (triggerCorners[2] - triggerCorners[1]) / 2; tooltipRectTrans.pivot = new Vector2(.5f, 0); bkgImage.sprite = style.topMiddle; break; case TipPosition.BottomMiddle: case TipPosition.MouseBottomMiddle: offsetVector = new Vector3(0, style.tipOffset); pos = useMousePos ? mousePos : triggerCorners[0] + (triggerCorners[3] - triggerCorners[0]) / 2; tooltipRectTrans.pivot = new Vector2(.5f, 1); bkgImage.sprite = style.bottomMiddle; break; case TipPosition.LeftMiddle: case TipPosition.MouseLeftMiddle: offsetVector = new Vector3(style.tipOffset, 0); pos = useMousePos ? mousePos : triggerCorners[0] + (triggerCorners[1] - triggerCorners[0]) / 2; tooltipRectTrans.pivot = new Vector2(1, .5f); bkgImage.sprite = style.leftMiddle; break; case TipPosition.RightMiddle: case TipPosition.MouseRightMiddle: offsetVector = new Vector3(-1 * style.tipOffset, 0); pos = useMousePos ? mousePos : triggerCorners[3] + (triggerCorners[2] - triggerCorners[3]) / 2; tooltipRectTrans.pivot = new Vector2(0, .5f); bkgImage.sprite = style.rightMiddle; break; } tooltip.GameObject.transform.position = pos; tooltipRectTrans.anchoredPosition += (Vector2)offsetVector; }