## 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
``````public static Vector3Int ConvertToVector3(this Vector3 vec3)
{
return new Vector3Int((int)vec3.x, (int)vec3.y, (int)vec3.z);
}
``````
2. Resetting the Transform
``````public 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 Vector
``````public 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 value
``````public static float RotationNormalizedDeg(this float rotation)
{
rotation = rotation % 360f;
if (rotation < 0)
rotation += 360f;
return rotation;
}
``````
5. Setting children layer value recursively
``````public 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 component
``````public static bool HasComponent(this Component component) where T : Component
{
return component.GetComponent() != null;
}
``````
7. Destroys all children recursively of a GameObject
``````public 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 GameObject
``````public 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 camera
``````public static bool IsVisibleFrom(this Renderer renderer, Camera camera)
{
var planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
``````
10. Checks if a Rect compoent intersects another Rect
``````public 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.
For More Unity Tutorials, go HERE.
For C# Tutorials, go HERE.

### Gift Wrapping Convex Hull Algorithm With Unity Implementation

Convex Hull Algorithm Convex Hull algorithms are one of those algorithms that keep popping up from time to time in seemingly unrelated fields from big data to image processing to collision detection in physics engines, It seems to be all over the place. Why should you care? Cus you can do magic with it and it seems so simple to implement when you first hear about it, but when you start thinking about it, you will realize why it's not such a straightforward thing to do.
Now that I got you interested (hopefully) and now we will see just what a convex hull is.
As you may have noticed a perimeter was made with the same points that was given and these perimeter points enclose the entire set of points.
Now we have to clear up the term 'Convex'.
Convex means no part of the object is caved inwards or that none of the internal angles made by the points exceed 180 degrees.
In this example of a concave shape internal angles go beyond 180 degrees.
What are those red lines for? Well...…

The process of shader optimization can seem like trial and error... in fact, that's how it is most of the time.
Most of the time shader optimizations could be boiled down to educated guesses because each time a shader gets compiled, the GPU driver of that specific hardware is what converts your code into actual machine code, therefore, the machine code generated will be different for each GPU and the driver itself might perform some optimizations on top of your's which won't be available on another GPU, thereby making it difficult to have a standard way of writing optimal shader code.

So the best way to know for sure is to actually test it on the hardware you are targeting.
With that said, Here are some universal best ways of getting your shader to perform better.😅 Do Calculations On Vertex Shader The most commonly used case for this is lighting, an example would be Gouraud lighting, where lighting calculations are done per vertex but at the loss of quality.

Some calculatio…

### Fortnite Procedural Construction Animation Shader

Fortnite Construction Shader This shader is loosely based on the one that was presented by the Fortnite developers in their GDC talk: Inner Working Of Fortnite's Shader-Based Procedural Animations.

Here is what we will end up with:
This technique requires you to author the 3D model in a certain way, More or less how those Fortnite developers did.
So we need the authored 3D model and the shader that uses data we get from the model to achieve the desired effect.

There are some nuances here and there so make sure you don't miss out on the details.😗
The first step will be preparing the 3D model and putting in the required data. I used Blender 2.79 but any 3D modeling software would do.
3D Model PreparationModel It
Apply Vertex Colors: For the direction of flight
Each color is a component of a vector (x, y, z). This will be considered as local space.
Values range from -1.0 to +1.0 for each component.
Negative values are achieved by using values of less than 0.5 and positive values wi…

### Visual Upgrade - Unity Asset Store [ September, 2018 ]

Visual Overhaul - Assets That Turn Mediocre Into Drool Worthy *Note: These assets are not arranged in any way, no comparison is done. They are just really cool assets that will help make your game look at least a bit better. 😁
8. Bat Particles
7.GPU Instancing Animation
6.Colored Pencils
4.Sleek Render: Mobile Post Processing
3.1000+ Effects pack
2.COLR - Coloring Redefined
1.AllSky

Check out more such Unity Asset Store Gems : HERE
Don't forget to share these wonderful assets with your colleagues and friends.
Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.