NGUI中点击文字弹出物品的Tip

来源:互联网 发布:水泵扬程计算软件 编辑:程序博客网 时间:2024/06/06 07:25

用途描述:
文字的超链接,所谓的文字的超链接,就是需要点击如下:
德玛西亚获得了[绿色盔甲],然后你点击绿色盔甲,要弹出一个物品的介绍界面。

这个需求一般的会想到的做法是,在绿色盔甲的后面创建一个透明的image,然后为image设置collider,然后编写click事件处理函数。但是绿色盔甲的id呢?因为要根据这个id去设置tip的数据呀,所以这个有点难办了。

下面的解决方法是,仿照ngui中的[url=]http://blog.csdn.net/wodownload2/article[/url]
然后再label中你看不到url这些关键字的,原因是在NGUIText.cs中,进行了处理:

switch (sub6){        case "[/sub]":            sub = 0;            index += 6;        return true;        case "[/sup]":            sub = 0;            index += 6;        return true;        //这里就是过滤掉url关键字        case "[/url]":            index += 6;            return true;        //这里是自己添加的        case "[/xxx]":            index += 6;            return true;}

然后再看:

if (text[index + 1] == 'u' && text[index + 2] == 'r' && text[index + 3] == 'l' && text[index + 4] == '='){            int closingBracket = text.IndexOf(']', index + 4);            if (closingBracket != -1)            {                index = closingBracket + 1;                return true;            }            else            {                index = text.Length;                return true;            }}

这个就是屏蔽掉url的地方了。

仿照此处,再拷贝一份即可。比如上面添加的当遇到xxx的时候也不显示,则为:

 if (text[index + 1] == 'x' && text[index + 2] == 'x' && text[index + 3] == 'x' && text[index + 4] == '='){            int closingBracket = text.IndexOf(']', index + 4);            if (closingBracket != -1)            {                index = closingBracket + 1;                return true;            }            else            {                index = text.Length;                return true;            }}

最后如何获取[url=]xxxx.com[/url中的值呢?在UILabel.cs中你可以看到:

public string GetUrlAtCharacterIndex(int characterIndex){     if (characterIndex != -1 && characterIndex < mText.Length - 6)     {            int linkStart;            // LastIndexOf() fails if the string happens to begin with the expected text            if (mText[characterIndex] == '[' &&                mText[characterIndex + 1] == 'u' &&                mText[characterIndex + 2] == 'r' &&                mText[characterIndex + 3] == 'l' &&                mText[characterIndex + 4] == '=')            {                linkStart = characterIndex;            }            else linkStart = mText.LastIndexOf("[req=", characterIndex);            if (linkStart == -1) return null;            linkStart += 5;            int linkEnd = mText.IndexOf("]", linkStart);            if (linkEnd == -1) return null;            int urlEnd = mText.IndexOf("[/url]", linkEnd);            if (urlEnd == -1 || characterIndex <= urlEnd)                return mText.Substring(linkStart, linkEnd - linkStart);    }    return null;}

这个就是获取关键字[url=]xxxx.com[/url]中的xxxx.com的方法,所以你也可以拷贝一份即可:

public string GetUrlAtCharacterIndex2(int characterIndex){     if (characterIndex != -1 && characterIndex < mText.Length - 6)     {            int linkStart;            // LastIndexOf() fails if the string happens to begin with the expected text            if (mText[characterIndex] == '[' &&                mText[characterIndex + 1] == 'x' &&                mText[characterIndex + 2] == 'x' &&                mText[characterIndex + 3] == 'x' &&                mText[characterIndex + 4] == '=')            {                linkStart = characterIndex;            }            else linkStart = mText.LastIndexOf("[req=", characterIndex);            if (linkStart == -1) return null;            linkStart += 5;            int linkEnd = mText.IndexOf("]", linkStart);            if (linkEnd == -1) return null;            int urlEnd = mText.IndexOf("[/xxx]", linkEnd);            if (urlEnd == -1 || characterIndex <= urlEnd)                return mText.Substring(linkStart, linkEnd - linkStart);    }    return null;}

这样你就不用再考虑添加什么collider了,而是直接能够点击响应并能够获取,当前是哪个物品的id了。

0 0
原创粉丝点击