Skip to content

Instantly share code, notes, and snippets.

@Sov3rain
Sov3rain / ExtendedScriptableObjectDrawer.cs
Created January 24, 2020 10:45 — forked from tomkail/ExtendedScriptableObjectDrawer.cs
Displays the fields of a ScriptableObject in the inspector
// Developed by Tom Kail at Inkle
// Released under the MIT Licence as held at https://opensource.org/licenses/MIT
// Must be placed within a folder named "Editor"
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
@Sov3rain
Sov3rain / ReadOnlyAttribute.cs
Created June 11, 2020 13:34 — forked from MattRix/ReadOnlyAttribute.cs
Read Only Attribute for Unity (just mark stuff as [ReadOnly] the same way you would use [HideInInspector])
using UnityEngine;
using System;
using System.Reflection;
using System.Text.RegularExpressions;
[AttributeUsage (AttributeTargets.Field,Inherited = true)]
public class ReadOnlyAttribute : PropertyAttribute {}
#if UNITY_EDITOR
[UnityEditor.CustomPropertyDrawer (typeof(ReadOnlyAttribute))]
@Sov3rain
Sov3rain / CustomHierarchy.cs
Created November 18, 2020 10:40
Editor class to override the default color highlighting effect of prefabs in hierarchy.
using System.Linq;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class CustomHierarchy : MonoBehaviour
{
private static Vector2 offset = new Vector2(0, 2);
static CustomHierarchy()
@Sov3rain
Sov3rain / Example.txt
Last active October 3, 2021 19:01
Custom yield instruction to wait inside a Unity coroutine until a callback is called.
public static class UrlDownloader
{
public static void DownloadText(string url, Action<string> callback);
}
// Brute force way of doing it
IEnumerator MyAsyncFunction()
{
var done = false;
var result = default(string);
@Sov3rain
Sov3rain / VersionIncrementor.cs
Last active November 27, 2020 16:35
A simple Editor Script to increment versions in Unity
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
[InitializeOnLoad]
class VersionIncrementor : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
@Sov3rain
Sov3rain / ScrollRectExtensions.cs
Last active February 12, 2021 14:20
Tell the Unity Scroll Rect View to snap to the given element (top).
using UnityEngine;
using UnityEngine.UI;
public static class ScrollRectExtensions
{
/// <summary>
/// Tell the Scroll Rect View to snap to the given element (top).
/// </summary>
public static void SnapTo(
this ScrollRect scrollRect,
@Sov3rain
Sov3rain / DeepCloning.cs
Last active October 12, 2021 11:22
Deep cloning objects
static public class ObjectExtensions
{
static public T DeepClone<T>(this T a)
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, a);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
@Sov3rain
Sov3rain / TwoFingersObjectRotation.cs
Created January 3, 2022 16:18
[Unity] Rotate any object with double finger touch
using static UnityEngine.TouchPhase;
using UnityEngine;
public class TwoFingersObjectRotation : MonoBehaviour
{
[SerializeField]
private Transform _target;
private Vector2 _startPosition;
@Sov3rain
Sov3rain / TouchOverUI.cs
Last active January 5, 2022 16:26
[Unity] Find if a touch is over a UI element
static class TouchHelper
{
// OG version: https://www.reddit.com/r/Unity3D/comments/6o9zsy/comment/dkgxa1i/?utm_source=share&utm_medium=web2x&context=3
public static bool IsOverUI()
{
PointerEventData pointerData = new PointerEventData(EventSystem.current);
pointerData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
@Sov3rain
Sov3rain / IfNotNull.cs
Last active April 13, 2022 15:42
Monad in c#
using System;
void Main()
{
string foo = "Hello";
// a.then(f).then(g).then(j)
foo.Then(x => x.Trim().Ret())
.Then(x => x.Substring(0, 5).Ret())
.Then(x => x.Length.Ret())