Skip to content

Instantly share code, notes, and snippets.

View jfranmora's full-sized avatar

JfranMora jfranmora

View GitHub Profile
@jfranmora
jfranmora / LockComponentEditor.cs
Last active August 24, 2021 20:26
Unity utility to make components not editable in Editor.
using UnityEditor;
using UnityEngine;
public static class LockComponentEditor
{
[MenuItem("CONTEXT/Component/Lock Component")]
public static void LockComponent(MenuCommand command)
{
Component component = (Component)command.context;
component.hideFlags |= HideFlags.NotEditable;
@jfranmora
jfranmora / EditorUtils.cs
Last active February 25, 2021 14:35
Utility to change the target script of a component
using UnityEditor;
using UnityEngine;
public static class EditorUtils
{
/// <summary>
/// Verify whether it can be converted to the specified component.
/// </summary>
public static bool CanConvertTo<T>(Object context) where T : MonoBehaviour
{
@jfranmora
jfranmora / SetComponentAsNonEditableMenuItem.cs
Last active February 25, 2021 14:32
Set component as non editable via context menu
using UnityEditor;
using UnityEngine;
public static class SetComponentAsNonEditableMenuItem
{
[MenuItem("CONTEXT/Component/Set non editable", isValidateFunction: true)]
private static bool _SetNonEditable(MenuCommand command)
{
return (command.context.hideFlags & HideFlags.NotEditable) == 0;
}
@jfranmora
jfranmora / JsonFormatter.cs
Created August 10, 2020 09:01
JSON Pretty print, useful for editor stuff without having to deserialize/serialize it again
using System.Text;
public static class JsonFormatter
{
private const string Indent = " ";
public static string PrettyPrint(string input)
{
var output = new StringBuilder(input.Length * 2);
char? quote = null;
@jfranmora
jfranmora / LocalPostProcessProfile.cs
Last active June 25, 2020 08:44
Component to create PostProcessProfile that lives in the Scene. Useful for temporal profiles.
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
[ExecuteInEditMode]
[RequireComponent(typeof(PostProcessVolume))]
public class LocalPostProcessProfile : MonoBehaviour
{
[SerializeField, HideInInspector]
private PostProcessProfile _profile;
@jfranmora
jfranmora / GroupGameObjects.cs
Last active June 25, 2020 08:43
Editor extension to group selected objects pressing Ctrl+G.
using System;
using UnityEditor;
using UnityEngine;
public static class GroupGameObjects
{
[MenuItem("Edit/Group Items %g")]
public static void GroupSelected()
{
if (Selection.gameObjects.Length > 1)
@jfranmora
jfranmora / IsNullOrEmptyExtensions.cs
Last active June 25, 2020 08:50
Extension to create a consistent way to check if null or empty with different types.
using System.Collections.Generic;
public static class IsNullOrEmptyExtensions
{
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
@jfranmora
jfranmora / EditorCoroutineRunner.cs
Last active June 25, 2020 08:51
Class that permits run coroutines manually, can be useful to run coroutines in Unity Editor.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
public static class EditorCoroutineRunner
{
private static List<IEnumerator> _coroutineList = new List<IEnumerator>();
static EditorCoroutineRunner()
{
@jfranmora
jfranmora / ReadOnlyAttribute.cs
Last active November 22, 2023 16:12
Custom property attribute to make ReadOnly variables in Unity Inspector
using UnityEngine;
public class ReadOnlyAttribute : PropertyAttribute
{
}
using System.Collections;
using UnityEngine;
/// <summary>
/// GC friendly coroutine utilities
/// @JfranMora #UnityTips
/// </summary>
public static class CoroutineUtils
{
public static IEnumerator WaitForSeconds(float seconds)