Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk
johnsoncodehk / ClampAngle.cs
Last active April 12, 2024 16:31
Unity Clamp Angle
public static float ClampAngle(float angle, float min, float max) {
float start = (min + max) * 0.5f - 180;
float floor = Mathf.FloorToInt((angle - start) / 360) * 360;
return Mathf.Clamp(angle, min + floor, max + floor);
}
@johnsoncodehk
johnsoncodehk / CreateScriptAsset.cs
Last active January 18, 2024 10:00
Create script from template in project
// Not support of 2019.1.0f1
static void CreateScriptAsset(string templatePath, string destName) {
#if UNITY_2019_1_OR_NEWER
UnityEditor.ProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, destName);
#else
typeof(UnityEditor.ProjectWindowUtil)
.GetMethod("CreateScriptAsset", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.Invoke(null, new object[] { templatePath, destName });
#endif
/**
*
* @type {import('@tsslint/config').Rule}
*/
const noConsoleRule = ({ typescript: ts, sourceFile, reportWarning }) => {
ts.forEachChild(sourceFile, function walk(node) {
if (
ts.isPropertyAccessExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === 'console'
@johnsoncodehk
johnsoncodehk / cheapComputed.ts
Last active August 27, 2020 23:16
Vue 3 "cheap" computed
import { computed, ref } from "vue";
import { ComputedGetter, pauseTracking, resetTracking } from "@vue/reactivity";
// if no version, not work for objects/arrays...
export function cheapComputed<T, K = T>(getValue: ComputedGetter<T>, getVersion?: ComputedGetter<K>) {
const value = computed(getValue);
const version = getVersion ? computed(getVersion) : value;
const lastValue = ref<T>();
const lastVersion = ref<K>();
const changed = computed(() => version.value !== lastVersion.value);
@johnsoncodehk
johnsoncodehk / Fix55To54.cs
Last active February 24, 2020 06:44
Fix Unity 5.5 Downgrading to 5.4 missing GameObject name, activeSelf.
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class Fix55To54 {
[System.SerializableAttribute]
public class GameObjectData {
public bool activeSelf;
@johnsoncodehk
johnsoncodehk / trim-trailing-slash.js
Last active November 16, 2019 17:20
Remove URL trailing slash
window.history.replaceState("", "", window.location.href.replace(new RegExp("/(?!.*/)"), ""))
@johnsoncodehk
johnsoncodehk / gscc.js
Last active June 6, 2019 21:56
GameSparks Cloud Code - (CommonJS)TypeScript to Cloud Code
/**
* Obsolete! Use https://github.com/johnsoncodehk/ts2gamesparks
*/
var path = require('path'), fs = require('fs');
var outDir = "./dist/";
function findAllTsFiles(startPath, paths = []) {
if (!fs.existsSync(startPath)) {
@johnsoncodehk
johnsoncodehk / ScreenCaptureMenu.cs
Last active January 28, 2019 08:34
Unity capture screenshot to file
#if UNITY_EDITOR
using System;
using UnityEngine;
using UnityEditor;
public static class ScreenCaptureMenu {
[MenuItem("ScreenCapture/CaptureScreenshot")]
public static void Cap() {
string filename = Convert.ToInt32(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds).ToString() + ".png";
@johnsoncodehk
johnsoncodehk / Unity.gitignore
Created December 1, 2018 21:09
.gitignore for Unity
/*
/*/
!/Assets/
!/Packages/
!/ProjectSettings/
!.gitignore
del /s /q /f /a .DS_STORE
del /s /q /f /a ._*
del /s /q /f /a *DS_STORE
pause