Get The Most Flexibility Out Of Unity With Extension Methods
Unity3D comes with a lot of helpful classes and functions but sometimes you will encounter cases where custom implementations are required.
Rather than writing those functionalities as completely separate from the classes that already exist, We can just 'extended' those classes to have our custom functionalities.
This leads to code that is easily discoverable, clean and that can be re-used across multiple projects.
This is not a tutorial on extension methods, For that go, HERE.
In this tutorial we will go over some really useful cases where we can improve on the default Unity code base with extension methods.
1. Converting from Vector3 to Vector3Int
1. Converting from Vector3 to Vector3Int
public static Vector3Int ConvertToVector3(this Vector3 vec3)
{
return new Vector3Int((int)vec3.x, (int)vec3.y, (int)vec3.z);
}
2. Resetting the Transformpublic static void ResetTransformation(this Transform trans)
{
trans.position = Vector3.zero;
trans.localRotation = Quaternion.identity;
trans.localScale = new Vector3(1, 1, 1);
}
3. Rotating a 2D Vectorpublic static Vector2 Rotate(this Vector2 vector, float degrees)
{
float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);
float tx = vector.x;
float ty = vector.y;
vector.x = (cos * tx) - (sin * ty);
vector.y = (sin * tx) + (cos * ty);
return vector;
}
4. Returning a normalized degree valuepublic static float RotationNormalizedDeg(this float rotation)
{
rotation = rotation % 360f;
if (rotation < 0)
rotation += 360f;
return rotation;
}
5. Setting children layer value recursivelypublic static void SetLayerRecursively(this GameObject gameObject, int layer)
{
gameObject.layer = layer;
foreach (Transform t in gameObject.transform)
t.gameObject.SetLayerRecursively(layer);
}
6. Checks if GameObject has a certain componentpublic static bool HasComponent(this Component component) where T : Component
{
return component.GetComponent() != null;
}
7. Destroys all children recursively of a GameObjectpublic static void DestroyChildren(this GameObject parent)
{
Transform[] children = new Transform[parent.transform.childCount];
for (int i = 0; i < parent.transform.childCount; i++)
children[i] = parent.transform.GetChild(i);
for (int i = 0; i < children.Length; i++)
GameObject.Destroy(children[i].gameObject);
}
8. Children of one GameObject gets transferred to another GameObjectpublic static void MoveChildren(this GameObject from, GameObject to)
{
Transform[] children = new Transform[from.transform.childCount];
for (int i = 0; i < from.transform.childCount; i++)
children[i] = from.transform.GetChild(i);
for (int i = 0; i < children.Length; i++)
children[i].SetParent(to.transform);
}
9. Checks if Renderer can be seen by camerapublic static bool IsVisibleFrom(this Renderer renderer, Camera camera)
{
var planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
10. Checks if a Rect component intersects another Rectpublic static bool Intersects(this Rect source, Rect rect)
{
return !((source.x > rect.xMax) || (source.xMax < rect.x) || (source.y > rect.yMax) || (source.yMax < rect.y));
}
Well I hope you learnt something of value.Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.
If you have any questions that you might have about shaders, C# or Unity development in general don't be shy and leave a message on my facebook page or down in the comments.
For More Unity Tutorials, go HERE.
For C# Tutorials, go HERE.
The way of describing about Technology is fine.Im really happy to read this.please share more like this.
ReplyDeleteclinical sas training in chennai
clinical sas training
clinical sas Training in Anna Nagar
clinical sas Training in T Nagar
SAS Training in Chennai
SAS Course in Chennai
SAS Training Institute in Chennai
QTP Training in Chennai
Nice article!thanks for sharing such great post with us. i studied all your information and it is really good.
ReplyDeleteAndroid Training in Chennai
Android Training in Tambaram
JAVA Training in Chennai
Python Training in Chennai
Big data training in chennai
Selenium Training in Chennai
Android Training in Chennai
Android Course in Chennai
The information is good. I preview this type of good blog only. Good job.
ReplyDeleteHtml5 Training in Chennai
Html5 Courses in Chennai
html course fees
DOT NET Training in Chennai
.Net training in chennai
C C++ Training in Chennai
LoadRunner Training in Chennai
Html5 Training in Chennai
I am glad that I have visited your blog, really amazing. Waiting for further updates.
ReplyDeleteRPA Training in Chennai
RPA course in Chennai
RPA Training Institute in Chennai
Blue Prism Training in Chennai
Blue Prism Training Institute in Chennai
UiPath Training in Chennai
RPA Training in Tambaram
RPA course in Chennai
ReplyDeleteThanks to the admin you have spend a lot for this blog I gained some useful info for you. Keep doing.
Ethical Hacking Course in Chennai
Hacking Course in Chennai
ReactJS Training in Chennai
ccna Training in Chennai
gst classes in chennai
ux design course in chennai
Salesforce Training in Chennai
Angularjs Training in Chennai
ethical hacking course in chennai
hacking course in chennai
I have gone through your blog, it was very much useful for me and because of your blog, and also I gained many unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.
ReplyDeleteAviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
best aviation academy in Chennai
best air hostess training institute in Chennai
airline management courses in Chennai
airport ground staff training in Chennai
Awesome post, this post has really got some valid useful content with it. Great job; Do share more post regularly.
ReplyDeleteSpoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
Great Blog!!! Was an interesting blog with a clear concept. And will surely help many to update them.
ReplyDeleteTally Course in Chennai
Tally Classes in Chennai
PHP Training in Chennai
Web Designing Course in Chennai
Ethical Hacking Training in Chennai
ccna Training in Chennai
Tally Training in Chennai
This is an awesome post.Really very informative and creative contents.
ReplyDeleteIELTS Coaching in Chennai
IELTS Coaching centre in Chennai
french courses in chennai
pearson vue
Blockchain Training in Chennai
Ionic Training in Chennai
IELTS Coaching in Anna Nagar
IELTS Coaching in Tnagar