Taking advantage of C# metadata attributes in Javascript

Hi there,

I have a fairly large project on the go with multiple developers working in either C#, Javascript or both. The topic of using metadata attributes has come up.

I know we can apply C#-defined attributes to methods in our C# scripts. The question is, does there exist a direct analog of C# attributes in Javascript? Here’s an example attribute.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ScriptParameterAttribute : System.Attribute
{
	public string name;
	public string values;
	
	public ScriptParameterAttribute(string paramName, params string[] paramValues)
	{
		name = paramName;
		values = paramValues;
	}
}

Say I’ve defined the above attribute in C#, which I can use to provide metadata about what values a script’s parameters are allowed to have. I want to be able to tag Javascript functions with this attribute, so that when I’m using Reflection in C# to inspect methods, I can see attributes declared for Javascript functions as well as C# ones.

Can this be done? I know you can tag Javascript functions with @RPC, @script, etc, but how about custom attributes like this?

Thanks,

Ahonek

Looking a little more into the tags like @RPC and such, I can see that Javascript has some sort of support for this as well, but try as I might I can’t find details on this online. Even the Mono documentation only talks about it in C# terms.

If I wanted to implement the same attribute as above, but in JS, could I do that? I guess it would be done somewhat like in C#, by subclassing the Attribute class, but how would that be done?

Thanks,

Ahonek

UnityScript does not exist outside of unity so you won’t find any docs outside here.
But you can try to just “map what you know” to what you find on mono or msdn for C# and see if it works.

If it works, then fine :slight_smile:
if it doesn’t its a mechanism not offered by UnityScript which would force you to use C# for that (if you put it in plugins folder its for most areas of your code as if it was something present by default in unity / .NET)

OK, so I got to looking at this. All I can tell you is that with a little work, you can use the C# attributes in the core Javascript side. I took your program and changed it a bit to get rid of errors. Since I am not the total .net guru on this and I have no idea what the implimentation of the AllowMultiple = true could be used for, and it just gave errors anyways, I commented it out. The rest works pretty good as I have it here:

// ScriptParameterAttribute.cs
using UnityEngine;
using System.Collections;

//[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ScriptParameterAttribute : System.Attribute
{
	public string name;
	public string[] values;
	
	public ScriptParameterAttribute(string paramName, params string[] paramValues)
	{
		name = paramName;
		values = paramValues;
	}
}

// myCode.js
function Start(){
	var a=ScriptParameterAttribute("name", "this", "is", "a", "test");
	print(a.name);
	print(a.values[2]);
}

Thanks for your help. Once you pointed out the plugins folder, I eventually realized that my Standard Assets folder (where I was trying to put the C# source files) was in the wrong place! Another developer had moved it before we realized we might need it.

In any case, now everything is working correctly. For any interested parties, the analogy goes like this:

[ScriptParameter("paramName", "possibleValue")]

is to C# as:

@ScriptParameter("paramName", "possibleValue")

is to Javascript. Even easier than I thought!

Cheers

Ahonek

Edit: BigMisterB beat me to it. Thanks BMB, and it turns out the only real problem I was having was the location of the Standard Assets folder to make the ScriptParameterAttribute type visible.

YW, forgot to mention that I moved the script into the Standard Assets because it wasn’t working right to begin with, Plugins works too. I guess it just has to be compiled before the rest of the code.

Oddly, the whole thing reminds me of a Hashtable. Since it really is just an array of values.