From 17b0ac3e395f5f6e1aab41cb912091547eb4e686 Mon Sep 17 00:00:00 2001 From: AyakorK Date: Fri, 18 Oct 2024 10:14:28 +0200 Subject: [PATCH] chore: Add environment variables to UnityProject --- .gitignore | 4 ++++ Assets/Scripts/PlayerController.cs | 5 ++++- Assets/Scripts/Trap/TrapManager.cs | 6 +++++- Packages/manifest.json | 1 + Packages/packages-lock.json | 7 +++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7fbba8f6..33aba243 100644 --- a/.gitignore +++ b/.gitignore @@ -290,4 +290,8 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk + +# Environment file +.env + # End of https://www.toptal.com/developers/gitignore/api/macos,windows,linux,unity,intellij+all,clion+all \ No newline at end of file diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index a53e9c98..8dfb2f64 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -3,6 +3,7 @@ using System.Text; using UnityEngine; using SocketIOClient; +using CandyCoded.env; public class PlayerMovement : MonoBehaviour { @@ -23,11 +24,13 @@ public class PlayerMovement : MonoBehaviour private Vector2 previousMovement = Vector2.zero; + private string socketUrl; async void Start() { try { - var uri = new Uri("http://localhost:3000"); + env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); + var uri = new Uri(socketUrl); clientSocket = new SocketIOUnity(uri); await clientSocket.ConnectAsync(); diff --git a/Assets/Scripts/Trap/TrapManager.cs b/Assets/Scripts/Trap/TrapManager.cs index c988f1c4..32014a38 100644 --- a/Assets/Scripts/Trap/TrapManager.cs +++ b/Assets/Scripts/Trap/TrapManager.cs @@ -4,6 +4,7 @@ using UnityEngine; using SocketIOClient; using Newtonsoft.Json.Linq; +using CandyCoded.env; public class TrapManager : MonoBehaviour { @@ -16,6 +17,8 @@ public class TrapManager : MonoBehaviour private Queue placementQueue = new Queue(); private SocketIOUnity clientSocket; // SocketIO client + private string socketUrl; + void Start() { // Initialize the grid @@ -35,7 +38,8 @@ async void SetupSocket() { try { - var uri = new Uri("https://d4cc-86-245-196-157.ngrok-free.app"); // Replace with your backend URL + env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); + var uri = new Uri(socketUrl); // Replace with your backend URL clientSocket = new SocketIOUnity(uri); // Connect to the server diff --git a/Packages/manifest.json b/Packages/manifest.json index 62cd7506..faeceae4 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -6,6 +6,7 @@ "com.unity.2d.tilemap": "1.0.0", "com.unity.nuget.newtonsoft-json": "3.2.1", "com.unity.ugui": "1.0.0", + "xyz.candycoded.env": "https://github.com/CandyCoded/env.git#v1.1.0", "com.unity.modules.ai": "1.0.0", "com.unity.modules.androidjni": "1.0.0", "com.unity.modules.animation": "1.0.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index b96bc9d6..4e20f47f 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -111,6 +111,13 @@ "com.unity.modules.imgui": "1.0.0" } }, + "xyz.candycoded.env": { + "version": "https://github.com/CandyCoded/env.git#v1.1.0", + "depth": 0, + "source": "git", + "dependencies": {}, + "hash": "c6951bc02488dbe99f48509a9e289ee73c74acda" + }, "com.unity.modules.ai": { "version": "1.0.0", "depth": 0,