今天把第一阶段的代码实现了,达到了预期的效果; 在码字的时候,没有遇到较大的阻力,倒是有一个不知道该笑还是该哭的情况。 今天项目组里面的大佬看我写的是什么东西之后,果断的给我发了张图片
这个插件居然在Unity商店有售!
在开发之初,我在商店里面实用关键词RichLabel查询过,可是没有找到相关的插件,所以才会想捣腾出这套插件来。 现在发现这个插件早就有人开发出来了,我心里还是觉得挺尴尬的,反复造轮子了都。 再看看插件的价格,这价格还不菲,零售要卖到25刀。我心里又挺乐滋滋的,毕竟价格不低。
全权当一个乐趣点吧!我是不会听到这个消息后就停止继续开发这个插件的,毕竟我开发的东西有市场,而且卖的价格还不低。^ v ^
即使不卖钱,当作练手也挺不错的,毕竟决定开发的初衷就是锻炼开发能力,而不是为了卖钱。
不说废话了,说说今天的开发的代码吧! 整个功能,我划分成两部分:一是能够通过给出的富文本字符串生成UI,另外一部分就是正确将UI组合排版。
呈上第一部分成果的效果图:
//调用过程
public string rich_txt = @"
<label font = 'SciFi/SciFi Font - Header', fontsize = '20',fontstyle = 'Normal',text = 'First Label' />
<sprite atlas_name = 'SciFi/SciFi Atlas',sprite_name = 'NGUI'/>
<spamin atlas_name = 'Wooden Atlas',sprite_name = 'Button',prefix_sprite_name = 'Button',loop = 'true'/>
<texture texture_name = 'Wooden Atlas',width = '768',m_width = '456'/>
<label font = 'SciFi/SciFi Font - Normal', fontsize = '22',fontstyle = 'Normal',text = 'Second Label' />
";
void OnGUI()
{
if(GUILayout.Button("TestRich"))
{
RichLabelMgr.CreateNewRichLab(rich_txt, this.transform, new Vector2(200, 200), NGUIText.Alignment.Automatic, (string id) =>
{
Debug.Log("richevent " + id);
});
}
}
执行后的结果:
效果不错,而且更兴奋的是,在功能分析当中,很多难点都分析到了。
截止目前,程序还是有些地方让我感觉不妥。
第一个就是解析的问题:
// 在RichLabelComponent中的定义的
internal static readonly Dictionary<RichComponentType, RichTxtSign> rich_txt_sign_dict = new Dictionary<RichComponentType, RichTxtSign>{
{RichComponentType.Label,new RichTxtSign("<label","/>")},
{RichComponentType.Sprite,new RichTxtSign("<sprite","/>")},
{RichComponentType.SpriteAmin,new RichTxtSign("<spamin","/>")},
{RichComponentType.Texture,new RichTxtSign("<texture","/>")},
};
// 在RichLabelComponent中的 Analytic 方法
protected virtual bool Analytic(string rich_txt)
{
string[] rich_txts = rich_txt.TrimStart(' ').TrimEnd(' ').Split(',');
}
// 还有在RichLabelComponent中的 GetPropertyValue 方法
protected internal static string[] GetPropertyValue(string txt)
{
string[] property = new string[2];
property = txt.Split('=');
if (property.Length != 2)
{
return null;
}
// 移除等号,属性值开始和结束时的'号
property[0] = property[0].TrimStart(' ').TrimEnd(' ');
var temp_str = property[1];
temp_str = temp_str.TrimStart(' ').TrimEnd(' ');
if (temp_str.StartsWith("'"))
{
temp_str = temp_str.Remove(0, 1);
}
if (temp_str.EndsWith("'"))
{
temp_str = temp_str.Remove(temp_str.Length - 1, 1);
}
property[1] = temp_str;
return property;
}
// 以及各个RichLabelComponent的子类的SetProperty方法
/*
以上的字符串解析的时候,基本上都属于写死的,这里的解决方法很不优美,该怎么样优美的方法去处理解析的问题呢?
其实有想法用配置表去配置,即使是不配置,那也应该集中在一起,用readonly,或者const去做一个宏定义才对!
*/
/*还有一个比较绕的地方,那就是onclick的调用过程*/
public class RichLabelEvent
{
public void Init()
{
if(m_comp.m_ui_widget != null)
{
if(m_can_click) listener.onClick = OnClick;
}
}
public void OnClick(GameObject go)
{
this.m_comp.ctrl.OnClick();
}
}
public class RichLabelCtrl
{
internal void OnClick(string id){
foreach(var comp in m_comps_list)
if(comp.m_rich_event.CanClick && comp.m_rich_event.Id.Equals(id)) comp.OnClick();
if (this.m_call_back != null) m_call_back(id);
}
}
/*
调用过程:RichLabelEvent.Onclick => RichLabelCtrl.Onclick => RichLabelComponent.Onclick
这里写法的实现方法就有点死循环和难维护,难改动的苗头,
现在还不知道该怎么改,还有怎么样优美的实现调用过程!
*/
今天就只做了这些了,我会继续完善的。
如果明天有时间,估计能够将排版功能给完成吧!
纯属练手,欢迎讨论
工程已经上传到git了