Skip to content

Instantly share code, notes, and snippets.

@Sacristan
Last active July 10, 2018 17:32
Show Gist options
  • Save Sacristan/10ae814a86c35e269def6886e8c33626 to your computer and use it in GitHub Desktop.
Save Sacristan/10ae814a86c35e269def6886e8c33626 to your computer and use it in GitHub Desktop.
Unity Tricks

#Scaling Unity UI World space Canvas formulae -> Scale = unit in meters / canvas_width

Get Transform children

Using index

for(int i=0;i<starManager.transform.childCount; i++)
{
    Transform child = starManager.transform.GetChild(i);
}

Using Enumerator

foreach(Transform child in starManager.transform)
{

}

Editor Script remove all children (LINQ)

    List<Transform> tempList = t.transform.Cast<Transform>().ToList();
    tempList.ForEach(x => DestroyImmediate(x.gameObject));

Simple State Machine

using UnityEngine;
using System.Collections;
 
public class Monster : MonoBehaviour {
 
    public enum State {
        Crawl,
        Walk,
        Die,
    }
 
    public State state;
 
    IEnumerator CrawlState () {
        Debug.Log("Crawl: Enter");
        while (state == State.Crawl) {
            yield return 0;
        }
        Debug.Log("Crawl: Exit");
        NextState();
    }
 
    IEnumerator WalkState () {
        Debug.Log("Walk: Enter");
        while (state == State.Walk) {
            yield return 0;
        }
        Debug.Log("Walk: Exit");
        NextState();
    }
 
    IEnumerator DieState () {
        Debug.Log("Die: Enter");
        while (state == State.Die) {
            yield return 0;
        }
        Debug.Log("Die: Exit");
    }
 
    void Start () {
        NextState();
    }
 
    void NextState () {
        string methodName = state.ToString() + "State";
        System.Reflection.MethodInfo info =
            GetType().GetMethod(methodName,
                                System.Reflection.BindingFlags.NonPublic |
                                System.Reflection.BindingFlags.Instance);
        StartCoroutine((IEnumerator)info.Invoke(this, null));
    }
   
}
private float GetPercentageAlong(Vector3 a, Vector3 b, Vector3 c)
{
    var ab = b - a;
    var ac = c - a;
    return Vector3.Dot(ac, ab) / ab.sqrMagnitude;
}

IF Type.GetType() throws null reference error (Unity bug) on build:

Assembly.Load("Assembly-CSharp").GetType(node.Name);
using System;
 
namespace StateMachine
{
    public class Machine<T> where T : struct, IConvertible
    {
        protected T _currentState;
 
        public Machine()
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumeration");
            }
        }
 
        public virtual T InOut(T input)
        {
            _currentState = input;
            return input;
        }
    }
}

Usage:

public enum EnemyState
{
      Idle,
      Sleeping,
      Patrolling
}

public class EnemyMachine : Machine<EnemyState>
{
    //Your logic here
}
 
EnemyMachine eMachine = new EnemyMachine;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment