Using Emissive Textures for Feedback | AR Unity Developer

Derek Anderson
3 min readMay 16, 2024

--

Last we left on our Magic Box project, the box lid opens after the unlock combo is checked with some parameters and class communication. While the box lid opens as intended, it’s difficult to know which gem has already been selected unless looking at the console logs. Let’s work on making these gems easier to see when selected.

Image from https://compoundsemiconductor.net/article/112452/Scientists_learn_more_about_ZnO_light_emission
public class Gem : MonoBehaviour
{
public string _gemColor = "";

// Start is called before the first frame update
void Start()
{
//cache gem intial emission color

//set gem emmission color to black
}


public void ChangeEmission(bool isEmitting)
{
if (isEmitting == true)
{
//make this gem emmissive
}
else
{
//make this gem unemissive
}
}
}

Start with the Gem script. Create a void method that passes a Boolean as a parameter. The method ChangeEmission will check if an object is emitting or not. In Start, the gem’s initial emission color will be cached. After that, the original color will be used as the emitted color. The gem’s emission color will then be set to black. In other words, the gem will not be lit up.

Create a variable for the gem’s material and a variable for the original gem emission color. These variables will be used for caching the gem’s emission color and setting it to black so that there is no emission on the gem.

    [SerializeField]
private Material _targetMaterial;

private Color _initialEmissionColor;

// Start is called before the first frame update
void Start()
{
//cache gem intial emission color
_initialEmissionColor = _targetMaterial.GetColor("_EmissionColor");

//set gem emmission color to black
_targetMaterial.SetColor("_EmissionColor", Color.black);
}

Now the ChangeEmission method can be written out, and it is similar to what is already in Start. If the gem is emitting, the color will be set the gem’s cached emission color. If not, it will be set to black, or not light up any color.

    public void ChangeEmission(bool isEmitting)
{
if (isEmitting == true)
{
//make this gem emissive
_targetMaterial.SetColor("_EmissionColor", _initialEmissionColor);
}
else
{
//make this gem unemissive
_targetMaterial.SetColor("_EmissionColor", Color.black);
}
}

This method can’t do anything right now because it isn’t being called anywhere. When we select a gem, the GemSelect method in BoxManager script runs with the selected gem passed in. This allows us to make the gem emissive in BoxManager.

    public void GemSelect(Gem currentSelectedGem)
{
_enteredGemOrder += currentSelectedGem._gemColor;

_currentGem += 1;

if (_currentGem == _amountOfGems)
{
CompareGemOrder();
}

currentSelectedGem.ChangeEmission(true);
}

The ResetGame method is what allows the emission of the gems to be reset, which can be done with an array. Each gem will run through and have the emission reset. Create the array and drag the gems over to the Box Manager script component on the box in the Unity Editor.

    [SerializeField] 
private Gem[] _gemsInScene;

To reset the emission on the gems, a foreach loop will be used to set the bool for the emission status of each gem to false in ResetGame.

    void ResetGame()
{
_currentGem = 0;
_enteredGemOrder = "";

//Reset the gem emission
foreach (var gem in _gemsInScene)
{
gem.ChangeEmission(false);
}
}

Finally, make sure that each gem has the correct material attached to them if you haven’t do so already.

--

--