Skip to content

Instantly share code, notes, and snippets.

View bartwttewaall's full-sized avatar

Bart Wttewaall bartwttewaall

View GitHub Profile
@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_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 / 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 / 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 / Unity3D_ChatMessageItemRenderer.cs
Last active July 22, 2019 09:59
A single chat richtext (multi)line prefab with fade in/out method and optional timeout event. Extends ItemRenderer
using System;
using System.Collections;
using DG.Tweening;
using TMPro;
using UnityEngine;
using Wirelab.Pages;
public class ChatMessageItemRenderer : ItemRenderer<ChatMessageData> {
public BaseEvent onTimeout;
@bartwttewaall
bartwttewaall / Unity3D_CleanupSceneBuildProcessor.cs
Created July 22, 2019 09:53
Build script to strip out Colliders from a scene, used in a project where all static colliders we exported as a json file and were not needed within the scene at runtime which made it more performant.
#if UNITY_EDITOR
/**
This script should be located in an Editor folder
No need to try assigning it to a GameObject
It will run automatically when playing from the editor or during a build
see: https://docs.unity3d.com/ScriptReference/Build.IProcessSceneWithReport.OnProcessScene.html
*/
using System;
@bartwttewaall
bartwttewaall / Unity3D_FullscreenButton.cs
Created July 22, 2019 09:58
Custom script extending Button: adds a onDown ButtonClickedEvent in order to trigger Screen.fullscreen when clicked and not on the next interaction event.
/* Usage
fullscreenButton.onDown.AddListener (() => {
Screen.fullScreen = !Screen.fullScreen;
});
*/
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
@bartwttewaall
bartwttewaall / dl-horizontal.scss
Created July 24, 2019 15:37
Horizontal definition list for Bootstrap4 in the same style as there used to be included in Bootstrap3
.dl-horizontal {
$definitionWidth: 100px;
display: flex;
flex-wrap: wrap;
> dt {
width: $definitionWidth;
text-align: left;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
@bartwttewaall
bartwttewaall / express_get-robots.js
Created August 1, 2019 12:33
Generate robots.txt on the fly which makes it possible to only allow web crawlers when in production mode
app.get("/robots.txt", function(req, res) {
res.type("text/plain");
var rules = {
"User-agent": "*",
"Disallow": process.env.NODE_ENV === "production" ? "" : "/"
};
var flattenedString = Object.keys(rules)
.map((key) => key + ": " + rules[key])
.join("\n");
res.send(flattenedString);
@bartwttewaall
bartwttewaall / mixin-truncate.scss
Created August 5, 2019 14:27
Set wordwrap properties for ellipsis text
/* example: @include truncate(24px, 30px, 3); */
@mixin truncate($font-size, $line-height, $lines-to-show) {
display: block; // Fallback for non-webkit
display: -webkit-box;
max-width: 400px;
font-size: $font-size;
line-height: $line-height;
-webkit-line-clamp: $lines-to-show;
-webkit-box-orient: vertical;