Skip to content

Instantly share code, notes, and snippets.

View brunomikoski's full-sized avatar
🛹

Bruno Mikoski brunomikoski

🛹
View GitHub Profile
@brunomikoski
brunomikoski / gist:7098735
Created October 22, 2013 11:06
How to sort a array in c#
// Sample sorting by player age:
Array.Sort( arrPlayers, delegate(Player a, Player b)
{
if( a.Age > b.Age ) // if a is higher than b
return 1;
else if ( a.Age < b.Age ) // if b is higher than a
return -1;
return 0; // if both are equals return 0
});
@brunomikoski
brunomikoski / gist:7098756
Last active December 26, 2015 05:09
Action example in C#
//C# Event using Action delegate:
public event Action OnSomeEvent;
public event Action<string> OnSomeEventWithParameters;
// Rising the event, must check if is null:
void SomeMethod()
{
if( OnSomeEvent != null )
OnSomeEvent();
@brunomikoski
brunomikoski / gist:7098760
Created October 22, 2013 11:07
A simple way to circular navigation in arrays.
//Next
int i = (i+1) % targetArray.Count
//Prev
int i = (i + (targetArray.Count-1)) % targetArray.Count;
@brunomikoski
brunomikoski / gist:7098772
Created October 22, 2013 11:08
Delega example C#
//C# Delegate:
public delegate [return] DelegateName([type parameter]...);
//Sample:
public delegate void CalbackHandler(int pIndex);
//So you can use it as a parameter to other method:
public void PassDelegateByParameter( CallbackHandler pHandler )
{
@brunomikoski
brunomikoski / gist:7098776
Created October 22, 2013 11:08
PT-BR Default characters
<>:;?/\|"'-_=+[]{}M%&*#$@! ()[]{}.,
qwertyuiopasdfghjklçzxcvbnmQWERTYUIOPASDFGHJKLZXCVBN1234567890
ÁáÉéÍíÓóÚúÀàÂâÊêÔôÃãÕõÜüÇç
@brunomikoski
brunomikoski / gist:7098783
Last active December 26, 2015 05:09
How to search for a gameobject inside another hierarchy.
public static class Extensions
{
public static Transform Search(this Transform target, string name)
{
if (target.name == name) return target;
for (int i = 0; i < target.childCount; ++i)
{
var result = Search(target.GetChild(i), name);
@brunomikoski
brunomikoski / UnitySingleton.cs
Created October 22, 2013 11:10
A singleton helper, to create a singleton to some class
/*************************************************************
* Unity Singleton Class (c) by ClockStone 2011 *
*
* Allows to use a script components like a singleton
*
* Usage:
*
* Derive your script class MyScriptClass from
* SingletonMonoBehaviour<MyScriptClass>
*
@brunomikoski
brunomikoski / SceneAutoLoader.cs
Created October 22, 2013 11:21
This class adds a File > Scene Autoload menu containing options to select a "master scene" enable it to be auto-loaded when the user presses play in the editor. When enabled, the selected scene will be loaded on play, then the original scene will be reloaded on stop.
using UnityEngine;
using UnityEditor;
/// <summary>
/// Scene auto loader.
/// </summary>
/// <description>
/// This class adds a File > Scene Autoload menu containing options to select
/// a "master scene" enable it to be auto-loaded when the user presses play
/// in the editor. When enabled, the selected scene will be loaded on play,
@brunomikoski
brunomikoski / gist:7603323
Last active December 29, 2015 02:49
Find component upwards.
public static T FindComponentUpwards<T>(this Transform pTarget) where T : Component
{
Transform tTransform = pTarget.parent;
while (true)
{
T tComponent = tTransform.GetComponent<T>();
if (tComponent == null)
{
if (tTransform.parent)
{
@brunomikoski
brunomikoski / gist:8713008
Last active August 29, 2015 13:55
Shuffle List Extention
/// <summary>
/// Shuffles the element order of the specified list.
/// </summary>
public static void Shuffle<T>(this IList<T> ts) {
var count = ts.Count;
var last = count - 1;
for (var i = 0; i < last; ++i) {
var r = Random.Range(i, count);
var tmp = ts[i];
ts[i] = ts[r];