/// <summary> /// Gets a point in a 2D square region defined by two points, accounting for padding on all sides. /// </summary> /// <returns>The point in region.</returns> /// <param name="bottomLeft">Bottom left.</param> /// <param name="topRight">Top right.</param> /// <param name="paddingTop">Padding top.</param> /// <param name="paddingRight">Padding right.</param> /// <param name="paddingBottom">Padding bottom.</param> /// <param name="paddingLeft">Padding left.</param> public static Vector3 GetPointInRegion( Vector3 bottomLeft, Vector3 topRight, float paddingTop = 0, float paddingRight = 0, float paddingBottom = 0, float paddingLeft = 0, int seed = -1, bool forceNewSeed = false ) { TrySetSeed(seed, forceNewSeed); float x = SaFrMo.RandomFloat(bottomLeft.x + paddingLeft, topRight.x - paddingRight); float y = SaFrMo.RandomFloat(bottomLeft.y + paddingBottom, topRight.y - paddingTop); return(new Vector3(x, y)); }
/// <summary> /// Creates the new Tooltip. /// </summary> /// <returns>The new.</returns> /// <param name="toDisplay">String to display.</param> /// <param name="parent">Parent.</param> /// <param name="position">Position in screen space.</param> /// <param name="toFollow">Object to stay on top of.</param> /// <param name="offset">Offset from followed object..</param> /// <param name="exitText">Exit text.</param> /// <param name="exitChoices">Exit choices.</param> /// <param name="exitCallbacks">Exit callbacks.</param> /// <param name="onCreation">On creation callback. Accepts the created Tooltip's GameObject as a parameter.</param> /// <param name="onDestruction">On destruction callback. Accepts the created Tooltip's GameObject as a parameter.</param> /// <param name="style">Style.</param> /// <param name="toCreate">Prefab to create.</param> /// <param name="destroyAfter">Destroy after a given number of seconds (-1 = never)</param> /// <param name="subsequentTooltips">Shortcut to subsequent tooltips. Useful for tutorials that don't require special placement, for instance.</param> public static Tooltip CreateNew(string toDisplay = "", RectTransform parent = null, Vector3 position = default(Vector3), Transform toFollow = null, Vector3 offset = default(Vector3), string exitText = "Continue", string[] exitChoices = null, TooltipCallback[] exitCallbacks = null, TooltipCallback onCreation = null, TooltipCallback onDestruction = null, TooltipStyleOptions style = null, Tooltip toCreate = null, float destroyAfter = -1f, string[] subsequentTooltips = null) { // Look for Tooltips component in the scene Tooltips tooltipsMaster = FindObjectOfType <Tooltips>(); if (tooltipsMaster == null) { // Create Tooltips master component if it doesn't exist GameObject g = new GameObject("Tooltip Master", typeof(Tooltips)); tooltipsMaster = g.GetComponent <Tooltips>(); } // Set prefab to instantiate if (tooltipsMaster.prefab == null) { tooltipsMaster.prefab = Resources.Load <GameObject>(defaultPrefabPath); } // Set GameObject to instantiate Tooltip toInstantiate = (toCreate == default(Tooltip) ? tooltipsMaster.prefab.GetComponent <Tooltip>() : toCreate); // Instantiate tooltip GameObject newTooltip = Instantiate <GameObject>(toInstantiate.gameObject) as GameObject; // Make sure new tooltip has Tooltip component Tooltip tooltip = SaFrMo.GetOrCreate <Tooltip> (newTooltip); RectTransform tooltipRecTransform = newTooltip.GetComponent <RectTransform>(); // OnCreate callback if (onCreation != null) { onCreation.Invoke(newTooltip); } // Set text to display if (toDisplay.Length > 0) { tooltip.SetDisplayText(toDisplay); } // Save OnDestruction callback if (onDestruction != null) { tooltip.onDestruction = onDestruction; } // Set parent if (parent == null) { Canvas c = FindObjectsOfType <Canvas>().FirstOrDefault(x => x.renderMode != RenderMode.WorldSpace); if (c == default(Canvas)) { // Create Canvas and EventSystem if none exists yet c = new GameObject("Canvas (Created by Tooltips.cs)", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)).GetComponent <Canvas>(); c.renderMode = RenderMode.ScreenSpaceOverlay; GameObject g = new GameObject("Event System (Created by Tooltips.cs)", typeof(EventSystem), typeof(StandaloneInputModule)); } parent = c.GetComponent <RectTransform>(); } // Make sure our parent is saved if (parent != null) { // Set parent newTooltip.transform.SetParent(parent, false); } // Position new Tooltip // First, account for default position if (position == default(Vector3)) { position.x = Screen.width / 2f; position.y = Screen.height / 2f; } // If toFollow is specified, override default position if (toFollow != null) { position = Camera.main.WorldToScreenPoint(toFollow.position); // Move by offset if (offset == default(Vector3)) { offset = Vector3.zero; } position += offset; // Save to Tooltip tooltip.toFollow = toFollow; tooltip.toFollowOffset = offset; } tooltipRecTransform.position = position; // Create exit buttons if (exitChoices == null && exitText.Length > 0) { exitChoices = new string[] { exitText }; } // Create exit text(s) and choice(s) if we have any if (exitChoices != null && exitChoices.Length > 0) { tooltip.CreateExitButtons(exitChoices, exitCallbacks); } // Set style if (style != null) { tooltip.style = style; } // Set autodestroy timer if (destroyAfter != -1f) { tooltip.SelfDestruct(destroyAfter); } // Set up subsequent Tooltips if (subsequentTooltips != null && subsequentTooltips.Length > 0) { List <string> allExceptFirst = new List <string>(subsequentTooltips); allExceptFirst.RemoveAt(0); string first = subsequentTooltips[0]; tooltip.onDestruction = x => { Tooltips.CreateNew(first, parent: parent, position: position, toFollow: toFollow, offset: offset, exitText: exitText, toCreate: toCreate, destroyAfter: destroyAfter, subsequentTooltips: allExceptFirst.ToArray <string>()); }; } // Return instantiated Tooltip return(tooltip); }