Introduction to UrhoSharp in Xamarin Forms

UrhoSharp.Forms is a Xamarin Forms component that enables cross platform development using Urho3D, a lightweight and powerful 3D Game Engine. UrhoSharp.Forms uses the UrhoSharp Xamarin Libraries, which are bindings for the community based Urho3D project.

urhosharp

Getting Started

UrhoSharp supports iOS, Android and UWP. Xamarin has provided a UrhoSharp Template that you can use, however I am going to start from the ground, and/or the basis of extending an existing Xamarin Forms application.

  1. Create a regular Xamarin Forms portable project.
  2. Install UrhoSharp.Forms in your platform and PCL projects.

Demo Project: If you want to follow along I have a completed UrhoSharp.Demo project.

UrhoSurface

The UrhoSurface is the Xamarin.Forms view that houses the Urho3D content. Now we will add a UrhoSurface to our MainPage. In MainPage.xaml.cs add the following.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:UrhoSharp.Demo"
             xmlns:urho="clr-namespace:Urho.Forms;assembly=Urho.Forms"
             x:Class="UrhoSharp.Demo.MainPage">

    <urho:UrhoSurface x:Name="HelloWorldUrhoSurface" VerticalOptions="FillAndExpand" />

</ContentPage>

Urho Application

We must now create an Application to put inside our UrhoSurface. Create a new class called HelloWorld.cs in the portable library.

 public class HelloWorld: Urho.Application
 {
 }

Assign this to the Surface by going back to your MainPage.xaml.cs and OnAppearing, call the Show method.

protected override async void OnAppearing()
{
    base.OnAppearing();

    await HelloWorldUrhoSurface.Show<HelloWorld>(new Urho.ApplicationOptions(assetsFolder: null));
}

The application loads in the UrhoSurface but your application has nothing in it. We are now going to add some text to the application.

UI Root Elements

In your HelloWorld.cs you override the Start method and create a new function to show some text.

public class HelloWorld : Application
{
    protected override void Start()
    {
         base.Start();
         CreateText();
    }

    private void CreateText()
    {
        // Create Text Element
        var text = new Text()
        {
            Value = "Hello World!",
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };

        text.SetColor(Color.Cyan);
        text.SetFont(font: ResourceCache.GetFont("Fonts/Anonymous Pro.ttf"), size: 30);
        // Add to UI Root
        UI.Root.AddChild(text);
    }
}

 

helloworld

Continuing On

We finally made the most basic app possible with UrhoSharp.Forms. Moving on to 3D objects, we have to create a Scene, load components and perform action. In my post UrhoSharp 3D Moving Object, I go step by step through creating a spinning pyramid.

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer |
Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

Leave A Comment?