Skip to content

Instantly share code, notes, and snippets.

@DCFApixels
Last active June 27, 2024 17:45
Show Gist options
  • Save DCFApixels/69135c78ea598e05c417219da0a3651e to your computer and use it in GitHub Desktop.
Save DCFApixels/69135c78ea598e05c417219da0a3651e to your computer and use it in GitHub Desktop.
A mini-script to simplify the work of designers in a project on Unity.
using System.Runtime.CompilerServices;
using UnityEngine;
namespace DCFApixels
{
public static class DesignMode
{
internal static bool _isOn = false;
public static bool ON
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _isOn; }
}
public static bool OFF
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return !_isOn; }
}
}
[System.AttributeUsage(System.AttributeTargets.All, Inherited = false, AllowMultiple = false)]
public sealed class DesignOnlyAttribute : PropertyAttribute
{
public DesignOnlyAttribute()
{
order = int.MinValue;
}
}
[System.AttributeUsage(System.AttributeTargets.All, Inherited = false, AllowMultiple = false)]
public sealed class DevelopmentOnlyAttribute : PropertyAttribute
{
public DevelopmentOnlyAttribute()
{
order = int.MinValue;
}
}
}
#if UNITY_EDITOR
namespace DCFApixels.Editors
{
using UnityEditor;
[InitializeOnLoad]
internal static class DesignModeSwitcher
{
private const string _PREF_KEY_NAME = "DCFApixels_DesignMode_IsOn";
static DesignModeSwitcher()
{
int x = PlayerPrefs.GetInt(_PREF_KEY_NAME);
DesignMode._isOn = x > 0;
}
[MenuItem("Design Mode/Toggle")]
public static void ToggleDesignMode()
{
DesignMode._isOn = !DesignMode._isOn;
PlayerPrefs.SetInt(_PREF_KEY_NAME, DesignMode._isOn ? 1 : 0);
}
}
[CustomPropertyDrawer(typeof(DesignOnlyAttribute), true)]
internal class DesignOnlyAttributeDrawer : ViewModeDrawer<DesignOnlyAttribute> { }
[CustomPropertyDrawer(typeof(DevelopmentOnlyAttribute), true)]
internal class DevelopmentOnlyAttributeDrawer : ViewModeDrawer<DevelopmentOnlyAttribute> { }
internal class ViewModeDrawer<T> : PropertyDrawer
{
protected static bool _isDesignOnly = typeof(T) == typeof(DesignOnlyAttribute);
private static bool IsDraw
{
get { return _isDesignOnly ? DesignMode.ON : DesignMode.OFF; }
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (IsDraw)
{
return EditorGUI.GetPropertyHeight(property, label);
}
return 0f;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (IsDraw)
{
EditorGUI.PropertyField(position, property, label, true);
}
}
}
}
#endif


The script adds a mode switcher for designers and for developers in a panel at the top of the Unity window called Design Mode.

image

Through the DesignMode.ON property, you can get the bool value of whether design mode is enabled.



And also adds 2 attributes to customize the display of types in the inspector::

public class SomeMonoBehaviour : MonoBehaviour
{
    // The field is visible only in Design Mode.
    [DesignOnly] public int someField1;
    // Hides the field in Design Mode.
    [DevelopmentOnly] public int someField2;
}

DesignMode.ON == false image

DesignMode.ON == true image



To get the current value of DesignMode.ON without depending on this script, you can get it like this:

bool isDesignMode = PlayerPrefs.GetInt("DCFApixels_DesignMode_IsOn") == 0 ? false : true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment