C# Extension Methods in Unity! - Intermediate Scripting Tutorial

preview_player
Показать описание
Watch this video in context on Unity Learn:

Extension methods allow you to give functionality to classes that you cannot modify and don't want to derive from. In this video you will learn how to create an extension method for the Transform class.
Рекомендации по теме
Комментарии
Автор

Transcript:

Extension methods are a way of adding functionality to a Type without having to create a DriveType, or change the original Type. They are perfect for situations where you need to add functionality to a class, but cannot edit the class. Consider the class Transform, which is built into Unity so we do not have access to its source code. Let's say we wanted an easy way to reset the Position, Rotation, and Scale of a Transform using a function. The ideal place for this function would be in the Transform class, but since we can't directly add to the class, and it wouldn't make sense to have this function in a derived class, we'll make an extension for it. Extension methods must be placed in a non-generic, static class. It is common to create a class specifically to contain them. While extension methods are used like instance methods, they too are declared static. What makes a function an extension method instead of a static method is the use of the "this" keyword in the parameters. For our example, we will create a static class called "ExtensionMethods", we then create our extension method named "ResetTransformation()". Notice that this method is declared static. Also notice that the first parameter has the "this" keyword, followed by the Transform class, and an arbitrary parameter name. This first parameter will be the calling object, and thus will not have to be supplied when we call this function. Furthermore, this first parameter dictates which class this method becomes a part of. If we wanted any more parameters, we could put them in here without the "this" keyword. Within the method, we can now write the code to reset the Transform. It is important to note that, despite this function declaration having a parameter, when the function is called, it will have none. The parameter implicitly becomes the instance of the Transform. In order to use this extension method, you only need to look for it as a member of the class you extended. In this case, we extend Transform, and can see that the method is now a part of the Transform class.

rajaspydey
Автор

wow was using unity for over a year and did not know that

DavidZobristGames
Автор

Thx Really I was searching for how to get the information out of this Method, because many Tutorials just give you a complex code, and then you can't utilize it.

steveschreiner
Автор

Posting my Enum extension method for getting a random value out of your Enum. Hope this helps anyone:
public static class EnumExtensionMethods
{
public static T GetRandom<T>(this T myEnum) where T : Enum
{
T[] values =
return values[UnityEngine.Random.Range(0, values.Length)];
}
}

After that you can call this method on any of your Enum variables to set its value to a random one:

public enum AnyEnumICreated { one, two, three }

public class AnyClass
{
private AnyEnumICreated _myNewEnum;
private void SomeMethod()
{
_myNewEnum = _myNewEnum.GetRandom();
}
}

victoraurelius
Автор

This is magical! This will allow for much cleaner code.

Nova
Автор

This one's great, thanks for it guys

simoncodrington
Автор

why there is no life(ingame) example? how can i use that?!

adamgray