Files
velopack/samples/CSharpUnityMono
Caelan 3128d34b14 Rework solution structure to allow more types of library projects (#196)
* Rename avalonia & wpf samples

* Rename rest of samples

* fix sample readme

* Fix compat util tests

* rename / move all src projects

* remove package lock files and move libC# again

* Add rust lib and cargo workspace

* Replace locksmith lib with new filelocksmith-rs library

* Deprecated type

* fix setup compile

* Use thiserror for error handling

* Rename some enums and formatting

* Add missing SHA256

* wip c++ library

* cpp wip

* alphabetize readme

* Try to get build working again

* Fix some conditionally compiled bits

* cross config should be in workspace root

* Fix PathHelper for new rust target dir

* Missed one old path to velopack.csproj

* remove obsolete coverage code

* testawareapp.exe no longer exists
2024-08-06 22:56:40 +01:00
..

UnitySample(Mono)

This project integrates Velopack into unity by way of a nuget for Unity package.

How to use

  1. Clone this repository
  2. Open the project in Unity
  3. Build the project to \Build folder
  4. Open BuildVisualStudioSolution\UnityMonoSample.sln
  5. Build the solution
  6. copy BuildVisualStudioSolution\build\bin\x64\Master\UnityMonoSample.exe to \Build folder
  7. use vpk cli tool to pack your project vpk pack -u UnityMonoSample -v 0.0.1 -p .\Build -e UnityMonoSample.exe

Requirements for your project

install the nuget package VelocyPack in your project you can use tool like NuGetForUnity

openupm add com.github-glitchenzo.nugetforunity

if your .net runtime is .NET Standard 2.0, you need use Velopack 0.0.359

for .NET 4.x runtime, you can use the latest version of Velopack

currently, the Velopack is only support for Unity mono runtime, not support for IL2CPP, due to the following reasons:

https://docs.unity3d.com/2022.3/Documentation/Manual/ScriptingRestrictions.html

System.Diagnostics.Process API

IL2CPP doesnt support the System.Diagnostics.Process API methods. For cases where this is required on desktop platforms, use the Mono scripting backend.


If you still want to use Velopack in IL2CPP, you can use the following workaround

(remember that this is not recommended and may change in the future):

  1. clone the Velopack repository
  2. edit the VelopackRuntimeInfo.cs file like this:
        static VelopackRuntimeInfo()
        {
            //delete the following line
            //EntryExePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            EntryExePath = Application.dataPath;
            if (!Application.isEditor)
            {
                EntryExePath = Path.Combine(EntryExePath, "..", Application.productName + ".exe");
            }
        }
  1. build the Velopack project
  2. replace the Velopack.dll in your project with the new Velopack.dll

FAQ

Why do I need to build the project twice?

The first build is to generate the normal Unity project.

The second build is to generate the .exe file that Velopack needs to pack. Since the Unity executable on Windows is packaged in C++, its process needs some modification to integrate Velopack's App Hooks. you can see the following code in the Main.cpp file

#include "PrecompiledHeader.h"
#include "..\UnityPlayerStub\Exports.h"

//for Velopack
#include "..\Velopack.hpp"
#include "Windows.h"
#include <shellapi.h>
//for Velopack

// Hint that the discrete gpu should be enabled on optimus/enduro systems
// NVIDIA docs: http://developer.download.nvidia.com/devzone/devcenter/gamegraphics/files/OptimusRenderingPolicies.pdf
// AMD forum post: http://devgurus.amd.com/thread/169965
extern "C"
{
    __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
    __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
{
    //for Velopack
    int pNumArgs = 0;
    wchar_t** args = CommandLineToArgvW(lpCmdLine, &pNumArgs);
    Velopack::startup(args, pNumArgs);
    //for Velopack
    return UnityMain(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
}

Note

Do not use ApplyUpdatesAndExit because Environment.Exit is not supported. Instead, use the following to apply update:

updateManager.WaitExitThenApplyUpdates(...);
UnityEngine.Application.Quit();

This will work because WaitExitThenApplyUpdates will not call Environment.Exit, but it will prepare up the Velopack updater to install the update when unity closes.