Search Unity

Accessing Unity's saved palettes

Discussion in 'Scripting' started by petey, Jan 24, 2018.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi All,

    Hey I just noticed that if you save a palette in unity it gets stored in what looks like a scriptable object -



    Just wondering, is there a way to access the colours stored within it?
    Could be really handy!

    Thanks!
    Pete
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    You can set up a thing to figure out the type of whatever you've clicked:

    Code (csharp):
    1. public static class EditorCommands {
    2.  
    3.     [MenuItem("Commands/Get Type Of Selected")]
    4.     public static void GetTypeOfSelected() {
    5.         Debug.Log(Selection.activeObject?.GetType().Name);
    6.     }
    7. }
    Using that, you'll find that the type of a palette is a ColorPresetLibrary. Now, is that a type we can work with? There's several ways you can do it, but if you have a code editor that's competent, you can just search for the name and find a decompiled version of the type:

    Code (csharp):
    1.  
    2. namespace UnityEditor
    3. {
    4.   internal class ColorPresetLibrary : PresetLibrary
    5.    ...
    6. }
    Well, it's internal, so we don't have access to it from scripts. Time to just open the asset in a text editor and check what it looks like:

    Code (csharp):
    1.  
    2. %YAML 1.1
    3. %TAG !u! tag:unity3d.com,2011:
    4. --- !u!114 &1
    5. MonoBehaviour:
    6.   m_ObjectHideFlags: 52
    7.   m_PrefabParentObject: {fileID: 0}
    8.   m_PrefabInternal: {fileID: 0}
    9.   m_GameObject: {fileID: 0}
    10.   m_Enabled: 1
    11.   m_EditorHideFlags: 1
    12.   m_Script: {fileID: 12323, guid: 0000000000000000e000000000000000, type: 0}
    13.   m_Name:
    14.   m_EditorClassIdentifier:
    15.   m_Presets:
    16.   - m_Name:
    17.     m_Color: {r: 1, g: 1, b: 1, a: 1}
    18.   - m_Name:
    19.     m_Color: {r: 0.9705882, g: 0.007136685, b: 0.007136685, a: 1}
    20.   - m_Name:
    21.     m_Color: {r: 0.13559689, g: 0.4712593, b: 0.5588235, a: 1}
    22.  
    Well, that's easy to work with! I'd suggest just grabbing that text file, look for all lines starting with "m_Color", and parse them into colors. It should be pretty straight-forward to create that as a helper-method. Give a call if you need more input.
     
    neonblitzer and petey like this.
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824