INSA
  • Tutorials
  • Documentation
  • Api Documentation
  • Changelog
Show / Hide Table of Contents
  • Getting Started
    • Quickstart
  • Unity tutorial
    • Introduction
    • Time for practice
    • Restricted scenario by script?
    • How to create the tutorial scene?
  • Scenario
    • How to create a sensor?
    • How to create an effector?
    • How to setup the engine?
    • How to edit a scenario?
    • How to run a scenario?
    • How to?
  • Relation engine
    • How to create a type?
    • How to create a relation?
    • What are queries? How do queries work?
    • How to be notified when a relation is executed?
  • Highlight Manager
    • How to create a Highlight?

How to create a Type ?

A Type describe a behavior of an object as a unique entity. It is characterized by a script inheriting from XUType and has to be attached as a component on the object.

To show you how to create a type, we will take the example of types present in the tutorial.

The first example is TypeColored. This type is characterized by its color and its capacity to change color.

If we take the object of the User represented by the circle, le script component looks like this :

TypeColored

Code explained

Now, let's look at the code!

First, any type must inherit from XUType

	using Xareus.Relations.Unity;
	public class TypeColored : XUType

Add any fields or properties you want! TypeColored need a public Color to be changed by the unity inspector and Boolean to check collision state

Note

A public field is used for debug purposes, hasBeenCollided should not be public and use a property instead but it is a simple way to see its state in the inspector.

	    //Color of the object
	    public Color color;

	    //If the object has been collided with
	    public bool hasBeenCollided = false;

XUType is a MonoBehavior, so you can use provided method like Start() to initialise your object. Here TypeColored apply the color selected in the inspector to the material of the object.

	    private void Start()
	    {
	        GetComponent<Renderer>().material.SetColor("_Color", color);
	    }

And other Monobehavior methods to detect collisions. Here we set hasBeenCollided to true or false and we make sure the collided object is an UFObject.

	    private void OnTriggerEnter(Collider other)
	    {
	        XUObject otherObject = other.GetComponent<XUObject>();
	        //Check if the collided object has an XUObject component attached
	        //We do not want any other collision to set hasBeenCollided to True
	        if (otherObject != null)
	        {
	            hasBeenCollided = true;
	        }
	    }

	    private void OnTriggerExit(Collider other)
	    {
	        XUObject otherObject = other.GetComponent<XUObject>();
	        //Check if the collided object have an XUObject component attached
	        //We do not want any other collision to set hasBeenCollided to False
	        if (otherObject != null)
	        {
	            hasBeenCollided = false;
	        }
	    }

Finally, you can add any methods you want. These methods can be called by all the relations involving a TypeColored.

Those two methods take the color of a GameObject and either swap the colors or mix the color between the current and the collided object.

	    /// <summary>
	    /// Swap the color of this object and the collided object
	    /// </summary>
	    /// <param name="collidedObject"></param>
	    public void SwapColor(GameObject collidedObject)
	    {
	        //Get the color of this object
	        Color actualColor = GetComponent<Renderer>().material.GetColor("_Color");
	        //Get the color of the collided object
	        Color collidedColor = collidedObject.GetComponent<Renderer>().material.GetColor("_Color");
	        //Set the collided color to this object
	        GetComponent<Renderer>().material.SetColor("_Color", collidedColor);
	        //Set this color to the collided object
	        collidedObject.GetComponent<Renderer>().material.SetColor("_Color", actualColor);
	    }

Full code

Here is the full code of TypeColored :

	using Xareus.Relations.Unity;

	public class TypeColored : XUType
	{
	    //Color of the object
	    public Color color;

	    //If the object has been collided
	    public bool hasBeenCollided = false;

	    private void Start()
	    {
	        GetComponent<Renderer>().material.SetColor("_Color", color);
	    }

	    private void OnTriggerEnter(Collider other)
	    {
	        XUObject otherObject = other.GetComponent<XUObject>();
	        //Check if the collided object have an XUObject component attached
	        //We do not want any other collision to set hasBeenCollided to True
	        if (otherObject != null)
	        {
	            hasBeenCollided = true;
	        }
	    }

	    private void OnTriggerExit(Collider other)
	    {
	        XUObject otherObject = other.GetComponent<XUObject>();
	        //Check if the collided object have an XUObject component attached
	        //We do not want any other collision to set hasBeenCollided to False
	        if (otherObject != null)
	        {
	            hasBeenCollided = false;
	        }
	    }

	    /// <summary>
	    /// Swap the color of this object and the collided object
	    /// </summary>
	    /// <param name="collidedObject"></param>
	    public void SwapColor(GameObject collidedObject)
	    {
	        //Get the color of this object
	        Color actualColor = GetComponent<Renderer>().material.GetColor("_Color");
	        //Get the color of the collided object
	        Color collidedColor = collidedObject.GetComponent<Renderer>().material.GetColor("_Color");
	        //Set the collided color to this object
	        GetComponent<Renderer>().material.SetColor("_Color", collidedColor);
	        //Set this color to the collided object
	        collidedObject.GetComponent<Renderer>().material.SetColor("_Color", actualColor);
	    }
	}

Empty types

Types can be used as a label of an object. It is mainly used for relations, so it can recognize a specific object.

Example of TypeUser:

	using Xareus.Relations.Unity;

	public class TypeUser : XUType
	{ }
INSA     IRISA     Inria     Ouest Valorisation Back to top