如何扩展方法工作的? [英] How do extension methods work?

查看:85
本文介绍了如何扩展方法工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Unity3d扩展方法的类的Vector3。但是我不太似乎得到它。这是我有:

 公共静态类ExtensionMethods {    公共静态的Vector3的MaxValue(此的Vector3 _vec3)
    {
        返回新的Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
    }
}

现在我想打一个 Vector3.MaxValue 就像 float.MaxValue 这一行$ C $的C:

 的Vector3 closestPoint = Vector3.MaxValue;

但后来我得到这个错误:

 错误CS0117:'UnityEngine.Vector3'不包含定义'的MaxValue

我知道这会工作:

 的Vector3 closestPoint =新的Vector3()MaxValue的()。

但后来我创建2个新的的Vector3 实例。一个在 MaxValue的呼叫,一组在外面。是不是有可能只是做一个,用这种code的做到这一点:

 的Vector3 closestPoint = Vector3.MaxValue;


解决方案

首先,这里有一个非常快的教程扩展对于那些学习C#/团结的人使用Google这里

请新建一个文本文件 HandyExtensions.cs 这样的...(请注意,有点混乱,你可以在所有使用任何名称其中持有您的扩展类 - 类的实际名称是根本就不会使用,是无关紧要的)...

 使用UnityEngine;
 System.Collections中使用;
 使用System.Collections.Generic; 公共静态类HandyExtensions
     {
     公共静态浮动摇动(本浮动FF)
         {
         返回FF * Random.Range(0.9F,1.1F);
         }
     }

您现在已经作出了新的命令微动()。您可以在任何浮动使用轻摇。看到它说:这个浮动 FF。因为它说,浮动就在那里,该扩建工程的彩车。

正如你所看到的,摇动()扩展将采取浮动略有改变它。尝试像这样

 浮动X = 3.5F;
 的debug.log(x为+ X);
 的debug.log(x是+ x.Jiggled());
 的debug.log(x是+ x.Jiggled());
 的debug.log(x是+ x.Jiggled());
 的debug.log(x是+ x.Jiggled());
 的debug.log(x是+ x.Jiggled());

做这样的一些测试,直到你有扩展的一个很好的理解。学习尽可能多地了解扩展!这里是另一个教程。

现在此页面上的问题! ...


