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 / 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 / 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 / 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 / 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 / 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 / ShortcutLevelLoader.cs
Last active May 9, 2021 18:26
Cycles through build scenes directly in the editor. Loads scenes in Unity according to their build order, using 'Shift' and left and right arrow keys.
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
// TODO: if the level is unchecked in build settings, the loop starts over
public class ShortcutLevelLoader
{
[MenuItem("Level loader/Load previous level &LEFT")]
@SiarheiPilat
SiarheiPilat / MultiPrefabCreator.cs
Created October 7, 2020 09:06
Creates prefabs from multiple game objects selection.
// Adapted from https://docs.unity3d.com/ScriptReference/PrefabUtility.html
using UnityEngine;
using UnityEditor;
public class MultiprefabCreator
{
// Creates a new menu item 'Examples > Create Prefab' in the main menu.
[MenuItem("Window/Create Prefab")]
static void CreatePrefab()
@SiarheiPilat
SiarheiPilat / ReverseAnimationLegacy.cs
Created August 7, 2020 22:13
Proper way of reversing animation playback in Unity when using legacy animation system ('Animation' component; when you need to play it backwards).
using UnityEngine;
public class ReverseAnimationLegacy : MonoBehaviour
{
Animation Animation;
void Start()
{
Animation = GetComponent<Animation>();
Animation["walk"].speed = -0.25f;
@SiarheiPilat
SiarheiPilat / ReplaceWithPrefab.cs
Last active September 20, 2022 23:01 — forked from unity3dcollege/ReplaceWithPrefab.cs
Updated for the latest API. Accounts for original prefabs and variants.
using UnityEngine;
using UnityEditor;
/// Taken from: https://gist.github.com/SiarheiPilat/05463e64d4662860c6a799bb23d9aec8
/// Forked from: https://gist.github.com/unity3dcollege/c1efea3f87d3775bee3e010e9c6d7648
/// Author: Siarhei Pilat (Suasor AB)
/// License: MIT
public class ReplaceWithPrefab : EditorWindow
{
@SiarheiPilat
SiarheiPilat / ScriptableObjectMassCreator.cs
Last active October 6, 2020 08:20
Editor script to create a whole bunch of scriptable objects from a list of comma-separated names or selected game objects in the scene.
using UnityEditor;
using UnityEngine;
public class ScriptableObjectMassCreator : EditorWindow
{
public string str;
public bool FromText;
public bool SplitComma;
[MenuItem("Window/Scriptable Object Mass Creator")]