Thursday, July 30, 2020
Wednesday, July 29, 2020
Photon engine learning
Tuesday, July 28, 2020
To calculate camera view World Position vs gameobject world position check out of boundary view
The Size of the Frustum at a Given Distance from the Camera
A cross-section of the view frustum at a certain distance from the camera
defines a rectangle in world space that frames the visible area. It is sometimes useful to calculate the size of this rectangle at a given distance, or find the distance where the rectangle is a given size. For example, if a moving camera needs to keep an object (such as the player) completely in shot at all times then it must not get so close that part of that object is cut off.
The height of the frustum at a given distance (both in world units) can be obtained with the following formula:
var frustumHeight = 2.0f * distance * Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
…and the process can be reversed to calculate the distance required to give a specified frustum height:
var distance = frustumHeight * 0.5f / Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
It is also possible to calculate the FOV angle when the height and distance are known:
var camera.fieldOfView = 2.0f * Mathf.Atan(frustumHeight * 0.5f / distance) * Mathf.Rad2Deg;
Each of these calculations involves the height of the frustum but this can be obtained from the width (and vice versa) very easily:
var frustumWidth = frustumHeight * camera.aspect;
var frustumHeight = frustumWidth / camera.aspect;
Sunday, July 26, 2020
RaiseEvent vs RPC in PhotonEngine
Generally speaking the easiest way to instantiate networked objects is by using PhotonNetwork.Instantiate, or PhotonNetwork.InstantiateSceneObject.
You can use RPCs or RaiseEvent to achieve the same, but then you need to manually do a lot of stuff that is automatically taken care of for you by PhotonNetwork.Instantiate, such as allocating viewIDs and cleaning up when a player disconnects.
The only advantage of RPCs over RaisEvent, in my opinion, is simplicity as there is less work required to get them working, but the con is that they require objects to have a PhotonView attached.
RaiseEvent on the other hand is much more flexible in as much as no PhotonViews are required and you decide which scripts will listen for and respond to them. You also have greater control over which clients you send to, which can save bandwidth.
I don't use RPCs at all now, preferring RaiseEvent for everything.
This is just fairly general personal opinion on my part, if you require more specific help you should post some of your code with a description of what you want it to do vs what it is actually doing.
Friday, July 24, 2020
SingletonMonoBehaviour
Calculate orthorgraphic camera size for tilemap based 2dGame
Pixel Perfect Calculator for Orthographic Camera : Unity3D
x = Screen Width (px)
y = Screen Height (px)
s = Desired Height of Photoshop Square (px)
Tuesday, July 21, 2020
Delegate vs Event C#
Objective:
Introduction :
Delegate :
Types of Delegate:
Single Delegate :
It can reference to only single method at a time.Multicast Delegate :
It can store the reference of multiple methods at a time.
Syntax :
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
| //Defining Delegate public delegate void OnButtonClickDelegate(); public static OnButtonClickDelegate buttonClickDelegate; //Subscribing to delegate // eg. of Singlecast buttonClickDelegate = myCustomMethod; //eg. of Multicast Delegate buttonClickDelegate += myCustomMethod; buttonClickDelegate +=myAnotherCustomMethod //Calling Delegates buttonClickDelegate(); //UnSubscribing to Delegate buttonClickDelegate -= myCustomMethod; buttonClickDelegate -=myAnotherCustomMethod |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
| using UnityEngine; using System.Collections; public class DelegateHandler : MonoBehaviour { public delegate void OnButtonClickDelegate (); public static OnButtonClickDelegate buttonClickDelegate; public void OnButtonClick() { buttonClickDelegate (); } } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| using UnityEngine; using System.Collections; public class ObjectController : MonoBehaviour { Renderer objRenderer; // Use this for initialization void Start () { DelegateHandler.buttonClickDelegate += ChangePosition; DelegateHandler.buttonClickDelegate += ChangeColor; objRenderer = GetComponent<Renderer>(); } void ChangePosition() { transform.position = new Vector2 (transform.position.x + 2f, transform.position.y); } void ChangeColor() { objRenderer.material.color = Color.yellow; } // Unsubscribing Delegate void OnDisable() { DelegateHandler.buttonClickDelegate -= ChangeColor; DelegateHandler.buttonClickDelegate -= ChangePosition; } } |
1
2
3
4
5
6
7
| void Start () { DelegateHandler.buttonClickDelegate += ChangePosition; DelegateHandler.buttonClickDelegate += ChangeColor; Delegatehandler.buttonClickDelegate = ChangeRotation; objRenderer = GetComponent (); } |
Events :
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
| using UnityEngine; using System.Collections; public class DelegateHandler : MonoBehaviour { public delegate void OnButtonClickDelegate (); public static event OnButtonClickDelegate buttonClickDelegate; public void OnButtonClick() { buttonClickDelegate (); } } |
1
2
3
| // Delegatehandler.buttonClickDelegate += ChangeRotation; // |
Conclusion :
- Delegates and Events help us to write modular and reusable code.
- Always use Events together with Delegates for safety.
- Do not forget to unsubscribe otherwise, it will lead to memory leak.