Skip to content

Instantly share code, notes, and snippets.

@leducanhh
leducanhh / Random.cs
Last active November 17, 2020 15:59
Better random
float GreaterRandom(float[] lttRate)
{
float total = 0;
foreach (float elem in lttRate)
{
total += elem;
}
float randomPoint = Random.value * total;
using UnityEngine;
using System.Collections;
public class ButtonManager : MonoBehaviour
{
//---this function has been assigned to (Button)YourButtonUI
public void OnHandleUndoEvent()
{
//---this is a solution to get YourButtonUI gameObject
var go = EventSystem.current.currentSelectedGameObject;
@leducanhh
leducanhh / GenericSingletonClass.cs
Created August 26, 2016 10:39
[Unity] Singleton Pattern
public class GenericSingletonClass<T> : MonoBehaviour where T : Component
{
private static T instance;
public static T Instance {
get {
if (instance == null) {
instance = FindObjectOfType<T> ();
if (instance == null) {
GameObject obj = new GameObject ();
obj.name = typeof(T).Name;
@leducanhh
leducanhh / HandlingCollisions.cs
Last active March 26, 2017 07:26
[Unity] use "Interface" for handling "Collision between game objects"
// Step 1: Create interface
// Bước 1: Tạo interface
public interface IHealth
{
void TakeDamage(int damage);
}
// Step 2: Implement in Victim
// Bước 2: nhét nó vào mồm thằng Player (Victim)
public class Player : IHealth
@leducanhh
leducanhh / GetStreamingAssetsPath.cs
Created July 29, 2016 08:58
[Unity] Get StreamingAssets file path with Android and iOS.
void GetStreamingAssetsPath(string fileName)
{
#if UNITY_EDITOR
dbPath = string.Format(@"Assets/StreamingAssets/{0}", fileName);
#else
// check if file exists in Application.persistentDataPath
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, fileName);
if (!File.Exists(filepath))