[轉(zhuǎn)]腳本生成Preset Libraries

Preset Libraries它干的事就是把若干個顏色值保存起來。我們都知道顏色值用rgba來保存的。這樣拷貝起來就很麻煩了,如果說我把每個界面的顏色都做成模板,需要設置顏色的時候在模板里選擇多好?unity提供了Preset Libraries 就可以達到這個需求。 http://docs.unity3d.com/Manual/PresetLibraries.html。

但是問題來了,這東西不能通過腳本來自動化完成,總不能手動的一個一個設置吧。。。我想做的就是用腳本來創(chuàng)建Preset Libraries,找了半天也沒找到官方提供的API。那么沒辦法只能自己來了。直接上代碼。

using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
 
 
public class Test  {
 
 
    public class ColorData
    {
        public string name;
        public Color color;
    }
    
 
    [MenuItem("Tool/Creat Color")]
    static void Build () 
    {
 
        //復制一份新的模板到newColorPath目錄下
        string templateColorPath="Assets/Template/color.colors";
        string newColorPath="Assets/Editor/界面1.colors";
        AssetDatabase.DeleteAsset(newColorPath);
        AssetDatabase.CopyAsset(templateColorPath,newColorPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
 
 
    
        //這里我寫了兩條臨時測試數(shù)據(jù)
        List<ColorData>colorList = new List<ColorData>(){
            new ColorData(){name ="按鈕樣式1顏色",color = Color.green},
            new ColorData(){name ="按鈕樣式2顏色", color =new Color(0.1f,0.1f,0.1f,0.1f)}
 
        };
 
        UnityEngine.Object newColor = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(newColorPath);
        SerializedObject serializedObject = new SerializedObject(newColor);
        SerializedProperty property = serializedObject.FindProperty("m_Presets");
        property.ClearArray();
 
        //把我的測試數(shù)據(jù)寫進去
        for(int i =0; i< colorList.Count; i++){
            property.InsertArrayElementAtIndex(i);
            SerializedProperty colorsProperty = property.GetArrayElementAtIndex(i);
            colorsProperty.FindPropertyRelative("m_Name").stringValue = colorList[i].name;
            colorsProperty.FindPropertyRelative("m_Color").colorValue = colorList[i].color;
        }
        //保存
        serializedObject.ApplyModifiedProperties();
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
 
}

因為我并不知道它的.colors的序列化類對象結(jié)構(gòu)。所以我在Color界面中Presets->Create New Library,Location中選擇本地Project。這時候.colors文件就生成在Editor下面了,然后我把它拷貝到Template文件夾下用來做我的模板。 然后我通過這個模板,再加上顏色的數(shù)據(jù)信息來生成我的.colors文件數(shù)據(jù),圖中“界面1”就是我生成出來的。 (這里可以用中文)

生成出來之后,在選擇顏色的時候就可以設置模板里的顏色啦。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,716評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,022評論 4 61
  • 若現(xiàn)在有人問你的夢想是什么,你要不就覺得像是歌唱節(jié)目導師的問話,要不就是嗤之以鼻嘲笑你還說夢想,或者你會發(fā)現(xiàn)你沒有...
    我是狐貍一只閱讀 229評論 0 2
  • 看了老師關(guān)于貓的故事,不由自主地勾起了我傷心的回憶…… 舉世矚目的唐山大地震前,我在上小學。我家離學校很近,有一位...
    海迪哲lshj閱讀 332評論 3 5
  • 挫敗感會累積 累積到?jīng)]有了驕傲 沒有沾沾自喜春風得意
    GUIYEWANSHAN閱讀 126評論 0 0

友情鏈接更多精彩內(nèi)容