Skip to content

Instantly share code, notes, and snippets.

View marinbenc's full-sized avatar

Marin Benčević marinbenc

View GitHub Profile
@marinbenc
marinbenc / CollisionHandler.cs
Last active May 10, 2018 08:54
Collision Handler - A Unity component to add callbacks to respond to collisions or triggers
// Often I need to add multiple triggers to single object that do different things.
// This script lets you add a child containing a collider, and assign Events to
// happen when the collider is entered or triggered.
//
// Simply create a child object, add a collider and add this script. Set up events
// like you would for any other components. (like Button etc.)
using System;
using UnityEngine;
using UnityEngine.Events;
@marinbenc
marinbenc / DebouncedAction.swift
Last active July 28, 2017 21:33
An action that will be debounced after a certain time in Swift.
import Foundation
/// An action that will get executed after a certain amount of
/// time, if the object is still alive. If the object gets
/// deinited before the provided time, the action will never
/// get executed.
class DebouncedAction {
private let action: ()-> Void
private var onCancel: (()-> Void)?
@marinbenc
marinbenc / DataService.swift
Last active February 3, 2018 23:35
Automatically create simple mocks for protocols in Swift
import Foundation
/// Protocols confoming to this protocol will have a mock generated for them.
protocol AutoMockable {}
protocol DataServiceProtocol: AutoMockable {
func fullUrl(forPath path: String)-> String
func fetchResource(fromURL url: URL, onComplete: (Result)-> Void)
@marinbenc
marinbenc / Array+Decodable.swift
Last active January 24, 2017 09:59
Make a Swift Array conform to Decodable (for JSON parsing)
typealias JSON = Any
public protocol Decodable {
init?(_ json: JSON)
}
extension Array: Decodable {
public init?(_ json: JSON) {
@marinbenc
marinbenc / MusicPlayer.swift
Created January 15, 2016 22:02
The night I invented array subscripting
//I was really tired when I wrote this:
//(I later replaced it with the two lines below)
// if let sound = Sound(index: soundData.soundNum, position: soundData.position) {
// positions
// .filter{$0.index == sound.position}
// .last?
// .sounds
// .append(sound)
// }