Skip to content

Instantly share code, notes, and snippets.

View benloong's full-sized avatar

benloong benloong

View GitHub Profile
@benloong
benloong / SimpleFSM
Created November 12, 2012 03:19
simple FSM using delegate and coroutine
using UnityEngine;
using System.Collections.Generic;
public class SimpleFSM : MonoBehaviour {
public delegate IEnumerator StateHandler();
public enum State {
Idle,
Attack,
@benloong
benloong / CameraShake.js
Created December 6, 2012 03:36
Simple CameraShake
function CameraShake () {
var hitTime = Time.time;
var originalPosition = cam.transform.localPosition.x;
var shakeCounter = numberOfShakes;
var shakeDistance = startingShakeDistance;
while (shakeCounter > 0) {
// Make timers always start at 0
var timer = (Time.time - hitTime) * shakeSpeed;
cam.transform.localPosition.x = originalPosition + Mathf.Sin(timer) * shakeDistance;
@benloong
benloong / TimerManager
Last active October 13, 2015 21:08
Simple Timer Event Dispatcher using delegate
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TimerManager : MonoBehaviour {
public delegate void Callback();
static TimerManager _instance = null;
@benloong
benloong / RTSCamera.cs
Created December 24, 2012 06:35
Simple RTS Camera
using UnityEngine;
using System.Collections;
public class RTSCamera : MonoBehaviour {
public float scrollArea = 50f;
public float scrollSpeed = 50f;
public float rotateSpeed = 4f;
@benloong
benloong / CamerFaderTest.cs
Last active December 10, 2015 23:19
simple camera fade script
using UnityEngine;
using System.Collections;
public class CameraFaderTest : MonoBehaviour {
void Start(){
//level 1 enter fade in
CameraFader.NewFadeIn().endCall = delegate {
//finish level1
//level 1 fade out
@benloong
benloong / Selection.cs
Created January 29, 2013 02:28
RTS Volume selection
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
class Selection {
List<Unit> static PickUnitsInRect(float left, float top, float right, float bottom, Camera camera = null) {
List<Unit> pickedUnits = new List<Unit>();
if(camera == null) camera = Camera.main;
Ray bottomLeft = camera.ScreenPointToRay(new Vector3(left, bottom));
Ray topLeft = camera.ScreenPointToRay(new Vector3(left, top));
using using UnityEngine;
using System.Collections;
using System.Collections.Generic;
class MathHelper {
float static CalcAngleOfElevation (Vector3 from, Vector3 to, float v, float g) {
float x = (to - from).z;
float y = (to - from).y;
float v2 = v * v;
float v4 = v2 * v2;
@benloong
benloong / object_manager.hpp
Last active December 14, 2015 11:39
simple object_manager and object_handle
#ifndef OBJECT_MANAGER
#define OBJECT_MANAGER 0
#include <vector>
#include <cassert>
const size_t INVALID = -1;
template <typename T>
class object_manager;
@benloong
benloong / NetworkClient.cs
Last active June 13, 2019 16:46
c# async socket client sample
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
using LitJson;
public class NetworkClient : MonoBehaviour {
@benloong
benloong / Export.cs
Last active December 29, 2015 07:58
generate assetbundle at specific assets folder
public static ExportMenu {
[MenuItem("Export/Config")]
static public void ExportConfig()
{
string platInfo = "PC";
BuildTarget target = BuildTarget.StandaloneWindows;
#if UNITY_ANDROID
target = BuildTarget.Android;
platInfo = "Android";
#endif