Skip to content

Instantly share code, notes, and snippets.

View SiarheiPilat's full-sized avatar
:octocat:
Working from home

suasor SiarheiPilat

:octocat:
Working from home
  • Suasor AB
  • Malmö, Sweden
View GitHub Profile
@SiarheiPilat
SiarheiPilat / DeckShuffle.cs
Created February 5, 2022 15:07
Array randomise example.
using UnityEngine;
using System.Collections;
// taken from https://answers.unity.com/questions/1189736/im-trying-to-shuffle-an-arrays-order.html
public class DeckShuffle : MonoBehaviour {
public GameObject[] decklist;
private GameObject tempGO;
@SiarheiPilat
SiarheiPilat / CreateScriptAsset.cs
Created January 13, 2022 15:05 — forked from johnsoncodehk/CreateScriptAsset.cs
Create script from template in project
// Not support of 2019.1.0f1
static void CreateScriptAsset(string templatePath, string destName) {
#if UNITY_2019_1_OR_NEWER
UnityEditor.ProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, destName);
#else
typeof(UnityEditor.ProjectWindowUtil)
.GetMethod("CreateScriptAsset", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
.Invoke(null, new object[] { templatePath, destName });
#endif
@SiarheiPilat
SiarheiPilat / AccidentalProjectClosePrevention.cs
Last active June 20, 2022 03:54
With this, you would close Unity using a menu command instead of pressing the "close cross".
using UnityEngine;
using UnityEditor;
// TODO: rename to 'secret quit button' xD
// Maybe just have a "Sure to quit?" window?
[InitializeOnLoad]
public class AccidentalProjectClosePrevention
{
[MenuItem("Tools/Quit")]
@SiarheiPilat
SiarheiPilat / CameraSceneCameraMirror.cs
Last active November 2, 2021 15:08
Editor script that makes main scene camera align with the editor camera, all in edit mode.
#if UNITY_EDITOR
// Author: Siarhei Pilat (Suasor AB)
// License: MIT
// Last updated: 02-11-2021
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
@SiarheiPilat
SiarheiPilat / FlexibleGridLayout.cs
Created September 12, 2021 16:46
It's like Unity's default grid layout component. but it actually behaves well.
// credit goes to: Game Dev Guide
// original: https://www.youtube.com/watch?v=CGsEJToeXmA&ab_channel=GameDevGuide
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FlexibleGridLayout : LayoutGroup
{
@SiarheiPilat
SiarheiPilat / SetLayerRecursively.cs
Created September 8, 2021 22:43
Sets layer on all transforms and children
// taken from: https://forum.unity.com/threads/change-gameobject-layer-at-run-time-wont-apply-to-child.10091/
// special thanks to: chadfranklin47
public static class ExtensionMethods
{
public static void SetLayerRecursively(this Transform parent, int layer)
{
parent.gameObject.layer = layer;
for (int i = 0, count = parent.childCount; i < count; i++)
@SiarheiPilat
SiarheiPilat / SceneNameDisplay.cs
Last active December 17, 2020 12:06
Displays active scene name in top left corner of the 'Scene' window, also highlights that scene in the 'Project' window, if pressed.
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
public class SceneNameDisplay
{
[MenuItem("Tools/Scene name toggle &r")]
@SiarheiPilat
SiarheiPilat / SciptableObjectValueSetter.cs
Created November 21, 2020 23:44
How to set ScriptableObject values in project files.
class SciptableObjectValueSetter {
// 'Mechanic' is a ScriptableObject
var so = (Mechanic)AssetDatabase.LoadAssetAtPath("Assets/SOs/Abilities.asset", typeof(Mechanic));
so.SomeValue = "boop";
// And this ensures that the value is saved not only in the inspector and also survives all kinds of reloads!
EditorUtility.SetDirty(so);
AssetDatabase.SaveAssets();
using UnityEngine;
using UnityEditor;
using UnityEditor.EditorTools;
[EditorTool("Prefab Painter 2D")]
public class PrefabPainter2D : EditorTool
{
float DiscRadius;
public override void OnToolGUI(EditorWindow window)
@SiarheiPilat
SiarheiPilat / NewObjectBottomOfHierarchy.cs
Created October 30, 2020 18:25
Move a prefab instance to the bottom of the hierarchy whenever you drag and drop it to the scene.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class NewObjectBottomOfHierarchy
{
static NewObjectBottomOfHierarchy()
{