您的位置:首页 > 移动开发 > Unity3D

Unity3D研究院编辑器之脚本生成Preset Libraries

2015-11-13 11:40 489 查看


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

 雨松MOMO 【Unity3D拓展编辑器】 围观499次 4条评论 编辑日期:2015-10-29 字体:大 中 小

<iframe id="cproIframe_u1121907_1" width="300" height="250" src="http://pos.baidu.com/acom?adn=4&at=134&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=18&ch=0&col=zh-CN&conOP=0&cpa=1&dai=1&dis=0&layout_filter=rank%2Ctabcloud&ltr=http%3A%2F%2Fwww.xuanyusong.com%2Farchives%2Fcategory%2Funity%2Funity3deditor&ltu=http%3A%2F%2Fwww.xuanyusong.com%2Farchives%2F3711&lunum=6&n=92004029_cpr&pcs=1920x946&pis=10000x10000&ps=369x1329&psr=1920x1080&pss=1920x409&qn=2c588d3b116eaffc&rad=&rsi0=300&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=1121907&tn=text_default_300_250&tpr=1447385594307&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1121907&ti=Unity3D%E7%A0%94%E7%A9%B6%E9%99%A2%E7%BC%96%E8%BE%91%E5%99%A8%E4%B9%8B%E8%84%9A%E6%9C%AC%E7%94%9F%E6%88%90Preset%20Libraries%EF%BC%88%E5%8D%81%E5%9B%9B%EF%BC%89%20%7C%20%E9%9B%A8%E6%9D%BEMOMO%E7%A8%8B%E5%BA%8F%E7%A0%94%E7%A9%B6%E9%99%A2&tt=1447385594265.43.70.73" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; font-family: inherit;"></iframe>

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

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



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





 

 

 

本文固定链接: http://www.xuanyusong.com/archives/3711

转载请注明: 雨松MOMO 2015年10月29日 于 雨松MOMO程序研究院 发表
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: