UrhoSharp from PowerShell

lahellerlaheller USMember ✭✭

Hi!

If somebody for some reason wants to use UrhoSharp from Windows PowerShell, then I created a sample script, how to do that.

# Set the correct parth here to Urho.dll assembly file
$libUrho = "C:\Users\held\.nuget\packages\urhosharp\1.8.93\lib\net45\Urho.dll"

Clear-Host

Add-Type -Path $libUrho #-PassThru

Set-Location -Path "$Env:USERPROFILE"

[Urho.Desktop.DesktopUrhoInitializer]::AssetsDirectory = $null

$ExceptionHandler = {
    # $args[0] -> Sender
    # $args[1] -> EventArgs
    Write-Warning -Message "$($args[1].Exception.Message)`n"
    $args[1].Handled = $true
}
[Urho.Application]::add_UnhandledException($ExceptionHandler)

$ActionStarted = {
    Write-Verbose -Message "Application has started!`n" -Verbose
    if ([Urho.Application]::HasCurrent) {
        # Obtain the instance of current existing SimpleApplication
        $app = [Urho.Application]::Current -as [Urho.SimpleApplication]

        # Change default settings
        $app.Zone.Enabled = $false

        $app.Viewport.SetClearColor([Urho.Color]::Black)
        $app.Viewport.RenderPath.Append([Urho.CoreAssets+PostProcess]::FXAA2)

        $app.RootNode.RemoveAllChildren()

        # Create one box and add it to the scene
        $box = [Urho.StaticModel]::new($app.Context)
        $app.RootNode.AddComponent($box)
        $box.Model = [Urho.CoreAssets+Models]::Box
        $mat = [Urho.CoreAssets+Materials]::DefaultGrey
        $box.Material = $mat
    }
}
[Urho.Application]::add_Started($ActionStarted)

$ActionStopped = {
    Write-Verbose -Message "Application has stopped!`n" -Verbose

    # Cleanup
    [Urho.Application]::remove_UnhandledException($ExceptionHandler)
    [Urho.Application]::remove_Started($ActionStarted)
    [Urho.Application]::remove_Stopped($ActionStopped)

    [gc]::Collect()
    [gc]::WaitForPendingFinalizers()
}
[Urho.Application]::add_Stopped($ActionStopped)

$opts = New-Object -TypeName Urho.ApplicationOptions -Property @{
    AutoloadCoreData = $true
    HighDpi = $true
    NoSound = $true
    Orientation = [Urho.ApplicationOptions+OrientationType]::LandscapeAndPortrait
    Height = 800
    Width = 1280
}
$app = [Urho.Application]::CreateInstance([Urho.SimpleApplication],$opts)

# At this point a new window with Scene appears and the rest of script is blocked until Esc is not pressed
$r1 = $app.Run()

# Execution continues here when Esc is pressed
$r2 = $app.Exit()

Save the above code to C:\UrhoSharp.ps1 file and from command line run following:

powershell.exe -ExecutionPolicy Unrestricted -File "C:\UrhoSharp.ps1"

The above example just displays one gray box in the scene.

BR,

Ladislav

Sign In or Register to comment.