-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabManager.cs
59 lines (49 loc) · 3.04 KB
/
TabManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using UnityEngine;
using System.Collections;
public class TabManager : MonoBehaviour {
public GameObject[] Tabs;
RectTransform[] TabsContent;
public int startingTab;
public RectTransform tabSelectedBG;
public float proximitySesitivity = 0.1f;
public float tabAnimSpeed = 0.1f;
public float outsideOffset; //TODO calculate that automaticaly
[HideInInspector] public static int currentTab , prevTab;
Vector3 outPosLeft, outPosRight;
Vector3 ContentZeroPos;
void Start () {
currentTab = startingTab;
prevTab = 0;
outPosLeft = new Vector3(-1*outsideOffset,Tabs[0].GetComponentsInChildren<Transform>()[2].position.y); //sets outside position on the left side of the screen
outPosRight = new Vector3(outsideOffset,Tabs[0].GetComponentsInChildren<Transform>()[2].position.y); //sets outside position on the right side of the screen
ContentZeroPos = gameObject.GetComponentsInChildren<Transform>()[3].transform.position; //im not actually sure why its 3 here but that how it works
TabsContent = new RectTransform[Tabs.Length];
for (int i=0;i<Tabs.Length;i++) {
foreach(RectTransform child in Tabs[i].GetComponentsInChildren<RectTransform>()){
if(child.CompareTag("tabContent"))
TabsContent[i] = child;
}
if (i<currentTab)
TabsContent[i].transform.position = outPosLeft;
else if (i>currentTab)
TabsContent[i].transform.position = outPosRight;
}
tabSelectedBG.transform.position = Tabs[currentTab].GetComponent<Transform>().position;
}
void Update () {
if ((tabSelectedBG.position-Tabs[currentTab].GetComponent<Transform>().position).magnitude > proximitySesitivity) //if tab havent reacehd destination - move new selected tab to its new location (selected tab)
tabSelectedBG.position = Vector3.Lerp(tabSelectedBG.position, Tabs[currentTab].GetComponent<Transform>().position , tabAnimSpeed);
if ((TabsContent[currentTab].transform.position-ContentZeroPos).magnitude > proximitySesitivity) //if content havent reacehd destination - move new current Tab content to center
TabsContent[currentTab].transform.position = Vector3.Lerp(TabsContent[currentTab].transform.position, ContentZeroPos , tabAnimSpeed);
if (((TabsContent[currentTab].transform.position-outPosLeft).magnitude > proximitySesitivity) && ((TabsContent[currentTab].transform.position-outPosRight).magnitude > proximitySesitivity)) //if content havent reacehd destination - move old current Tab to the side
TabsContent[prevTab].transform.position = Vector3.Lerp(TabsContent[prevTab].transform.position, (currentTab > prevTab) ? outPosLeft : outPosRight , tabAnimSpeed);
tabSelectedBG.sizeDelta = Vector2.Lerp(tabSelectedBG.sizeDelta, Tabs[currentTab].GetComponents<RectTransform>()[0].sizeDelta,tabAnimSpeed);
}
public void tabPress(int tabNum) {
if (tabNum!=currentTab) {
prevTab = currentTab;
currentTab = tabNum;
TabsContent[currentTab].transform.position = (prevTab>currentTab) ? outPosLeft : outPosRight; //fix a bug where sometime the ingoing tab comes from the wrong side
}
}
}