Skip to content

Instantly share code, notes, and snippets.

@anchan828
Created July 12, 2013 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anchan828/ee29e9815353dea42ec3 to your computer and use it in GitHub Desktop.
Save anchan828/ee29e9815353dea42ec3 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public class PresetLibraryHelper
{
public static Type presetLibraryType = GetType ("UnityEditor.PresetLibrary");
static Type GetType (string typeName)
{
return Types.GetType (typeName, "UnityEditor.dll");
}
public static string GetName (object presetLibraryObject, int index)
{
MethodInfo info = presetLibraryType.GetMethod ("GetName", new []{typeof(int)});
object obj = info.Invoke (presetLibraryObject, new object[]{index});
return obj != null ? obj.ToString () : "";
}
public static int Count (object presetLibraryObject)
{
MethodInfo info = presetLibraryType.GetMethod ("Count");
object obj = info.Invoke (presetLibraryObject, new object[0]);
return obj != null ? (int)obj : 0;
}
public static T GetPreset<T> (object presetLibraryObject, int index)
{
MethodInfo info = presetLibraryType.GetMethod ("GetPreset", new []{typeof(int)});
System.Object obj = info.Invoke (presetLibraryObject, new object[]{index});
return obj != null ? (T)obj : default(T);
}
}
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public class Test
{
[DidReloadScripts]
static void OnDidReloadScripts ()
{
Color color = DoTest<Color> ("Assets/Editor/EditorColors.colors");
Debug.Log (color);
Gradient gradient = DoTest<Gradient> ("Assets/Editor/EditorGradients.gradients");
Debug.Log (gradient.colorKeys);
}
static T DoTest<T> (string path)
{
Object presetLibraryObject = AssetDatabase.LoadAssetAtPath (path, PresetLibraryHelper.presetLibraryType);
if (presetLibraryObject == null)
return default(T);
int count = PresetLibraryHelper.Count (presetLibraryObject);
Debug.Log (count);
string name = PresetLibraryHelper.GetName (presetLibraryObject, 0);
Debug.Log (name);
return PresetLibraryHelper.GetPreset<T> (presetLibraryObject, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment