Xenko for Unity® developers

Xenko and Unity® both use C# and share many concepts, with a few major differences.

Xenko for Unity® developers

Editor

The Xenko editor is Game Studio. This is the equivalent of the Unity® Editor.

Xenko and Unity® interface comparison

Unity® screenshot taken from Calling a web-service from a Unity3D scene by Kean Walmsley.

You can customize the Game Studio layout by dragging tabs, similar to Visual Studio.

For more information about Game Studio, see the Game Studio page.

Terminology

Unity® and Xenko use mostly common terms, with a few differences:

Unity®Xenko
Hierarchy panelEntity Tree
InspectorProperty Grid
Project browserAsset View
Scene viewScene Editor
GameObjectEntity
MonoBehaviourSyncScript, AsyncScript, StartupScript

Folders and files

Like Unity®, Xenko projects are stored in a directory that contains:

  • the project .sln solution file, which you can open with Game Studio or any IDE such as Visual Studio

  • a MyGame.Game folder with project source files, dependencies, resources, configurations, and binaries

Package folder structure

  • Assets contains the asset files which represent elements in your game.

  • Bin contains the compiled binaries and data. Xenko creates the folder when you build the project, with a subdirectory for each platform.

  • MyPackage.Game contains your source code.

  • MyPackage.Platform contains additional code for the platforms your project supports. Game Studio creates folders for each platform (eg MyPackage.Windows, MyPackage.Linux, etc). These folders are usually small, and only contain the entry point of the program.

  • obj contains cached files. Game Studio creates this folder when you build your project. To force a complete asset and code rebuild, delete this folder and build the project again.

  • Resources is a suggested location for files such as images and audio files used by your assets.

Xenko and Unity® differ in the following ways:

  • Xenko doesn't automatically copy resource files to your project folder when you import them into assets. You have to do this yourself. We recommend you save them in the Resources folder.

  • Xenko doesn't require resource files and asset files to be in the same folder. You can save resource files in the Assets folder if you want, but instead we recommend you save them in the Resources folder. This makes sharing your project via version control easier.

For more information about project structure in Xenko, including advice about how to organize and share your files, see the Project structure page.

Open the project directory from Game Studio

You can open the project directory from Project > Show in explorer in Game Studio.

Open project directory from Game Studio

Game settings

Unity® saves global settings in separate assets (ie Graphics Settings, Quality Settings, Audio Manager, and so on).

Xenko saves global settings in a single asset, the Game Settings asset. You can configure:

  • The default scene
  • Rendering settings
  • Editor settings
  • Texture settings
  • Physics settings
  • OverridesTo use the Game Settings asset, in the Asset View, select GameSettings and view its properties in the Property Grid.

Game settings

Scenes

Like Unity®, in Xenko you place all objects in a scene. Game Studio stores scenes as separate .xkscene assets in your project directory.

Set the default scene

You can have multiple scenes in your project. Xenko loads the default scene at runtime.

To set the default scene:

  • In the GameSettings properties, next to Default Scene, click Hand icon (Select an asset).

Set default scene

The Select an asset window opens.

  • Select the default scene and click OK.

For more information about scenes, see Scenes.

Entities vs GameObjects

In Unity®, objects in the scene are called GameObjects. In Xenko, they're called entities.

Entities in Xenko

Like GameObjects, entities are carriers for components such as transform components, model components, audio components, and so on. If you're used to working with GameObjects in Unity®, you should have no problem using entities in Game Studio.

Entity components

In Xenko, you add components to entities just like you add components to GameObjects in Unity®.

To add a component to entity in Game Studio:

  • Select the entity you want to add the component to.
  • In the Property Grid (on the right by default), click Add component and select the component from the drop-down list.

Add component

Transform component

Like GameObjects in Unity®, each entity in Xenko has a Transform component which sets its position, rotation, and scale in the world.

Transform component

Even empty entities have a Transform component, because every entity in the scene must have a position.

Assets

In Unity®, you select an asset in the project browser and edit its properties in the Inspector tab.

Xenko is similar. You select an asset in the Asset View and edit its properties in the Property Grid.

Asset and properties

For certain types of asset, Game Studio also has dedicated editors:

  • prefabs
  • scenes
  • sprite sheets
  • UI pages
  • UI libraries
  • scriptsTo open the dedicated editor for these types of asset:

  • double-click the asset, or

  • right-click the asset and select Edit asset, or
  • select the asset and type Ctrl + EnterThe editor opens in a new tab. You can arrange the tabs how you like, or float them as separate windows, just like tabs in web browsers.

Dedicated Xenko editors

Note

When you modify resource files outside Game Studio, the corresponding assets update automatically in Game Studio.

Import assets

To import an asset, drag it from Explorer to the Asset View. You can also click an Add asset button, navigate to the desired file and specify the type of asset you want to import.

As soon as you add an asset to your project, you can edit its properties in the Property Grid.

Add asset

Note

Unlike Unity®, Xenko doesn't automatically copy resource files to the project directory when you import them to projects.

Supported file formats

Like Unity®, Xenko supports file formats including:

Asset typeSupported formats
Models, animations, skeletons.dae, .3ds, obj, .blend, .x, .md2, .md3, .dxf, .fbx
Sprites, textures, skyboxes.dds, .jpg, .jpeg, .png, .gif, .bmp, .tga, .psd, .tif, .tiff
Audio.wav, .mp3, .ogg, .aac, .aiff, .flac, .m4a, .wma, .mpc
Fonts.ttf, .otf

For more information about assets, see Assets.

Note
  • Xenko currently doesn't support movie files.

Prefabs

Like Unity®, Xenko uses prefabs. Prefabs are "master" versions of objects that you can reuse wherever you need. When you change a prefab, every instance of the prefab changes too.

Prefabs in Xenko

Unlike Unity®, in Xenko, you can also add prefabs to other prefabs. These are called nested prefabs. If you modify a nested prefab, all the dependent prefabs inherit the change automatically.

For example, imagine you create a Vehicle prefab with acceleration, braking, steering, and so on. Then you nest the Vehicle prefab inside prefabs of different types of vehicles: a taxi, bus,truck, etc. If you adjust a property in the Vehicle prefab, the changes are inherited by all other prefabs. For example, if you increase the Acceleration property in the Vehicle prefab, the acceleration property in the taxi, bus and truck prefabs also increase.

To do this in Unity®, you have to create separate prefabs for each vehicle type and modify their acceleration parameters one by one.

For more information about using prefabs in Xenko, see Prefabs.

Archetypes

Archetypes are master assets that control the properties of assets you derive from them. Derived assets are useful when you want to create a "remixed" version of an asset. This is similar to prefabs.

For example, imagine we have three sphere entities that share a material asset named Metal. Now imagine we want to change the color of only one sphere, but keep its other properties the same. We could duplicate the material asset, change its color, and then apply the new asset to only one sphere. But if we later want to change a different property across all the spheres, we have to modify both assets. This is time-consuming and leaves room for mistakes.

The better approach is to derive a new asset from the archetype. The derived asset inherits properties from the archetype and lets you override individual properties where you need them. For example, we can derive the sphere's material asset and override its color. Then, if we change the gloss of the archetype, the gloss of all three spheres changes.

Create derived asset

You can derive an asset from an archetype, then in turn derive another asset from that derived asset. This way you can create different layers of assets to keep your project organized:

  1. Archetype
  2. Derived asset
  3. Derived asset

For more information about archetypes, see Archetypes.

Input

Xenko supports a variety of inputs. The code samples below demonstrate the difference in input code between Xenko and Unity®.

For more information about Input in Xenko, see Input.

Unity

  1. void Update()
  2. {
  3. // true for one frame in which the space bar was pressed
  4. if(Input.GetKeyDown(KeyCode.Space))
  5. {
  6. // Do something.
  7. }
  8. // true while this joystick button is down
  9. if (Input.GetButton("joystick button 0"))
  10. {
  11. // Do something.
  12. }
  13. float Horiz = Input.GetAxis("Horizontal");
  14. float Vert = Input.GetAxis("Vertical");
  15. //Do something else.
  16. }

Physics

Just like Unity®, Xenko has three types of collider:

  • static colliders
  • rigidbodies
  • charactersThey're controlled from scripts in slightly different ways.

Kinematic rigidbodies

Unity®

  1. public Rigidbody rigidBody;
  2. void Start()
  3. {
  4. rigidBody = GetComponent<Rigidbody>();
  5. }
  6. void EnableRagdoll()
  7. {
  8. rigidBody.isKinematic = false;
  9. rigidBody.detectCollisions = true;
  10. }
  11. void DisableRagdoll()
  12. {
  13. rigidBody.isKinematic = true;
  14. rigidBody.detectCollisions = false;
  15. }

Xenko

  1. public class KinematicX : SyncScript
  2. {
  3. public RigidbodyComponent component;
  4. public override void Start()
  5. {
  6. // Initialization of the script.
  7. component = Entity.Get<RigidbodyComponent>();
  8. }
  9. public override void Update()
  10. {
  11. }
  12. public void EnableRagdoll()
  13. {
  14. component.IsKinematic = false;
  15. component.ProcessCollisions = true;
  16. }
  17. public void DisableRagdoll()
  18. {
  19. component.IsKinematic = true;
  20. component.ProcessCollisions = false;
  21. }
  22. }

For more information about rigidbodies in Xenko, see Rigidbodies.

Triggers

