Skip to content

Instantly share code, notes, and snippets.

@Biodam
Last active November 20, 2018 20:37
Show Gist options
  • Save Biodam/dc2c370fcbe3f14d56751d3ffea1b05d to your computer and use it in GitHub Desktop.
Save Biodam/dc2c370fcbe3f14d56751d3ffea1b05d to your computer and use it in GitHub Desktop.
Apply presets to scriptable objects
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditor.Presets;
using System;
[CreateAssetMenu(fileName = "PreBuild", menuName = "Data/CI/PreBuild")]
public class PreBuild : ScriptableObject
{
[Serializable]
private struct DefaultData
{
public Preset preset;
public ScriptableObject scriptableObject;
}
[SerializeField] private DefaultData[] presetsToApplyBeforeBuild;
public void ApplyPresets()
{
foreach (var item in presetsToApplyBeforeBuild)
{
if (item.preset != null && item.scriptableObject != null)
{
if (item.preset.CanBeAppliedTo(item.scriptableObject))
{
item.preset.ApplyTo(item.scriptableObject);
EditorUtility.SetDirty(item.scriptableObject);
}
else
{
Debug.LogError("Cannot apply " + item.preset.name + " to " + item.scriptableObject.name);
}
}
else
{
Debug.LogError("PreBuild preset or scriptable object is null, skipping.");
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
[CustomEditor(typeof(PreBuild))]
public class PreBuildInspector : Editor
{
private bool showDefaultInspector;
private SerializedProperty presetsToApplyBeforeBuild;
private ReorderableList list;
private void OnEnable()
{
presetsToApplyBeforeBuild = serializedObject.FindProperty("presetsToApplyBeforeBuild");
SetupReorderableList();
}
private void SetupReorderableList()
{
list = new ReorderableList(serializedObject, presetsToApplyBeforeBuild);
list.headerHeight = EditorGUIUtility.singleLineHeight * 2;
list.drawHeaderCallback = (Rect rect) =>
{
rect.height = EditorGUIUtility.singleLineHeight;
GUI.Label(rect, presetsToApplyBeforeBuild.displayName);
rect.y += EditorGUIUtility.singleLineHeight;
GUI.Box(rect, GUIContent.none);
rect.x += 20;
GUI.Label(rect, "Presets Apply to -> ScriptableObject");
};
list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
var element = presetsToApplyBeforeBuild.GetArrayElementAtIndex(index);
var preset = element.FindPropertyRelative("preset");
var scriptableObject = element.FindPropertyRelative("scriptableObject");
rect.width /= 2;
EditorGUI.PropertyField(rect, preset, GUIContent.none);
rect.x += rect.width;
EditorGUI.PropertyField(rect, scriptableObject, GUIContent.none);
};
}
public override void OnInspectorGUI()
{
showDefaultInspector = GUILayout.Toggle(showDefaultInspector, "Show default inspector");
if (showDefaultInspector)
base.OnInspectorGUI();
serializedObject.Update();
if (list != null)
list.DoLayoutList();
if (GUILayout.Button("Execute this.ApplyPresets"))
((PreBuild)target).ApplyPresets();
serializedObject.ApplyModifiedProperties();
}
}
public static void ApplyPreBuild()
{
var guid = AssetDatabase.FindAssets("PreBuild t:PreBuild", null);
if (guid.Length == 1)
{
var prebuild = AssetDatabase.LoadAssetAtPath<PreBuild>(AssetDatabase.GUIDToAssetPath(guid[0]));
if (prebuild != null)
{
prebuild.ApplyPresets();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment