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 / RandomiseInsideCollider.cs
Created October 20, 2020 15:44
Spawn objects (or randomise their position) inside an area defined by a 2D collider.
using UnityEngine;
public class RandomiseInsideCollider : MonoBehaviour
{
public Collider2D AreaPolygonCollider;
Bounds Bounds;
Vector3 BoundX1, BoundX2, BoundY1, BoundY2;
Vector3 randomPos;
public Transform go;
@SiarheiPilat
SiarheiPilat / GroupCommand.cs
Last active October 20, 2020 19:07
Grouping objects in the Hierarchy, in Unity.
using UnityEditor;
using UnityEngine;
// Taken from: https://answers.unity.com/questions/118306/grouping-objects-in-the-hierarchy.html
public static class GroupCommand
{
[MenuItem("GameObject/Group Selected %g")]
private static void GroupSelected()
{
@SiarheiPilat
SiarheiPilat / ObjectShaker.cs
Created October 20, 2020 19:06
Object shaker utilising a coroutine. Suitable for shaking all kinds of objects, but also VERY suitable for camera shake effect.
using System.Collections;
using UnityEngine;
// Taken from: https://stackoverflow.com/questions/39380901/shake-an-object-back-and-forth-with-easing-at-the-end
public class ObjectShaker : MonoBehaviour
{
public GameObject GameObjectToShake;
bool shaking = false;
@SiarheiPilat
SiarheiPilat / AddSceneToBuildSettingsShortcut.cs
Last active October 29, 2020 10:52
Add open/active scene to the build settings as last scene or (!!!) second to last scene using menu item or shortcuts.
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;
// inspired by https://answers.unity.com/questions/205742/adding-scene-to-build.html?_ga=2.184209894.1575122923.1603913761-831749454.1600446627
public static class AddSceneToBuildSettingsShortcut
{
[MenuItem("Game/Add scene as last build scene &a")]
private static void AddAsLastBuildScene()
@SiarheiPilat
SiarheiPilat / DisableMenuItem.cs
Created October 29, 2020 13:19
How to disable a given MenuItem based on a certain condition using the isValidateFunction argument.
using UnityEditor;
public static class DisableMenuItem
{
[MenuItem("Examples/How to disable a custom menu item")]
static void DisableMenuItemExample(){ }
// Disable the menu item under a certain condition. This is where isValidateFunction comes in play, by the way.
[MenuItem("Examples/How to disable a custom menu item", true)]
static bool ValidateMenuItem(){ return Selection.activeGameObject != null; }
@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()
{
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 / 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();
@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 / 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++)