Question

I need a special keyword in my code to be replaced by a consequence of symbols before the build.

For example, I want hello to be replaced by Debug.Log("Hello");

According to this, MonoDevelop didn't have this feature in 2006. Has anything changed?

If no, are there any plugins/external tools implementing it?

I don't think that switching to another IDE will be helpful unless it can use code completion for unity3d.

Please, don't answer that macro definitions are evil.


Update: I understood that my example was too abstract. In fact, I want to replace read("name"); with

var name;
name=gameObject.Find("name");
if(!name)
    return;
name=name.param;
Was it helpful?

Solution

1)Every script should have all needed variables declared + a variable called "self".

2)They should be public.

3)

public static function set_var(target,name:String,value)
{
    var fi=typeof(target).GetField(name);
    fi.SetValue(target,value);
}
public static function read(name:String,target):String
{
    set_var(target,"self",target);
    return "var rtmp=self.gameObject.GetComponent(\""+name+"\");"+"if(!rtmp)return;"+name+"=rtmp.param;"; 
}

4)eval(read("name",this));

5)As far as I know, it wouldn't work in unity C#


6)Probably, set_var can be replaced by assignment


Far better solution:

    var component_names = ["hello","thing","foo"];

    var component;
    for(var name:String in component_names)
    {
        component = gameObject.GetComponent(name);
        if(!component)
            return;
        set_var(this,name,component.param);
    }

(Requires set_var() from the first one)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top