Implementing the interface
Using an interface is easy, simply add the interface after the MonoBehaviour. From here depending on your IDE you can right click the red underlined interface and select quick actions and implement interface. If you do not have this, simply create a function with exactly the same name as the interface has. For example – IOnBuildingPlace has the method OnPlace(GameObject gameObject) which is exampled below.
public class InterfaceExamples : MonoBehaviour, IOnBuildingPlace, IOnBuildingRemove
{
public void OnPlace(GameObject gameObject)
{
Debug.Log($"{gameObject.name} has been placed");
}
public void OnRemove(GameObject gameObject)
{
Debug.Log($"{gameObject.name} has been removed");
}
}
After implementing, the gameObject in question is returned to you, so you can manipulate to your needs. Adding code to the OnPlace method will execute when a building is placed. All interfaces have a summary which explains when they are called.
The full list of all 11 interfaces are exampled in InterfaceExamples.cs in the Demo/DemoScripts folder. Here is the demo code to see how to implement each one.
using UnityEngine;
public class InterfaceExamples : MonoBehaviour,
IOnBuildingPlace,
IOnBuildingRemove,
IOnBuildingMoveStart,
IOnBuildingMoveEnd,
IOnBuildingSelect,
IOnBuildingDeselect,
IOnBuildingOver,
IOnBuildingExit,
IOnBuildingTimedStart,
IOnBuildingTimedEnd,
IOnSetPreview
{
public void OnPlace(GameObject gameObject)
{
Debug.Log($"{gameObject.name} has been placed");
}
public void OnRemove(GameObject gameObject)
{
Debug.Log($"{gameObject.name} has been removed");
}
public void OnMoveStart(GameObject gameObject)
{
Debug.Log($"Started moving {gameObject.name}");
}
public void OnMoveEnd(GameObject gameObject)
{
Debug.Log($"Finished moving {gameObject.name}");
}
public void OnSelect(GameObject gameObject)
{
Debug.Log($"{gameObject.name} selected");
}
public void OnDeselect(GameObject gameObject)
{
Debug.Log($"{gameObject.name} deselected");
}
public void OnBuildingOver(GameObject gameObject)
{
Debug.Log($"{gameObject.name} over");
}
public void OnBuildingExit(GameObject gameObject)
{
Debug.Log($"{gameObject.name} exit");
}
public void OnTimerStart(GameObject gameObject)
{
Debug.Log($"Timer started on temp object {gameObject.name}");
}
public void OnTimerEnd()
{
Debug.Log("Timer ended");
}
public void OnPreview(GameObject prefab)
{
Debug.Log($"This {prefab} holds a copy of the prefab to place");
}
}