Skip to content

Instantly share code, notes, and snippets.

View benloong's full-sized avatar

benloong benloong

View GitHub Profile
public bool Raycast(Ray r, out float distance)
{
Vector3 min = this.min;
Vector3 max = this.max;
float tmin = float.NegativeInfinity, tmax = float.PositiveInfinity;
Vector3 dir_inv = new Vector3();
@benloong
benloong / RandomExtension.swift
Created June 28, 2016 09:20
Array random take extension
import Foundation
extension Array {
func random2(_ lower: Int, _ upper: Int) -> Int {
return random() % (upper - lower) + lower
}
func randomTake<T>(_ n: Int) -> [T] {
@benloong
benloong / pattern-examples.swift
Created January 21, 2016 02:01 — forked from terhechte/pattern-examples.swift
Swift pattern examples for the swift, for, and guard keywords
import Foundation
// 1. Wildcard Pattern
func wildcard(a: String?) -> Bool {
guard case _? = a else { return false }
for case _? in [a] {
return true
}
using System;
using System.Collections;
using System.Globalization;
using System.Text;
namespace Procurios.Public
{
/// <summary>
/// This class encodes and decodes JSON strings.
/// Spec. details, see http://www.json.org/
@benloong
benloong / ReimportUnityEngineUI.cs
Created January 11, 2016 06:15
解决Unity UI timestamp guid不同步
using UnityEngine;
using UnityEditor;
using System.IO;
public class ReimportUnityEngineUI
{
[MenuItem("Assets/Reimport UI Assemblies", false, 100)]
public static void ReimportUI()
{
#if UNITY_4_6
@benloong
benloong / utf8.cs
Created August 14, 2015 05:27
utf-8编码规则, 可用于网络协议头的长度编码中。
public class UTF8
{
/// <summary>
/// 计算utf-8编码后的字节数
/// </summary>
/// <param name="code">Unicode</param>
/// <returns>utf-8编码后的字节数</returns>
public static int GetByteCount(int code)
{
@benloong
benloong / ignoreFile
Created January 16, 2015 01:38
A better Unity Git Ignore File
[Ll]ibrary/
[Tt]emp/
[Oo]bj/
[Bb]uild/
[Dd]ebug/
[Rr]elease/
# Autogenerated VS/MD solution and project files
*.csproj
*.unityproj
public static int getVersionCode(Context ctx) {
int versionCode = 0;
try {
PackageInfo pInfo = ctx.getPackageManager().getPackageInfo(
ctx.getPackageName(), 0);
versionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
@benloong
benloong / pack_array.hpp
Created March 18, 2014 09:26
c++11 meta sequence
template<typename T, T... _Rest>
struct array_pack;
template<typename T>
struct array_pack<T>{};
template<typename T, T _This, T... _Rest>
struct array_pack<T, _This, _Rest...> : private array_pack<T, _Rest...>
{
const T & operator[](const int pos)
{
//as cpp memory layout, indexing reversely
// C++11 32bit FNV-1 and FNV-1a string hasing (Fowler–Noll–Vo hash)
//
// Requires a compiler with C++11 support
// See main(...) for examples
#include <iostream>
#include <cassert>
namespace hash
{