作为的Mikael解释说,你要找的是不是真正的延伸。 (另请注意,如果你想的 的添加一个字段的Vector3,你不能这样做,在C#)。

现在,如果你想返回那件事,你需要的包括静态类名称的前面,你不让它的扩展。这只是一个静态的正常通话。

错了...

 公共静态的Vector3的MaxValue(此的Vector3 _vec3)
{
    返回新的Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
}

正确...

 公共静态的Vector3的MaxValue()
{
    返回新的Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
}

那么你就可以做到这一点...

 的debug.log(ExtensionMethods.MaxValue());

我觉得可能是你在找什么在那里。

你通常称之为 ExtensionMethods 短的东西像便捷所以你可以写

  Handy.MaxValue();

事实上,因为你不能把的Vector3前面,这样的事情会更清楚:

  Handy.MaxVector3();

有道理?

如果你想这样写: Vector3.MaxValue()你不能这样做,......因为的Vector3部分的是一类的(或结构),而不是一个变量。例如,这是一个整数的扩​​展:

  375.Jiggle();

这是一个整数的扩​​展:

  INT X;
 x.Jiggle();

,但这是没有意义的

  int.Jiggle(); // 无意义的

要重复为清楚起见,请注意您问:


  

我想在Unity3d扩展方法的类的Vector3 [实际上是一个结构]


扩展只能被称为在实际变量,而不是类或结构。

 的Vector3英雄;
   敌人的Vector3;
   hero.Jiggle(); //伟大工程
   enemy.Jiggle(); //伟大工程   Vector3.Jiggle(); //语法错误 - 无意义

请注意过,作为在了Mikael他的回答居然提到,你其实可以真正做到这一点:

 (新的Vector3())微动()。

如果你仔细想想是有道理的,太。其中一个在C#中我最喜欢的事情是,你可以使用扩展在普通的旧常量

  14.Jiggle();
 7.5f.Radians();

您已经有了爱,除非你只是得到从生活没有乐趣。这里是一个例子:

 公共静态颜色有色(这个浮动α,INT R,诠释克,INT B)
    {
    返回新颜色(
        (浮点)R / 255F,
        (浮点)克/ 255F,
        (浮点)B / 255F,
        α );
    }

您可以接着写.. 颜色C = .5f.Colored(.2f,.4f,.2f); ,这是一个pucey颜色的alpha .5f。

我一直认为这将是巨大的,如果的你问有关语法可用的,如果你能有一个类型或类莫名其妙的东西你延长 - 但它并非如此。对于你正在尝试做的,你必须只使用一个方便的字,如,同时,得心应手,而做到这一点。

大的Vector3 = Handy.MaxVec3();

我希望这说明了情况!


再为任何需要扩展的总体介绍,介绍。下面是一些我最喜欢的扩展。

类似下面是每个游戏项目中使用...

 公共静态持股量微动(本浮动FF)
    {
    返回FF * UnityEngine.Random.Range(0.9F,1.1F);
    }公共静态浮动PositiveOrNegative(本浮动FF)
    {
    INT R = UnityEngine.Random.Range(0,2);
    如果(R == 0)返回FF;
    返回-ff;
    }公共静态布尔有时(这INT N)
    {
    如果(N< = 1)返回true;
    INT R = UnityEngine.Random.Range(0,N);
    返程(R == 0);
    }

例使用..

 如果(10.Sometimes())
   {
   飞船爆炸
   }

它只做一次在十倍以上。

 如果(3.Sometimes())
   {
   激光闪耀
   }

在这个例子中它只做它的左右时间的三分之一。

(注意,通常确实您的扩展使用泛型在可能的情况。这里没有显示为简单起见。)

下面是一个方便的扩展把戏。这里是一个非常高级扩展

扩展是在Unity工程中普遍存在。

如果你是很多人学习code使用Unity之一... pretty多,你应该掌握的第一件事就是扩展。尝试做的几乎所有的带有扩展名。有时使用Unity时,几乎每一个code线需要一个或多个扩展。尽情享受吧!

I want to make an extension method in Unity3d for the Vector3 class. But I don't quite seem to get it. This is what I have:

public static class ExtensionMethods{

    public static Vector3 MaxValue(this Vector3 _vec3)
    {
        return new Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
    }
}

Now I want to make a Vector3.MaxValue just like float.MaxValue with this line of code:

Vector3 closestPoint = Vector3.MaxValue;

But then I get this error:

error CS0117: `UnityEngine.Vector3' does not contain a definition for `MaxValue'

I know that this would work:

Vector3 closestPoint = new Vector3().MaxValue();

But then I create 2 new Vector3 instances. One in the MaxValue call and one outside. Isn't it possible to just make one and do it with this kind of code:

Vector3 closestPoint = Vector3.MaxValue;

解决方案

First, here's a very quick tutorial on extensions for those learning c#/Unity for anyone googling here:

Make a new text file HandyExtensions.cs like this ... (note that somewhat confusingly, you can use any name at all for the class which "holds" your extensions - the actual name of the class is never used at all, and is irrelevant) ...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public static class HandyExtensions
     {
     public static float Jiggled(this float ff)
         {
         return ff * Random.Range(0.9f,1.1f);
         }
     }

You have now made a new "command" Jiggle(). You can use jiggle on any float. See where it says "this float ff". Because it says "float" just there, this extension works on floats.

As you can see, the Jiggled() extension will take a float and change it slightly. Try it like this

 float x = 3.5f;
 Debug.Log("x is " + x);
 Debug.Log("x is " + x.Jiggled() );
 Debug.Log("x is " + x.Jiggled() );
 Debug.Log("x is " + x.Jiggled() );
 Debug.Log("x is " + x.Jiggled() );
 Debug.Log("x is " + x.Jiggled() );

Do some tests like that until you have a good understanding of extensions. Learn as much as you can about extensions! Here's another tutorial.

And now the question on this page! ...


As Mikael explains, what you are looking for is not actually an extension. (Also note, if you're trying to add a field to Vector3, you cannot do that in c#.)

Now, if you want to return "that thing" you need to include the static class name up front, and you DO NOT make it an extension. It's just a normal call to a static.

wrong...

public static Vector3 MaxValue(this Vector3 _vec3)
{
    return new Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
}

correct...

public static Vector3 MaxValue()
{
    return new Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
}

you can then do this ...

Debug.Log(   ExtensionMethods.MaxValue()  );

I think that may be what you're looking for there.

You'd normally call ExtensionMethods something short like Handy so you can write

 Handy.MaxValue();

indeed, since you can't put the "Vector3 up front", something like this would be more clear:

  Handy.MaxVector3();

makes sense?

If you're trying to write something like this: Vector3.MaxValue() you can't do that ... because the "Vector3" part IS A CLASS (or Struct), not a variable. For example, this is an extension on an integer:

 375.Jiggle();

and this is an extension on an integer:

 int x;
 x.Jiggle();

but this is meaningless:

 int.Jiggle();  // meaningless

To repeat for clarity, note that you asked:

I want to make an extension method in Unity3d for the Vector3 class [actually a struct]

extensions can only be called on actual variables, not on "the class or struct".

   Vector3 hero;
   Vector3 enemy;
   hero.Jiggle();  // works great
   enemy.Jiggle();  // works great

   Vector3.Jiggle();  // syntax error - meaningless

Note too that as Mikael actually mentions in his answer, you can in fact actually do this:

  (new Vector3()).Jiggle();

If you think about it that makes sense, too. One of my favorite things in c# is that you can use extensions on plain old constants:

 14.Jiggle();
 7.5f.Radians();

You've got to love that unless you just get no pleasure from life. Here's an example of that:

public static Color Colored( this float alpha, int r, int g, int b )
    {
    return new Color(
        (float)r / 255f,
        (float)g / 255f,
        (float)b / 255f,
        alpha );
    }

You can then write .. Color c = .5f.Colored(.2f,.4f,.2f); and that is a pucey color with an alpha of .5f.

I've always thought it would be great if the syntax you ask about was available, if you could have a "type" or "class" somehow as the "thing" you extend on - but it's not the case. For what you are trying to do, you have to just use a handy word such as, well, Handy, and do this

Vector3 big = Handy.MaxVec3();

I hope this explains the situation!


Again for anyone needing a general intro to extensions, intro. Here are some of my favorite extensions.

Something like the following is used in every game project...

public static float Jiggle(this float ff)
    {
    return ff * UnityEngine.Random.Range(0.9f,1.1f);
    }

public static float PositiveOrNegative(this float ff)
    {
    int r = UnityEngine.Random.Range(0,2);
    if (r==0) return ff;
    return -ff;
    }

public static bool Sometimes(this int n)
    {
    if (n<=1) return true;
    int r = UnityEngine.Random.Range(0,n);
    return (r==0);
    }

Example uses..

 if ( 10.Sometimes() )
   {
   explode spaceship
   }

it only does it once in ten times.

 if ( 3.Sometimes() )
   {
   laser sparkle
   }

in that example it only does it about a third of the time.

(Note that indeed typically you make extensions use generics where possible. Not shown here for simplicity.)

Here's a handy extension trick. Here's a really advanced extension.

Extensions are ubiquitous in Unity engineering.

If you are one of the many folks learning to code using Unity...pretty much the first thing you should master is extensions. Try to do almost everything with an extension. Sometimes when working with Unity, almost every line of code needs one or more extensions. Enjoy!

这篇关于如何扩展方法工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