skip to Main Content

Scripting Support (C#)

Using scripting to add effects

Use GetComponent<HighlightEffect>() to get a reference to the component of your gameobject.

Most properties shown in the inspector can be accessed through code, for example:

using HighlightPlus;
…
HighlightEffect effect = myGameObject.GetComponent<HighlightEffect>();
effect.outline = true;
effect.outlineColor = Color.blue;
effect.SetGlowColor(Color.yellow);
effect.UpdateMaterialProperties();

To control the state of the highlight, use:

effect.SetHighlighted(true/false) or effect.highlighted = true/false.

Important! If you change the hierarchy of your object (change its parent or attach it to another object), you need to call effect.Refresh() to make Highlight Plus update its internal data.

Setting profile at runtime

Call ProfileLoad() or ProfileReload() methods of the HighlightEffect component and pass your highlight profile object.

Changing properties at runtime

When changing specific script properties at runtime, call UpdateMaterialProperties() to ensure those changes are applied immediately.

Getting a reference to highlighted / selected objects

The Highlight Effect component exposes two properties “lastHighlighted” and “lastSelected” which keep track of the last activated highlight plus objects (highlighted or selected). See “Highlighting vs Selection” section in this manual for more details.

Also, the Highlight Manager script exposes a static “selectedObjects” list which contains all selected objects.

Executing a Hit FX effect

The HitFX effect is a fast flash overlay effect which is used from code. Just call HitFX and pass the desired parameters:

using HighlightPlus;
…

HighlightEffect effect = myGameObject.GetComponetn<HighlightEffect>();
effect.HitFX(color, duration, initial_intensity);

Starting the Target FX on demand

Call effect.TargetFX() method to start the target FX whenever you wish. This method is useful if the target is already selected but you want to repeat the TargetFX animation.

Note: if you change target fx properties using scripting, you may need to call UpdateMaterialProperties() so the new values get reflected in new target fx executions.

Changing the target of highlight using scripting

The objects to be highlighted by the script are those configured by the “Include” option in the Highlight Options section. However, you can override this selection using scripting in two ways:

effect.SetTarget(transform) will instruct the script to execute the effects on the given object.

effect.SetTarget(transform, Renderer[] additionalObjects) will do the same but also will include any number of other renderers. Useful when you want to highlight a set of objects defined by a script.

Events / reacting to highlighting

Note: check SphereHighlightEventExample.cs script in the demo scene.

The Highlight Effect script exposes two events (OnObjectHighlightStart and OnObjectHighlightEnd) which fire when the object highlight starts or ends.

Example:

using UnityEngine;
using HighlightPlus;

public class SphereHighlightEventExample : MonoBehaviour {

   void Start() {
       HighlightEffect effect = GetComponent<HighlightEffect> ();
       effect.OnObjectHighlightStart += ValidateHighlightObject;
   }


   bool ValidateHighlightObject(GameObject obj) {
       // Used to fine-control if the object can be highlighted; return false to cancel highlight
       return true;
   }

}

These two events are also exposed by the HighlightManager and the HighlightTrigger.

For example, when using the HighlightManager, you can also hook into its OnObjectHighlightStart, OnObjectHighlightEnd, OnObjectSelected, OnObjectUnselected events:

HighlightManager.instance.OnObjectHighlightStart += MyEventHandler;

Events / reacting to selection

Selection events OnObjectSelected and OnObjectUnSelected are provided by the HighlightManager or HighlightTrigger components. Check demo scene 2 for a working sample (using the code below).

Using the Highlight Manager

using UnityEngine;
using HighlightPlus;

public class SelectionEventExample : MonoBehaviour {

   void Start() {
       HighlightManager.instance.OnObjectSelected += SelectObject;
       HighlightManager.instance.OnObjectUnSelected += UnSelectObject;
   }


   bool SelectObject (GameObject obj) {
       // Used to fine-control if the object can be selected; return false to cancel selection
       return true;
   }

   bool UnSelectObject (GameObject obj) {
       // Used to fine-control if the object can be unselected; return false to cancel unselection
       return true;
   }
}

Using the Highlight Trigger

using UnityEngine;
using HighlightPlus;

public class SelectionEventExample : MonoBehaviour {

   void Start() {
HighlightTrigger ht = GetComponent<HighlightTrigger> ();
       ht.OnObjectSelected += SelectObject;
       ht.OnObjectUnSelected += SelectObject;
   }


   bool SelectObject (GameObject obj) {
       // Used to fine-control if the object can be selected; return false to cancel selection
       return true;
   }

   bool UnSelectObject (GameObject obj) {
       // Used to fine-control if the object can be unselected; return false to cancel unselection
       return true;
   }
}

Messages

Highlight Effect will send the “HighlightStart” and “HighlightEnd” to any script attached to the gameobject when its highlighted (or highlight ends). You can get those messages using the following code:

using UnityEngine;
using HighlightPlus;

public class MyBehaviour : MonoBehaviour {

  void HighlightStart () {
      Debug.Log ("Object highlighted!");
  }

  void HighlightEnd () {
      Debug.Log ("Oject not highlighted!");
  }

}

Manually selecting/toggling/unselecting objects with Highlight Manager

The Highlight Manager provides a “selection” state functionality which you can invoke using the methods SelectObject, ToggleObject and UnselectObject of the Highlight Manager. The following code is extracted from demo scene 2:

using UnityEngine;
using HighlightPlus;

namespace HighlightPlus.Demos {

    public class ManualSelectionDemo : MonoBehaviour {

        HighlightManager hm;

        public Transform objectToSelect;

        void Start() {
            hm = FindObjectOfType<HighlightManager>();
        }

        void Update() {
            if (Input.GetKeyDown(KeyCode.Alpha1)) {
                hm.SelectObject(objectToSelect);
            }
            if (Input.GetKeyDown(KeyCode.Alpha2)) {
                hm.ToggleObject(objectToSelect);
            }
            if (Input.GetKeyDown(KeyCode.Alpha3)) {
                hm.UnselectObject(objectToSelect);
            }
        }
    }
}
Back To Top