-
-
Save anchan828/ee29e9815353dea42ec3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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