网站首页 > 游戏开发 正文
Preset Libraries它干的事就是把若干个颜色值保存起来。我们都知道颜色值用rgba来保存的。这样拷贝起来就很麻烦了,如果说我把每个界面的颜色都做成模板,需要设置颜色的时候在模板里选择多好?unity提供了Preset Libraries 就可以达到这个需求。 http://docs.unity3d.com/Manual/PresetLibraries.html。
但是问题来了,这东西不能通过脚本来自动化完成,总不能手动的一个一个设置吧。。。我想做的就是用脚本来创建Preset Libraries,找了半天也没找到官方提供的API。那么没办法只能自己来了。直接上代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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 ( ) ;
//这里我写了两条临时测试数据
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 ( ) ;
//把我的测试数据写进去
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的序列化类对象结构。所以我在Color界面中Presets->Create New Library,Location中选择本地Project。这时候.colors文件就生成在Editor下面了,然后我把它拷贝到Template文件夹下用来做我的模板。 然后我通过这个模板,再加上颜色的数据信息来生成我的.colors文件数据,图中“界面1”就是我生成出来的。 (这里可以用中文)
生成出来之后,在选择颜色的时候就可以设置模板里的颜色啦。
猜你喜欢
- 2022-04-24 Unity3D研究院之通过ipa或apk获取游戏所使用的unity和Xcode版本
- 2021-09-07 Unity3D研究院之系统内置系统图标大整理
- 2021-09-06 Unity3D研究院编辑器之5.3JSON的序列化
- 2021-09-06 Unity3D研究院编辑器之脚本打开SpritePacker窗口(十七)
- 2021-09-06 #你好Unity3D#Hierarchy视图监听gameObject点击事件
- 2021-09-04 Unity3D研究院编辑器之自定义默认资源的Inspector面板(十)
- 2021-09-04 Unity3D研究院之两个游戏工程资源同步问题(八十六)
- 2021-09-04 Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五)
- 2021-08-30 Unity3D研究院编辑器之独立Inspector属性(九)
- 2021-08-30 Unity3D研究院之悬浮框显示其他游戏电流功耗(一百二十六)
本文暂时没有评论,来添加一个吧(●'◡'●)