游戏/应用集成
为您的应用或游戏添加 SignalRGB 支持既快速又简单。如果您的项目可以发送 HTTP POST 请求,它就可以利用 SignalRGB 广泛兼容的设备范围。
您的应用或游戏可以通过两种主要方式与 SignalRGB 集成:触发预先存在的效果或与您开发的自定义效果通信。第一种选项所需的开发时间要少得多,但仅限于我们预制效果库。第二种方法可能需要更多时间,但它为您可以创建的效果类型和数量提供了无限可能。
最终,由您作为开发者决定哪种方法最适合您的项目。但是,两种方法都可以提供出色的灯光体验。
如果您不想自己开发灯光效果,您可以利用我们丰富的预制效果库。使用 SignalRGB 应用 URL,您可以轻松安装和激活可从 SignalRGB 下载的任何公共灯光效果。
建议您在应用或游戏首次启动时安装将要激活的灯光效果,以确保所有效果能快速触发。
以下 API 路由可用于协助此操作:
| 请求 URI | 目的 |
|---|---|
| signalrgb://effect/install/EffectName | 安装名为 EffectName 的灯光效果 |
| signalrgb://effect/apply/EffectName | 激活名为 EffectName 的灯光效果 |
如果您选择的引擎或语言无法与自定义 URL 协议处理程序通信,您仍然可以使用 SignalRGB API 的这部分。只需使用 --url 参数启动位于 C:\Users\<username>\AppData\Local\VortxEngine 中的 SignalRgbLauncher.exe 可执行文件,并将其设置为您想使用的 API 路由路径。要防止应用被带到前台,您可以添加 ?-silentlaunch-。
例如,要安装名为 Azure 的效果,您将使用以下参数启动可执行文件:
SignalRgbLauncher.exe --url=effect/apply/Azure?-silentlaunch-
Unity
Unreal Engine
Section titled “Unreal Engine”以下 C++ 类可与 Unreal Engine 一起使用以安装和激活任何 SignalRGB 效果。
#include "SignalEffectApi.h"
void SignalEffectApi::ApplyEffect(const std::string& effect) { const TCHAR* appdataPath = FPlatformProcess::UserSettingsDir(); const FString exePath = FString(appdataPath) + TEXT("\\VortxEngine\\SignalRgbLauncher.exe"); const FString params = TEXT("--url=effect/apply/") + FString(effect.c_str()) + TEXT("?-silentlaunch-"); FPlatformProcess::CreateProc(*exePath, *params, true, false, false, nullptr, 0, nullptr, nullptr);}
void SignalEffectApi::InstallEffect(const std::string& effect) { const TCHAR* appdataPath = FPlatformProcess::UserSettingsDir(); const FString exePath = FString(appdataPath) + TEXT("\\VortxEngine\\SignalRgbLauncher.exe"); const FString params = TEXT("--url=effect/install/") + FString(effect.c_str()) + TEXT("?-silentlaunch-"); FPlatformProcess::CreateProc(*exePath, *params, true, false, false, nullptr, 0, nullptr, nullptr);}#pragma once
#include "CoreMinimal.h"
class SignalEffectApi{public: void ApplyEffect(const std::string& effect); void InstallEffect(const std::string& effect);};建议您在应用启动时安装任何您希望使用的效果,以便快速触发效果。
以下示例展示了如何使用 SignalEffectApi 类安装和激活效果。
#include "SignalEffectApi.h"
... // Initialize effect API. SignalEffectApi srgbEffect;
// Set the name of the effect you wish to install. std::string effectName = "Azure";
// Install the effect. srgbEffect.InstallEffect(effectName);
// Apply the effect. srgbEffect.ApplyEffect(effectName);
...自定义效果
使用 HTTP 请求直接与 SignalRGB 通信一旦了解请求的结构就很容易完成。请求的结构如下:
请求类型: POST
请求 URI: http://localhost:16034/canvas/event?sender=your-app-name&event=your-event
您应用发送的所有命令都应使用相同的 sender 变量。event 变量是传递给 SignalRGB 的数据应放置的位置。
在效果内部,所有数据通过 onCanvasApiEvent 函数发送。使用此函数来处理您的效果如何响应 HTTP 请求。
此基本示例展示了 Unity 或 Unreal Engine 游戏如何与 SignalRGB 通信以触发自定义灯光效果中的事件。但是,任何发送 HTTP 请求的方法都可以用于控制自定义灯光效果。这种灵活性意味着几乎可以为任何游戏或应用添加 SignalRGB 支持。
所有发送到 API 路由的事件都通过 onCanvasApiEvent 函数传递给灯光效果。有许多管理事件的方法,但以下示例保持简单易懂。如果您正在开发具有多个事件的更复杂内容,请考虑使用状态处理程序方法来跟踪事件及其相应的灯光效果。
<head> <title>CustomApiEffect</title> <meta description="Effect description goes here." /> <meta publisher="Your Name Here" /></head>
<body style="margin: 0; padding: 0;"> <canvas id="exCanvas" width="320" height="200"></canvas></body>
<script> var c = document.getElementById("exCanvas"); var ctx = c.getContext("2d"); var width = 320; var height = 200;
var activeEvent = ""; // Variable to store most recent event
function onCanvasApiEvent(apiEvent) { if (apiEvent["sender"] == "apiTestApp") { // Only respond to API events with the sender "apiTestApp". activeEvent = apiEvent["event"]; // Set the activeEvent variable to the most recent API event data. } }
function update() { if (activeEvent == "allRed") { // Turn the canvas all red in response to an allRed event ctx.fillStyle = '#FF0000'; ctx.fillRect(0, 0, width, height); } else { // Otherwise, make the canvas all black ctx.fillStyle = '#000000'; ctx.fillRect(0, 0, width, height); }
window.requestAnimationFrame(update); }
window.requestAnimationFrame(update);</script>以下函数将允许 Unity 与 SignalRGB 通信。但是,任何发送 HTTP POST 请求的方法都可以使用。
using UnityEngine;using UnityEngine.Networking;using System.Collections;using System.Collections.Generic;
public class SendAllRedEventBehavior : MonoBehaviour{ void Start() { StartCoroutine(Upload()); }
IEnumerator Upload() { UnityWebRequest www = UnityWebRequest.Post("http://localhost:16034/canvas/event?sender=apiTestApp&event=allRed"); yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { Debug.Log("Sent lighting event to SignalRGB!"); } }}Unreal Engine
Section titled “Unreal Engine”以下 C++ 类可与 Unreal Engine 一起使用,通过 SignalRGB API 启用通信。确保将请求 URI 的 sender 参数更改为与您的 Lightscript 使用的内容匹配。
#include "SignalApi.h"
void SignalApi::SendEvent(const std::string& sender, const std::string& event) { Http = &FHttpModule::Get(); TSharedRef<IHttpRequest> Request = Http->CreateRequest(); std::string requestUri = "http://localhost:16034/canvas/event?sender="+ sender + "&event=" + event; Request->SetURL(requestUri.c_str()); Request->SetVerb("GET"); Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent"); Request->SetHeader("Content-Type", TEXT("application/json")); Request->ProcessRequest();}#pragma once
#include "CoreMinimal.h"#include "Runtime/Online/HTTP/Public/Http.h"
class SignalApi{public: FHttpModule* Http; void SendEvent(const std::string& sender,const std::string& event);};要在游戏代码中触发灯光效果,只需调用 SendEvent 函数,指定您想要触发的事件名称,以及您的应用或游戏用于向 SignalRGB 标识自身的名称。
#include "SignalApi.h"
... // Set the sender variable. const std::string SAPI_APPNAME = "apiTestGame"; // Initialize SignalApi class. SignalApi sapi; // Set the name of the event. std::string eventName = "weaponFired"; // Send API event to SignalRGB. sapi.SendEvent(SAPI_APPNAME, eventName);
...using UnrealBuildTool;
public class SignalApiTest : ModuleRules{ public SignalApiTest(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "EnhancedInput", "Http" }); // you MUST add Http to this array or the build will fail! }}