Skip to content

Instantly share code, notes, and snippets.

View SiarheiPilat's full-sized avatar
🍌
Working from home

spilat12 SiarheiPilat

🍌
Working from home
View GitHub Profile
@SiarheiPilat
SiarheiPilat / BezierCurve3PointLineRenderer.cs
Created December 17, 2019 00:15
Bezier Curve Line Renderer for Unity, credit goes to World of Zero: https://www.youtube.com/watch?v=tgCFzoG_BJM
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Bezier Curve Line Renderer for Unity, credit goes to World of Zero: https://www.youtube.com/watch?v=tgCFzoG_BJM
/// </summary>
[ExecuteInEditMode]
public class BezierCurve3PointLineRenderer : MonoBehaviour
{
@SiarheiPilat
SiarheiPilat / ScriptAutoRefreshControl.cs
Created January 15, 2020 15:38
Turns Auto Refresh option (script recompilation, domain reload) on and off.
using UnityEngine;
using UnityEditor;
public class ScriptAutoRefreshControl : MonoBehaviour
{
/// <summary>
/// Turns Auto Refresh option on and off (it is about script recompilation, domain reload).
/// Same as going to Edit -> Preferences -> General and changing Auto Refresh option.
/// </summary>
@SiarheiPilat
SiarheiPilat / PerformanceEnhancer.cs
Created April 29, 2020 07:56
Computes distances between vectors faster than Vector3.Distance() or Vector2.Distance()
using UnityEngine;
/// <summary>
/// Vector3.Distance(a,b) is the same as (a-b).magnitude
/// Since computing squared magnitudes is faster than Vector3.magnitude, (a - b).sqrMagnitude is more performant than Vector3.Distance
/// </summary>
public class PerformanceEnhancer : MonoBehaviour
{
public static float Distance(Vector3 a, Vector3 b)
@SiarheiPilat
SiarheiPilat / AutofillAttribute.cs
Last active December 27, 2021 06:50
Custom attribute that adds a button in front of a variable in the Inspector, pressing which performs an attempt to fill in the object reference with corresponding object that is sitting on the inspected game object.
using UnityEngine;
using UnityEditor;
// REMEMBER that for custom attributes with property drawers, the attribute script should be in normal folder, while the drawer script - in the editor folder
public class AutofillAttribute : PropertyAttribute { }
[CustomPropertyDrawer(typeof(AutofillAttribute))]
public class AutofillAttributeDrawer : PropertyDrawer
{
@SiarheiPilat
SiarheiPilat / SpritesheetPivotEditor.cs
Created June 12, 2020 21:48
Simple editor script that allows to set pivot for multiple slices of a sprite sheet automatically, so you won't have to do it manually. The sprite sheet has to be sliced beforehand.
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(SpritesheetPivotEditor))]
public class SpritesheetPivotEditor : EditorWindow
{
[MenuItem("Tools/Sprites/Pivot Editor")]
static void OpenPivotEditor()
{
@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")]
@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 / 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 / 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 / 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")]