Skip to content

Instantly share code, notes, and snippets.

View benloong's full-sized avatar

benloong benloong

View GitHub Profile
@benloong
benloong / UIBorder.cs
Created January 6, 2017 02:13
Unity RectTransform Border
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIBorder : MaskableGraphic {
[SerializeField]
float cornerRadius = 5;
[SerializeField]
@benloong
benloong / Semaphore.h
Created December 20, 2016 08:56
C++ semaphore, condition variable mutex based.
struct Semaphore
{
std::atomic_int count = 0;
std::condition_variable cond_var;
std::mutex mutex;
void wait() {
std::unique_lock<std::mutex> lock(mutex);
while (!count)
@benloong
benloong / pool_allocator.h
Created December 19, 2016 13:00
simple pool allocator
#pragma once
#include<memory>
template<typename T, size_t ChunkSize = 1024>
struct pool_allocator
{
using alloc_type = T;
using pointer_type = alloc_type*;
pool_allocator()
@benloong
benloong / aligned_allocator.h
Created December 19, 2016 09:17
aligned allocator
#pragma once
#include<memory>
template<int alignment>
struct aligned_allocator
{
static_assert((alignment&(alignment - 1)) == 0, "alignment must be power of 2.");
void* allocate(size_t size) {
@benloong
benloong / Semaphore.cpp
Created December 8, 2016 05:17
Semaphore using c++11 condition_variable and mutex, SpinLock using atomic_flag
#include <future>
struct SpinLock {
std::atomic_flag spin_flag = ATOMIC_FLAG_INIT;
void lock() {
for (size_t k = 0; spin_flag.test_and_set(std::memory_order_acquire); k++)
{
if (k < 4)
{
template<typename L1, typename L2>
struct S : L1, L2
{
S(L1 l1, L2 l2) :L1( std::move(l1) ), L2( std::move(l2) )
{
}
using L1::operator();
using L2::operator();
@benloong
benloong / PageViewController.cs
Created November 3, 2016 08:59
PageViewController for Unity engine.
using UnityEngine;
using UnityEngine.EventSystems;
public class PageViewController : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler
{
public enum Direction
{
Horizontal,
Vertical
}
@benloong
benloong / delegate.cpp
Last active December 20, 2016 06:36
c++ delegate implementation
#pragma once
#include<type_traits>
template <typename T> class Delegate;
template<class R, class ...Args>
class Delegate <R(Args...)> final
{
@benloong
benloong / UIDissolve.shader
Last active April 17, 2024 20:30 — forked from nukeop/dissolve.shader
Dissolve shader for Unity
Shader "UI/Dissolve"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
@benloong
benloong / ExportSprites.cs
Created July 11, 2016 03:52 — forked from tsubaki/ExportSprites.cs
Load Multiple Sprite with scriptableObject
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ExportSprites {
[MenuItem("Assets/Create/SpriteScriptableObject")]
static void Export()
{
foreach (var sprite in Selection.objects) {