Monday, July 28, 2014

Resolution Manager Script Unity3d

Hello developers,
As a mobile app/game developers it is not quite easy to make app/game UI same on different resolution devices,
In previous post you can see common resolutions with aspect ratios, also most used resolution devices among with that,
Below is the Unity3d Monobehaviour version of that discussion which is quite easy to get and use,

using UnityEngine;
using System.Collections;

public class ResolutionManager : MonoBehaviour 
{
public int bgWidthToLaod = 800;

float xWidth;
float yHeight;

void Awake()
{
xWidth = Screen.width;
yHeight = Screen.height;

float aspectRatio = xWidth / yHeight;

if(aspectRatio >= 1.30f && aspectRatio <= 1.35f)
{
// 2048 * 1536
aspectRatio = 1.33f;
bgWidthToLaod = 2048;
}
else if(aspectRatio >= 1.45f && aspectRatio <= 1.55f)
{
// 960 * 640
aspectRatio = 1.5f;
bgWidthToLaod = 960;
}
else if(aspectRatio >= 1.58f && aspectRatio <= 1.70f)
{
// 1280 * 768
aspectRatio = 1.67f;
bgWidthToLaod = 1280;
}
else if(aspectRatio >= 1.75f && aspectRatio <= 1.80f)
{
// 1136 * 640
aspectRatio = 1.78f;
bgWidthToLaod = 1136;
}
else
{
aspectRatio = 1.67f;
bgWidthToLaod = 1280;
}
}
}

Tuesday, July 22, 2014

Unity3d Important Methods and its Priority Order

Some Unity3d important methods and its usage with priority order,

[1] Awake : 

        Called just after object got initialized, also for prefab. It is called before Start function call. If gameobject is disabled Awake is not called, but if any function of this script or function of script attached to same object is invoked, object would be initialized and Awake is called. This is where most of developers got confused with Awake method invocation.
Priority : 1


[2] OnEnable : 

        This is called when Monobehaviour is enabled. It is also called before Start call. If object is not active this will not be invoked.
Priority : 2


[3] Start :

        Start is called before any frame update started if object is enabled.
Priority : 3

[4] FixedUndate :

        Fixed update is called more frequently then Update call if frame rate is low. If frame rate is high then it might called less frequently then Update call. Fixed update is called on reliable basis then Update function that's why you do not need to multiply movement calculation with Time.deltaTime. All physics related calculation should be placed inside FixedUpdate to make it behave correctly.
Priority : 4


[5] Update : 

        Update is called once per frame. This is main function in Unity3d Monobehaviour. If frame rate is set to be high then all input related code should be placed here to avoid input issues.
Priority : 5


[6] LateUpdate :

        LateUpdate is also called once per frame. The only difference between Update and LateUpdate is that its been called once Update has completed its execution. This will be very usefull in case where there is a need of task to be executed if and only if another task got completed. 
Example : Camera movement, score calculation, input management.
Priority : 6


[7] OnDisable : 

        This is called when Monobehaviour becomes inactive/disabled.
Priority : 7


[8] OnGUI :

        This function is called multiple time per frame, might call double then the Update call depending upon current frame rate. All input related code should be place in this function. Main use of this function is two draw various UI elements. Thought it is not advisable to use this function as of performance issues.

Sunday, July 20, 2014

Resoultions and Aspect Ratio

As a mobile game/app developer it is headache to port game/app to various mobile/hand held devices having different resolutions. Here is the list of resolutions for all major mobile and hand held devices which would help you while developing cross platforms mobile applications and games,

Resolutions* (AR*)

2048 * 1536 --> 1.33
1024 * 768 --> 1.33
1136 * 640 --> 1.78
960 * 640 --> 1.50
854 * 480 --> 1.78
800 * 480 --> 1.67
800 * 400 --> 1.50
2560 * 1536 --> 1.67
2560 * 1600 --> 1.60
1920 * 1080 --> 1.78
1920 * 1200 --> 1.60
1920 * 1152 --> 1.67
1824 * 1200 --> 1.52
1536 * 1152 --> 1.33
1280 * 720 --> 1.78
1280 * 800 --> 1.60
1280 * 768 --> 1.67
1024 * 600 --> 1.71
1024 * 800 --> 1.28
960 * 600 --> 1.60

* Resolutions are in Pixels
* AR - Aspect Ration

Most used aspect ratios,

1.33 - iPad, iPad 2, iPad Retina
1.50 - iPhone 4, iPhone 4s
1.67 - Most of Android devices
1.78 - iPhone 5, iPhone 5s, iPhone 5c, Android Tab Series

Monobehaviour script for resolutions and aspect ration is here.

Sunday, July 13, 2014

Facts About Unity3d Coroutines

         Here are some of important facts of Unity3d Coroutines which might help you,

1. You can only stop Coroutines that are invoked using StartCoroutine with string as a argument.


Example:


StartCoroutine("MethodName") can be stop using StopCoroutine("MethodName"),
Coroutines those are invoked as StartCoroutine(MethodName()) will not be cancelled by StopCoroutine("MethodName").

2. You can invoke other class Coroutine even after it is  marked as Private.

Example:


Class1.cs


using UnityEngine;
using System.Collections;

public class Class1 : MonoBehaviour {


// Use this for initialization

void Start () {
}

// Update is called once per frame
void Update () {
}

private void DemoCoroutine(){

print("Coroutine Invoked!");
}
}


Class2.cs


using UnityEngine;
using System.Collections;

public class Class2 : MonoBehaviour {


        // Reference Assigned in Inspector

public Class1 Class1Instance;

// Use this for initialization

void Start () {
        // You can even use singleton calss to invoke Coroutine defined in different class
Class1Instance.StartCoroutine("DemoCoroutine");
}

// Update is called once per frame
void Update () {
}
}


More about Unity3d Coroutine,

Friday, July 4, 2014

Shaders and It's Best Usage

Here is the basic details of Unity3d's inbuilt shaders and best usage of it,


Particles - Mobile/Particles/Alpha Blended
Particles - Mobile/Particles/Addictive
Transparent Background - Mobile/Particles/Alpha Blended
Line Renderer - Particles/VertexLit Blended

Tuesday, July 1, 2014

Facts about Resources folder in Unity3d

Some facts about Resource folder in Unity3d,

    The Resources folder is a special folder which allows you to access assets by file path and name in your scripts, rather than by referencing them in Unity Inspector.
  • All the assets of Resources will be included in build generated(even if it is not used in anyway).
  • Resources folder would not exist when build is generated, It exists in editor only.
  • Multiple resources folder can be used, Unity will examine assets from all the Resource folders. So it is not recommended to use same named asset in two different Resources folder.
  • All the assets found in Resources folder will be stored in resources.assets file, when build is generated.
  • It can be used to load Textures, Materials, AudioClips etc.
  • Prefab can also be instantiated directly form Resource folder as shown below,
Example,
GameObject gameObj = Instantiate(Resouces.Load("ObjectName", GameObject));
OR
GameObject gameObj = Instantiate(Resouces.Load<GameObject>("ObjectName"));
OR
GameObject gameObj = Instantiate(Resouces.Load("ObjectName", typeof(GameObject)));

Note : Only forward shash('/') is allowed to use to indicate path of assets in Resources, backward shash('\') would not work.


Popular Posts