Custom tags with additional info

36 views
Skip to first unread message

michal.r...@krej.net

unread,
Mar 16, 2016, 8:53:41 AM3/16/16
to Developer Support
Hello,

Is there a possibility to define custom tags with my own information? I would like to create something like this:

<tag1 id="1">ABC</tag1>
<tag2 name="asd" width="120">CBA</tag2>

I need additional parameters to perform my own OnClick action. It would be great if i could do it by code only.

Regards,
Michał

Developer Support

unread,
Mar 16, 2016, 10:59:51 AM3/16/16
to Developer Support
Cześć Michał!

The recommended way to achieve this is to define a custom link class with the styling you need and then simply use a JSON string as the value for the name attribute. You can then get whatever information you need from this JSON string in your event handler.

For example, set your text strings like this:

<a name="{\"id\":1}" class="tag1">ABC</a>
<a name="{\"name\":\"asd\", \"width\":120}" class="tag2">CBA</a>

And then in your event handler something like this

public void OnClick(HyperText sender, HyperText.LinkInfo link)
{
MyClass myObject = JsonUtility.FromJson<MyClass>(link.Name);
}

michal.r...@krej.net

unread,
Mar 17, 2016, 6:15:13 AM3/17/16
to Developer Support
Thank you for your response! I thought about it but was hoping it exists different solution.

I also would like to ask if it is possible to set hyperlinks to use text color I will specify? I sometimes need to generate few links with user defined color.

Developer Support

unread,
Mar 18, 2016, 12:06:40 PM3/18/16
to Developer Support
There are possibly a few approaches you could use depending on the specifics. Could you tell me more about the user story you are wanting to implement and maybe I can recommend an approach?

michal.r...@krej.net

unread,
Mar 23, 2016, 9:58:11 AM3/23/16
to Developer Support
I have object with HyperText component. In each line there is user nick, which is link and should be colored with color loaded from database. I would prefer to do it by code only.

Developer Support

unread,
Mar 23, 2016, 10:40:07 AM3/23/16
to Developer Support
So I guess there's two potential options here, depending on how much flexibility you have with styling
  1. If there is a defined set of colors the user can pick, then just have one link subclass for each specific color they can pick. This option gives you the most control over color styling.
  2. If the user can pick any color whatsoever, you can enclose a color tag inside of a link (e.g., "New <a name="link"><color=#ff0000ff>HyperText</color></a>"). To make this mode work for you, you need to change the link style's Color Tint Mode to something other than Constant on your style sheet. In Constant mode, the underlying color specified by your color tag gets completely overwritten by the state color. In e.g., Multiplicative mode, each state color is multiplied with the underlying color specified in your manual color tag. Just like the built-in Button component, you can also use the Color Multiplier slider to get overbright results. So, for example, you could use a 2.0 Color Multiplier, with 50% grey for your normal color and something like 80% grey for the highlight color. (I've attached an image to give you an idea for a starting point.)
Screen Shot 2016-03-23 at 9.38.57 AM.png

baop...@gmail.com

unread,
Feb 8, 2017, 10:08:05 PM2/8/17
to Developer Support
Just want to chip in for anyone who uses this as part of the docs.
In Unity 5.5, the Json Object syntax no longer takes escape sign "\", so the right syntax is:
Reply all
Reply to author
Forward
0 new messages