Unity®

  1. // When game object collides with the trigger.
  2. void OnTriggerEnter(Collider Other)
  3. {
  4. Other.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
  5. }
  6. //When game object exits collider space.
  7. void OnTriggerExit(Collider Other)
  8. {
  9. Other.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
  10. }

Xenko

  1. var trigger = Entity.Get<PhysicsComponent>();
  2. trigger.ProcessCollisions = true;
  3. // Start state machine.
  4. while (Game.IsRunning)
  5. {
  6. // 1. Wait for an entity to collide with the trigger.
  7. var firstCollision = await trigger.NewCollision();
  8. var otherCollider = trigger == firstCollision.ColliderA
  9. ? firstCollision.ColliderB
  10. : firstCollision.ColliderA;
  11. otherCollider.Entity.Transform.Scale = new Vector3(2.0f, 2.0f, 2.0f);
  12. // 2. Wait for the entity to exit the trigger.
  13. Collision collision;
  14. do
  15. {
  16. collision = await trigger.CollisionEnded();
  17. }
  18. while (collision != firstCollision);
  19. otherCollider.Entity.Transform.Scale = new Vector3(1.0f, 1.0f, 1.0f);
  20. }

For more information about triggers in Xenko, see Triggers

Raycasting

Unity®

  1. Collider FindGOCameraIsLookingAt()
  2. {
  3. int distance = 50;
  4. // Cast a ray and set it to the mouse cursor position in the game
  5. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  6. RaycastHit hit;
  7. if (Physics.Raycast(ray, out hit, distance))
  8. {
  9. // Draw invisible ray cast/vector
  10. Debug.DrawLine(ray.origin, hit.point);
  11. // Log hit area to the console
  12. Debug.Log(hit.point);
  13. return hit.collider;
  14. }
  15. return null;
  16. }

Xenko

  1. public static PhysicsComponent ScreenPositionToWorldPositionRaycast(Vector2 screenPos, CameraComponent camera, Simulation simulation)
  2. {
  3. Matrix invViewProj = Matrix.Invert(camera.ViewProjectionMatrix);
  4. Vector3 sPos;
  5. sPos.X = screenPos.X * 2f - 1f;
  6. sPos.Y = 1f - screenPos.Y * 2f;
  7. sPos.Z = 0f;
  8. var vectorNear = Vector3.Transform(sPos, invViewProj);
  9. vectorNear /= vectorNear.W;
  10. sPos.Z = 1f;
  11. var vectorFar = Vector3.Transform(sPos, invViewProj);
  12. vectorFar /= vectorFar.W;
  13. var result = simulation.Raycast(vectorNear.XYZ(), vectorFar.XYZ());
  14. return result.Succeeded;
  15. }

For more information about Raycasting in Xenko, see Raycasting.

Scripts

Xenko saves scripts in a subfolder in the MyGame.Game folder in the project directory.

To open a script in the Game Studio script editor, double-click it in the Asset View. The script editor has syntax highlighting, auto-completion, and live diagnostics.

Xenko script editor

You can also edit scripts in other IDEs, such as Visual Studio. When you edit a script in an external IDE, Xenko reloads them automatically.

If you install the Visual Studio plug-in during the Xenko installation, you can open your project in Visual Studio from Game Studio. To do this, in the Game Studio toolbar, click Open in IDE.

Open project in Visual Studio

Alternatively, right-click the script in the Asset View and click Open asset file:

Open asset file

Event functions (Start, Update, Execute, etc)

In Unity®, you work with MonoBehaviours with Start(), Update(), and other methods.

Instead of MonoBehaviours, Xenko has three types of scripts: SyncScript, AsyncScript, StartupScript. For more information, see Types of script.

Unity® MonoBehaviour

  1. public class BasicMethods : MonoBehaviour
  2. {
  3. void Start() { }
  4. void OnDestroy() { }
  5. void Update() { }
  6. }

Xenko SyncScript

  1. public class BasicMethods : SyncScript
  2. {
  3. public override void Start() { }
  4. public override void Cancel() { }
  5. public override void Update() { }
  6. }

Xenko AsyncScript

  1. public class BasicMethods : AsyncScript
  2. {
  3. // Declared public member fields and properties that will appear in the game studio
  4. public override async Task Execute()
  5. {
  6. while(Game.IsRunning)
  7. {
  8. // Do stuff every new frame
  9. await Script.NextFrame();
  10. }
  11. }
  12. public override void Cancel()
  13. {
  14. // Cleanup of the script
  15. }
  16. }

Xenko StartupScript

  1. public class BasicMethods : StartupScript
  2. {
  3. // Declared public member fields and properties that will appear in the game studio
  4. public override void Start()
  5. {
  6. // Initialization of the script
  7. }
  8. public override void Cancel()
  9. {
  10. // Cleanup of the script
  11. }
  12. }

