优质中文开源软件代码项目资源技术共享平台
传播开源的理念,推广开源项目
学习是对自己最棒的投资!与君共勉!

网站首页 > 游戏开发 正文

Unity3D研究院编辑器之脚本生成Preset Libraries(十四)

longtao100 2022-04-24 00:12:37 游戏开发 410 ℃ 0 评论

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”就是我生成出来的。 (这里可以用中文)

生成出来之后,在选择颜色的时候就可以设置模板里的颜色啦。

 

 

 


Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

请填写验证码
开源分类
最近发表
开源网标签
开源网归档