Adventure Creator Wikia
(Updated the script, removing the need to manually assign the custom events.)
Tag: Visual edit
No edit summary
Tag: Visual edit
Line 18: Line 18:
 
public Hotspot hotspotToShow;<br>
 
public Hotspot hotspotToShow;<br>
 
public Highlight highlightToSync;<br>
 
public Highlight highlightToSync;<br>
 
 
<br>
 
<br>
 
public string menuName = "Hotspot"; // The name of the Hotspot Menu to copy<br>
 
public string menuName = "Hotspot"; // The name of the Hotspot Menu to copy<br>
  +
public string labelName = "HotspotLabel"; // The Label element of the Hotspot Menu<br>
 
private Menu myMenu; // Our own local Menu<br>
 
private Menu myMenu; // Our own local Menu<br>
 
<br>
 
<br>
Line 27: Line 27:
 
{<br>
 
{<br>
 
// Attempt to auto-assign components if not set yet<br>
 
// Attempt to auto-assign components if not set yet<br>
if (highlightToSync == null)<br>
+
if (hotspotToShow == null)<br>
 
{<br>
 
{<br>
highlightToSync = GetComponent <Highlight>();<br>
 
 
hotspotToShow = GetComponent <Hotspot>();<br>
 
hotspotToShow = GetComponent <Hotspot>();<br>
  +
}<br>
  +
if (highlightToSync == null && hotspotToShow != null)<br>
  +
{<br>
 
highlightToSync = hotspotToShow.highlight;<br>
 
}<br>
 
}<br>
 
<br>
 
<br>
Line 66: Line 69:
 
myMenu.appearType = AppearType.Manual; // Set it to Manual so that we can control it easily<br>
 
myMenu.appearType = AppearType.Manual; // Set it to Manual so that we can control it easily<br>
 
myMenu.isLocked = false; // Unlock it so that the default can remain locked if necessary<br>
 
myMenu.isLocked = false; // Unlock it so that the default can remain locked if necessary<br>
myMenu.SetHotspot (hotspotToShow, null); // Link it to the Hotspot<br>
 
 
myMenu.title += this.name;<br>
 
myMenu.title += this.name;<br>
 
myMenu.SetHotspot (hotspotToShow, null); // Link it to the Hotspot<br>
  +
<br>
  +
if (!string.IsNullOrEmpty (labelName))<br>
  +
{<br>
  +
(myMenu.GetElementWithName (labelName) as MenuLabel).labelType = AC_LabelType.Normal;<br>
  +
(myMenu.GetElementWithName (labelName) as MenuLabel).label = hotspotToShow.GetName (Options.GetLanguage ());<br>
  +
}<br>
 
}<br>
 
}<br>
 
<br>
 
<br>

Revision as of 11:16, 11 September 2019

This script allows you to display multiple Hotspot labels at the same time - one for each highlight Hotspot.  It works by using the Highlight component's On/Off events to control a copy of a Hotspot menu defined in the Menu Manager.

It is particularly useful when working with Touch Screen input in conjunction with Player Vicinity hotspot detection, since that involves nearby Hotspots highlighting without becoming selected.

Note that this requires AC v1.60.5 or newer.

First, create a Hotspot menu (or modify the default one) with both an Appear type and Position of On Hotspot.  To prevent it showing up normally as well, also check Start game locked off? (our copies will be unlocked through script).

Next, define a Highlight component for each Hotspot.  If you don't want the Highlight to have a visible effect on any mesh, uncheck Auto-brighten materials?.

Create a new C# script named MultiHotspotLabel, and attach it to each Highlight GameObject:

using UnityEngine;
using AC;

public class MultiHotspotLabel : MonoBehaviour
{

public Hotspot hotspotToShow;
public Highlight highlightToSync;

public string menuName = "Hotspot"; // The name of the Hotspot Menu to copy
public string labelName = "HotspotLabel"; // The Label element of the Hotspot Menu
private Menu myMenu; // Our own local Menu


private void OnEnable ()
{
// Attempt to auto-assign components if not set yet
if (hotspotToShow == null)
{
hotspotToShow = GetComponent <Hotspot>();
}
if (highlightToSync == null && hotspotToShow != null)
{
highlightToSync = hotspotToShow.highlight;
}

// Add our own listeners to the Highlight component's onHighlightOn and onHighlightOff events
if (highlightToSync != null)
{
highlightToSync.callEvents = true;
highlightToSync.onHighlightOn.AddListener (ShowForHotspot);
highlightToSync.onHighlightOff.AddListener (Hide);
}
}


private void OnDisable ()
{
// Remove our own listeners from the Highlight component's onHighlightOn and onHighlightOff events
if (highlightToSync != null)
{
highlightToSync.onHighlightOn.RemoveListener (ShowForHotspot);
highlightToSync.onHighlightOff.RemoveListener (Hide);
}
}


private void ShowForHotspot ()
{
// Call this function to show a new Menu linked to the given Hotspot
if (myMenu == null)
{
// When run for the first time, create a new Menu and use the default Hotspot menu to copy from
Menu menuToCopy = PlayerMenus.GetMenuWithName (menuName);
myMenu = ScriptableObject.CreateInstance <Menu>();

myMenu.CreateDuplicate (menuToCopy); // Copy from the default Menu
myMenu.appearType = AppearType.Manual; // Set it to Manual so that we can control it easily
myMenu.isLocked = false; // Unlock it so that the default can remain locked if necessary
myMenu.title += this.name;
myMenu.SetHotspot (hotspotToShow, null); // Link it to the Hotspot

if (!string.IsNullOrEmpty (labelName))
{
(myMenu.GetElementWithName (labelName) as MenuLabel).labelType = AC_LabelType.Normal;
(myMenu.GetElementWithName (labelName) as MenuLabel).label = hotspotToShow.GetName (Options.GetLanguage ());
}
}

// Turn the menu on
myMenu.TurnOn ();

// Register with PlayerMenus to handle updating
KickStarter.playerMenus.RegisterCustomMenu (myMenu, true);
}


private void Hide ()
{
// Call this function to hide the Menu
if (myMenu != null)
{
myMenu.TurnOff ();
myMenu = null;
}
}

}

In its component Inspector (or by modifying the script), set the Menu Name value to the name of the Hotspot Menu listed in your Menu Manager that should be copied each time. The Hotspot To Show and Highlight To Sync values will also need to be assigned, but this is automatic if these components are already on the same GameObject as the above script.

When the game runs, the script will automatically add its own functions to the Highlight component's custom events.