Skip to content

Instantly share code, notes, and snippets.

Avatar

Robert Wahler robertwahler

View GitHub Profile
@robertwahler
robertwahler / UnityFindAndReplaceWithGNUTools.md
Created September 10, 2019 15:34
Find and Replace in Unity Assets using GNU Tools
View UnityFindAndReplaceWithGNUTools.md

Find and Replace in Unity Assets

Directly change serialized field name LevelId to levelId in all scenes. Requires Unity Force Text serialization.

Requires GNU tools. Tested on OSX. Note Windows @code users can run WSL Linux directly in the editor terminal!

Find Product Scenes

@robertwahler
robertwahler / package.rb
Created January 23, 2019 14:22
Ruby Thor snippet to package code on OSX
View package.rb
desc "osx", "create distribution package for Mac OS X"
method_option :dmg, :type => :boolean, :desc => "Create a DMG container instead of a zip file"
def osx
set_instance_variables
# clean staging folder
clean
osx_copy_to_staging(@product)
@robertwahler
robertwahler / entity.cs
Created January 22, 2019 16:24
Non allocating, lazy loaded, Unity Physics.OverlapSphere
View entity.cs
/// <summary>
/// Collider cache for non allocating physics calls. Lazy loaded.
/// </summary>
/// <example>
/// Usage
/// <code>
/// float radius = 0.2f;
/// int colliderCount = Physics.OverlapSphereNonAlloc(transform.position, radius: radius, results: ColliderCache);
/// for (int i = 0; i<colliderCount; i++) {
/// ...
@robertwahler
robertwahler / Settings.cs
Created May 8, 2018 16:43
Unity DataPath property w/ hard coded macOS path
View Settings.cs
/// <summary>
/// The root data folder. Used by the entire application to read and write
/// data, not just used by the Settings class. This is an absolute path.
/// </summary>
#if UNITY_EDITOR
// Each product has its own folder in tmp when running in the editor.
// This allows audio and other debug settings to be separate from
// standalone builds on the same development machine.
// NOTE: The hard-coded forward slash should work on Windows, Linux, and OSX.
public static string DataPath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "tmp/settings/" + Application.productName);
@robertwahler
robertwahler / ScreenCapture.cs
Created May 3, 2018 13:26
Unity snippet for supersizing in-engine screenshots
View ScreenCapture.cs
// Take up to 10 screenshots per session, then start overwriting
private IEnumerator Capture(string path=null, string filename=null) {
Log.Debug(string.Format("ScreenCapture.Capture(path: {0}, filename: {1}) Settings.DataPath={0}", path, filename, Settings.DataPath));
screenShotCount += 1;
if (screenShotCount > 10) {
screenShotCount = 1;
}
if (filename == null) {
@robertwahler
robertwahler / FileIO.jslib
Created April 12, 2018 13:08
UnityEngine.PlayerPrefs wrapper for WebGL LocalStorage
View FileIO.jslib
var FileIO = {
SaveToLocalStorage : function(key, data) {
localStorage.setItem(Pointer_stringify(key), Pointer_stringify(data));
},
LoadFromLocalStorage : function(key) {
var returnStr = localStorage.getItem(Pointer_stringify(key));
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
@robertwahler
robertwahler / code.rb
Last active August 1, 2016 21:38
Empty folders, #git, & #unity3d don't mix. Git doesn't track empty folders, why does @Unity3D?
View code.rb
module BasicUnity
class Code < Thor
namespace :code
include Thor::Actions
desc "prune", "Remove empty folders and Unity meta files that point to empty folders"
method_option "dry-run", :type => :boolean, :desc => "Show what will happen but don't 'rmdir' or 'rm'"
def prune(folder=nil)
# bash version, requires GNU find
@robertwahler
robertwahler / JsonSerializerBinderTest.cs
Last active June 27, 2016 18:47
Testing Newtonsoft.Json Binder
View JsonSerializerBinderTest.cs
using UnityEngine;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters;
using System.Linq;
using NUnit.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
@robertwahler
robertwahler / DynamicLayoutGroup.cs
Last active January 20, 2022 00:41
Unity 3D layout group switches orientation automatically between landscape and portrait layouts so it either acts like a VerticalLayoutGroup or a HorizontalLayoutGroup.
View DynamicLayoutGroup.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
namespace SDD.UI {
/// <summary>
/// Layout group switches orientation automatically between landscape and
@robertwahler
robertwahler / WaitForExample.cs
Created October 20, 2015 13:11
Avoid Unity garbage collection when coroutines wait
View WaitForExample.cs
// Avoid GC when coroutines wait, instantiate outside of loop
WaitForSeconds shortWait = new WaitForSeconds(1.0f);
private IEnumerator Run() {
while (true) {
// do stuff here
yield return shortWait;
}
}