基於NGUI的富文本實現--第一部分功能實現後的隨便說說

今天把第一階段的代碼實現了,達到了預期的效果; 在碼字的時候,沒有遇到較大的阻力,倒是有一個不知道該笑還是該哭的情況。 今天項目組裏面的大佬看我寫的是什麼東西之後,果斷的給我發了張圖片

大佬給我的圖片

這個插件居然在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了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章