Script components

Like Unity®, in Xenko, you attach scripts to entities by adding them as script components.

Create a script

To create a script, click Add asset button and select Scripts.

Create script in Xenko

In Unity®, when you create a MonoBehaviour script, it has two base functions: Start() and Update(). Xenko has a SyncScript that works similarly. Like MonoBehaviour, SyncScript has two methods:

  • Start() is called when it the script is loaded.

  • Update() is called every update.

Unlike MonoBehaviour, you have to use Update() method in every SyncScript, or your code won't work properly.

If you want your script to be a startup or asynchronous, use the corresponding script types:

  • StartupScript: this script has a single Start() method. It initializes the scene and its content at startup.

  • AsyncScript: an asynchronous script with a single method Execute() and you can use async/await inside that method. Asynchronous scripts aren't loaded one by one like synchronous scripts. Instead, they're all loaded in parallel.

Reload assemblies

Unlike Unity®, after you create a script, you have to reload the assemblies manually. To do this, click Reload assemblies in the Game Studio toolbar.

Reload assemblies

Add scripts to entities

  • In the Entity Tree (on the left by default), or in the scene, select the entity you want to add the script to.

Select an entity

  • In the Property Grid (on the right by default), click Add component and select the script you want to add.

Add script component

In Unity®, script components are grouped under Components > Scripts. In Xenko, scripts are not grouped. Instead, Game Studio lists them alphabetically with other components.

For more information about adding scripts in Xenko, see Use a script.

Scripting gameplay

Unity® and Xenko both use C#. However, scripting gameplay in Xenko is a little different from Unity®.

Instantiate Entity / GameObject

In Unity®, you use Instantiate to create new object instances. This function makes a copy of UnityEngine.Object and spawns it to the scene.

Unity®

  1. public GameObject CarPrefab;
  2. public Vector3 SpawnPosition;
  3. public Quaternion SpawnRotation;
  4. void Start()
  5. {
  6. GameObject NewGO = (GameObject)Instantiate(CarPrefab, SpawnPosition, SpawnRotation);
  7. NewGO.name = "NewGameObject1";
  8. }

Xenko

In Xenko, you can instantiate Entities similarly to Unity® GameObjects:

  1. // Declared public member fields and properties displayed in the Game Studio Property Grid.
  2. public Prefab CarPrefab;
  3. public Vector3 SpawnPosition;
  4. public Quaternion SpawnRotation;
  5. public override void Start()
  6. {
  7. // Initialization of the script.
  8. var car = CarPrefab.Instantiate();
  9. SceneSystem.SceneInstance.RootScene.Entities.AddRange(car);
  10. car.First().Transform.Position = SpawnPosition;
  11. car.First().Transform.Rotation = SpawnRotation;
  12. car.First().Name = "MyNewEntity";
  13. }

Use default values

Each class in Unity® has certain default values. If you don't override these properties in the script, the default values will be used. This works the same in Xenko:

Unity®

  1. public int NewProp = 30;
  2. public Light MyLightComp = null;
  3. void Start()
  4. {
  5. // Create the light component if we don't already have one.
  6. if (MyLightComp == null)
  7. {
  8. MyLightComp = gameObject.AddComponent<Light>();
  9. MyLightComp.intensity = 3;
  10. }
  11. }

Xenko

  1. // Declared public member fields and properties displayed in the Game Studio Property Grid.
  2. public int NewProp = 30;
  3. public LightComponent MyLightComponent = null;
  4. public override void Start()
  5. {
  6. // Create the light component if we don't already have one.
  7. if (MyLightComponent == null)
  8. {
  9. MyLightComponent = new LightComponent();
  10. MyLightComponent.Intensity = 3;
  11. Entity.Add(MyLightComponent);
  12. }
  13. }

Disable GameObject/entity

Unity®

  1. MyGameObject.SetActive(false);

Xenko

  1. Entity.EnableAll(false, true);

Access component from GameObject/entity

Unity®

  1. Light lightComponent = GetComponent<Light>();

Xenko

  1. LightComponent lightComponent = Entity.Get<LightComponent>();

Access GameObject/entity from component

Unity®

  1. GameObject ParentGO = lightComponent.gameObject;

Xenko

  1. Entity ParentEntity = lightComponent.Entity;

Log output

To see the output, in the Game Studio toolbar, under View, enable Output.

Enable output

Game Studio displays in the Output tab (at the bottom of Game Studio by default).

Output tab

Print debug messages

To print to the Visual Studio output, use:

  1. System.Diagnostics.Debug.WriteLine("hello");
Note

To print debug messages, you have to run the game from Visual Studio, not Game Studio. There's no way to print to the Game Studio output window.


Unity® is a trademark of Unity Technologies.