Skip to main content

Posts

Showing posts from 2016

How to create a carousel view with Unity UI

Hi Guys,

In my last project i was asked to add a carousel view in one of the screen, But as you know unity doesn't provide anything like that, so i created my own solution.

If anyone need it than you can download the project file from the link given below.


Here is a video showing how my Carousel view looks like.



Here is the Project File link : http://bit.ly/2gRybKZ

And if you find this post useful, you can contribute by clicking the link Given Below.
https://www.paypal.me/Sushanta1991


Good Luck!

How to create Chat Screen with new Unity UI

Hi Guys,

Unity UI is a blank page you can create anything you want, but some time it needs a lot of effort, and this one took a lot.

Anyways, i was successful in creation of an UI which looks similar to WhatsApp or any modern chat application.

All you need to do is pass a string to a Coroutine, separated by  "~0" and "~1" based on these separators function decides where to put the text on screen (left/right side).

So, i am not gonna post any script here, I will attach the Project file itself so you can check things in detail. There is only 2 scripts, all the logical part is handle by ChatManager.cs.

if you have any doubts, please feel free to ask.





Basically the setup looks something like this.






If you like this post and want to contribute Please click on the link given below.
https://www.paypal.me/Sushanta1991

Project File Link :http://bit.ly/2fdx2tZ

How to merge 2 Texture in unity

Hi guys,


Finally i got a chance to play with pixels, i was trying make an image effect. In the process i faced some challenges, so this post is about one of the challenges i faced.


I was trying to make a 'Vignettes' Image effect, to do that i needed to merge my overlay with the background image, this was little bit tricky at first. There was very less information available on the internet, though 'wikipedia' came to the rescue. I found this article which have a simple formula that merges 2 texture.


So the formula was,


Looks complex right ? lets read that formula as a sentence,
outputRed = (foregroundRed * foregroundAlpha) + (backgroundRed * (1.0 - foregroundAlpha));

Now it makes more sense, so basically you need to use that formula to get red, green and blue values of each pixel and create a new texture with that.

foreground is the overlay i created, which is a transparent texture, and my background is opaque.


background


foreground (overlay image for Vignettes effect)



I m…

How to rotate a 2D Vector in unity

There are hundreds of posts on this topic, but i will specifically stick to unity only. Since i struggle a little bit to figure out how vector's work's in unity, so i don't want this to happen with you guy's.


Few day's back i was simply trying to rotate a 2D vector in unity , just to create a circle.

I found this equation in wikipedia , which rotates a vector




First i tried to rotate a simple vector which was pointing at (2,0) to 90 degrees at (0,2), take a look at the image :




So i took the equation, and tried it in unity, which looks like this :

publicVector2RotateVector(Vector2v,floatangle)
{
float_x=v.x*Mathf.Cos(angle)-v.y*Mathf.Sin(angle);
float_y=v.x*Mathf.Sin(angle) +v.y*Mathf.Cos(angle);
returnnewVector2(_x,_y);
}

If i pass RotateVector(new Vector2(2,0), 90), I should get a Vector(0,2) but unfortunately i was getting something like this (-0.9, 1.8) .



If you read the Unity Documents carefully they have said that Sin and Cos function takes the value in Radian, so w…

How to use webcam in unity new UI

Hello! guys...

welcome to another post on UI :)

so in my last project there was a requirement of creating a selfie screen in unity, so i used new UI to create it.

There was some challenges but as always unity community was very helpful, that is the reason i thought of sharing my experience here in this blog.

My setup in unity was really simple, I used rawImage and assigned webcamTexture to its  texture   property, and that's it, once you call webcamTexture.Play(); you will get the realtime feed from camera in your rawImage.

here is an image showing my setup.



you can see that, my rawImage width is larger than its height, thats because my webcamTexture width was larger than its height, so if i keep it in square shape, my image was getting stretched, so thats why i kept it like that.

But to tackle this problem i am cropping my final texture to a square shape, based on smaller width /height, which gives me a perfect square shape image without any stretching.

here is a sample of my f…

How to move unity UI using mouse

Make UI Image Follow Mouse Position
4.6 UI Image follow mouse position
move UI object to click position


Hello guys,

As we all know this is the most basic thing we do in Unity, That is 'dragging stuffs', and there is one straight forward solution to it, which works perfectly. But in some cases it will not, that is the reason you see a lot of forum posts on this topic, and we don't understand why its not working for us but the same thing is working for others.



this is the code for dragging UI with our mouse.
rect.anchoredPosition=Input.mousePosition;
rect is a type off  RectTransform   so don't get confused.


Simple right ?

Than why sometime it doesn't work ?



LETS EXPLORE THE REASONS

I got stuck in a similar problem few days back, and i found out that there are certain reason's, that above code will not work.

check out this image:



Here you can see that my pointer and my image are at same position, this is because i have set the Image anchors to bottom left position w…

Understanding unity UI through coding

Hello guys,

In this post i will be sharing what i learned about unity new UI system, i will start with smaller ones later i will keep adding new things to this post.

Before starting, i will assume that our Script is attach to an Image inside a canvas. You can use some of the one line codes, shown in this post inside your Start function to test the results.




1. How to get width and height of an UI element.

For Width use  : GetComponent<RectTransform>().rect.width;
For Height use : GetComponent<RectTransform>().rect.height;







2. How to move an UI element.

You can use this line of code to move a UI.

GetComponent<RectTransform>().anchoredPosition+=newVector2(10,0);

Before moving :


    After moving :


     As you can see the image have moved 10 units to its right side.

But,

     You can also notice, Image have moved, but Anchors are 'at there previous position'.

     I found one solution over Internet, to set Anchors position, it works very well.

     Pass any gameObject to …

How to create unique ID in unity

Hi guys,

Sometime we come in a situation where we need to assign an unique id to our gameObject or any other thing.
Creating a random number is not reliable because there is a chance that 2 generated numbers can be same.
So i came up with a simple solution, in which i am generating a number by mixing date and time of the system, which will never be same, because it changes every micro second.

Here is the function to get Unique ID,

stringGetUniqueID()
{
string[]split= System.DateTime.Now.TimeOfDay.ToString().Split(newChar[]{':','.'});
stringid="";
for(inti=0;i<split.Length;i++){
id+=split[i];
}
returnid;
}



Or you can use  :
'System.Guid.NewGuid()' as suggested by 'Yed' in comments.



If you like this post and want to contribute Please click on the link given below.
https://www.paypal.me/Sushanta1991