Skip to content

Instantly share code, notes, and snippets.

View bartwttewaall's full-sized avatar

Bart Wttewaall bartwttewaall

View GitHub Profile
@bartwttewaall
bartwttewaall / CypherUtils.cs
Last active July 22, 2019 09:23
AES-256-CBC encryption / decryption with secret key and IV string with additional C# decrypt-only method
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class CypherUtils {
public static string Decrypt (string cipherData, string keyString, string ivString) {
byte[] key = Encoding.UTF8.GetBytes (keyString);
byte[] iv = Convert.FromBase64String (ivString);
@bartwttewaall
bartwttewaall / AWSDeploy.js
Last active July 22, 2019 09:24
AWS-SDK upload script, specially setup for uploading .unityweb files (gzipped webassembly files) which sets the correct content type
/** You'll need to provide a .env file at the root folder containing an endpoint, bucket name and access credentials
* # AWS_ENDPOINT=https://ams3.digitaloceanspaces.com
* # AWS_ACCESS_ID=
* # AWS_SECRET_KEY=
* # AWS_BUCKET_NAME=
*/
const dotenv = require("dotenv");
const fs = require("fs");
const path = require("path");
@bartwttewaall
bartwttewaall / Unity3D_InputFieldSetFocus.cs
Created July 22, 2019 08:39
Use Unity3D's EventSystem to set or unset focus on a UI element
public TMP_InputField inputField;
float focusReleaseTime;
float focusReleaseCooldown = 0.1f;
public bool HasFocus () {
return EventSystem.current.currentSelectedGameObject == inputField.gameObject;
}
public void SetFocus (bool value) {
if (value && !inputField.isFocused) {
@bartwttewaall
bartwttewaall / Unity3d_SetCursor.cs
Created July 22, 2019 08:34
Set or unset a cursor with a pivot at the center (Crosshair texture is 64x64 px)
if (player.canUseWeapons && current) {
Cursor.SetCursor (Resources.Load<Texture2D> ("Crosshair"), new Vector2 (32, 32), CursorMode.Auto);
} else {
Cursor.SetCursor (null, Vector2.zero, CursorMode.Auto);
}
@bartwttewaall
bartwttewaall / Unity3D_UpdateFixedDeltaTime.cs
Created July 22, 2019 08:31
Update as many times in steps of fixedDeltaTime until we have caught up to the elapsed time
float timer;
void Update () {
timer += Time.deltaTime;
while (timer >= Time.fixedDeltaTime) {
timer -= Time.fixedDeltaTime;
// calculate single step
// ..
@bartwttewaall
bartwttewaall / PlayerInputState.cs
Last active July 22, 2019 09:25
Compress multiple boolean states into a single integer and unpack it on the server or vice versa
using System;
using UnityEngine;
// https://dev.to/somedood/bitmasks-a-very-esoteric-and-impractical-way-of-managing-booleans-1hlf
[Flags]
public enum KeyFlags {
UP = 1 << 0,
DOWN = 1 << 1,
LEFT = 1 << 2,
@bartwttewaall
bartwttewaall / TriggeredInputTicker.cs
Last active July 22, 2019 09:30
A ticker / stopwatch class that keeps track of the activation and cooldown of a player's ability, like a shield.
public abstract class AbstractTicker {
public delegate void TickerEvent ();
public delegate void TickerProgressEvent (float progress);
public abstract void Tick (float time, bool input);
}
// Example
// TriggeredInputTicker shieldTicker = new TriggeredInputTicker () { duration = 4, cooldown = 8 };
@bartwttewaall
bartwttewaall / Unity3D_CullingMask-example.cs
Created July 22, 2019 08:05
Combining layers to define a cullingmask
LayerMask everything = -1;
void Start () {
LayerMask uiMask = 1 << LayerMask.NameToLayer ("UI");
cam.cullingMask = everything;
cam.cullingMask &= ~uiMask; // exclude UI layer
}
@bartwttewaall
bartwttewaall / Unity3D_UrlEncoding.md
Created July 22, 2019 08:01
When you need to send a string to a backend but some characters are missing, chance is you need to URLEncode it

UrlEncoding

Sending a string over to the colyseus server through a Dictionary<string, object> can result in missing characters.

var options = new Dictionary<string, object> () {
  { "sessionTicket", WebUtility.UrlEncode(sessionTicket) }
};
room = client.Join<SK.Schemas.PvpGameState> (roomId, options);
@bartwttewaall
bartwttewaall / Unity3D_AimReticle.cs
Created July 22, 2019 07:30
Position and rotate a reticle on the floor, at the feet of a player in the 3D direction we're aiming at a range from the center
Transform aimTransform;
float spriteOffsetY = 0.01f;
float aimRange = 1.5f;
// use incoming player.state.aimDirection to calculate the rotation and position of the reticle's aimTransform
Vector3 aim = player.state.aimDirection.normalized;
aimTransform.localPosition = new Vector3 (aim.x * aimRange, spriteOffsetY, aim.z * aimRange);
float angle = Mathf.Atan2 (aim.x, aim.z) * (180 / Mathf.PI);
aimTransform.rotation = Quaternion.Euler (90, angle, 0);