Sunday, 9 October 2016

LoadingScreen

using UnityEngine;
using System.Collections;

public class LoadingScreen : MonoBehaviour {

public string levelToLoad;
public GameObject background;
public GameObject text;
public GameObject progressBar;

private int loadProgress = 0;


// Use this for initialization
void Start () {
StartCoroutine(DisplayLoadingScreen(levelToLoad));
//background.SetActive (false);
//text.SetActive (false);
//progressBar.SetActive (false);
}

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

if (Input.GetKeyDown ("space")) {

}
}

IEnumerator DisplayLoadingScreen(string level){
background.SetActive (true);
text.SetActive (true);
progressBar.SetActive (true);

progressBar.transform.localScale = new Vector3(loadProgress,progressBar.transform.localScale.y, progressBar.transform.localScale.z);

text.GetComponent<GUIText>().text = "Loading Progress..." + loadProgress + "%";

AsyncOperation async = Application.LoadLevelAsync (level);
while (!async.isDone) {
loadProgress = (int)(async.progress * 112);
text.GetComponent<GUIText>().text = "Loading Progress..." + loadProgress + "%";
progressBar.transform.localScale = new Vector3(async.progress,progressBar.transform.localScale.y, progressBar.transform.localScale.z);
yield return null;
}

}
}

InventryManager

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class InventryManager : MonoBehaviour {


public Button Brake;
public Button Speed;


public static int InventryCoin;
public Text InventryCoinText;

public Text Speedvalue;

public Text Brackvalue;

int brake = 0;
public static int Speed2 = 0;

public static int Carspeed;

public Text Speed1;
public Text Brake1;


// Use this for initialization
void Start () {

//PlayerPrefs.DeleteAll ();

//PlayerPrefs.DeleteAll ();
InventryCoin = PlayerPrefs.GetInt("Coin");
InventryCoinText.text = "Coin : " + InventryCoin;

Commanmethod();

}



void Awake ()
{

Speed2 = PlayerPrefs.GetInt ("Speed");
brake = PlayerPrefs.GetInt ("Brake");


}

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



}

public void speedInc()
{

Speed.interactable = false;

if (InventryCoin >= 6000 && Speed2 == 5) {


InventryCoin = InventryCoin - 6000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;

Speed2 =6;
PlayerPrefs.SetInt ("Speed", Speed2);

} if (InventryCoin >= 4000 && Speed2 == 4) {

InventryCoin = InventryCoin - 4000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;


Speed2 =5;
PlayerPrefs.SetInt ("Speed", Speed2);


} if (InventryCoin >= 3500 && Speed2 == 3) {

InventryCoin = InventryCoin - 3500;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;


Speed2 =4;
PlayerPrefs.SetInt ("Speed", Speed2);


}
if (InventryCoin >= 3000 && Speed2 == 2) {


InventryCoin = InventryCoin - 3000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;


Speed2 =3;
PlayerPrefs.SetInt ("Speed", Speed2);


}

else if (InventryCoin >= 2000 && Speed2 == 1) {


InventryCoin = InventryCoin - 2000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;


Speed2 =2;
PlayerPrefs.SetInt ("Speed", Speed2);



}
else if (InventryCoin >= 1000 && Speed2 == 0) {


InventryCoin = InventryCoin - 1000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;


Speed2 =1;
PlayerPrefs.SetInt ("Speed", Speed2);

}
else
{
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;
}
Commanmethod();
}


public void BrakeInc()
{

Brake.interactable = false;

if (InventryCoin >= 6000 && brake == 5) {

InventryCoin = InventryCoin - 6000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;

brake =6;
PlayerPrefs.SetInt ("Brake", brake);


}
if (InventryCoin >= 4000 && brake == 4) {

InventryCoin = InventryCoin - 4000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;

brake =5;
PlayerPrefs.SetInt ("Brake", brake);


} if (InventryCoin >= 3500 && brake == 3) {

InventryCoin = InventryCoin - 3500;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;

brake =4;
PlayerPrefs.SetInt ("Brake", brake);


}
if (InventryCoin >= 3000 && brake == 2) {

InventryCoin = InventryCoin - 3000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;

brake =3;
PlayerPrefs.SetInt ("Brake", brake);


}
else if (InventryCoin >= 2000 && brake == 1) {
InventryCoin = InventryCoin - 2000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;


brake =2;
PlayerPrefs.SetInt ("Brake", brake);


}

else if (InventryCoin >= 1000 && brake == 0) {
InventryCoin = InventryCoin - 1000;
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;


brake =1;
PlayerPrefs.SetInt ("Brake", brake);



} else {
PlayerPrefs.SetInt ("Coin", InventryCoin);
InventryCoinText.text = "Coin : " + InventryCoin;

}
Commanmethod();
}


void Commanmethod()
{
//Speed condition value on button
if (Speed2 == 0) {
Speedvalue.text = " Use : 1000";
Speed1.text = "Top Speed = 60";

Carspeed = 60;
PlayerPrefs.SetInt ("CarSpeed", Carspeed);
PlayerPrefs.SetInt ("TopSpeed", Carspeed);


} else if (Speed2 == 1) {
Speedvalue.text = " Use : 2000";
Speed1.text = "Top Speed = 70";

Carspeed = 70;
PlayerPrefs.SetInt ("CarSpeed", Carspeed);
PlayerPrefs.SetInt ("TopSpeed", Carspeed);

} else if (Speed2 == 2) {
Speedvalue.text = " Use : 3000";
Speed1.text = "Top Speed = 80";

Carspeed = 80;
PlayerPrefs.SetInt ("CarSpeed", Carspeed);
PlayerPrefs.SetInt ("TopSpeed", Carspeed);

} else if (Speed2 == 3) {
Speedvalue.text = " Use : 3500";
Speed1.text = "Top Speed = 85";

Carspeed = 85;
PlayerPrefs.SetInt ("CarSpeed", Carspeed);
PlayerPrefs.SetInt ("TopSpeed", Carspeed);

}else if (Speed2 == 4) {
Speedvalue.text = " Use : 4000";
Speed1.text = "Top Speed = 90";

Carspeed = 90;
PlayerPrefs.SetInt ("CarSpeed", Carspeed);
PlayerPrefs.SetInt ("TopSpeed", Carspeed);

}else if (Speed2 == 5) {
Speedvalue.text = " Use : 6000";
Speed1.text = "Top Speed = 100";

Carspeed = 100;
PlayerPrefs.SetInt ("CarSpeed", Carspeed);
PlayerPrefs.SetInt ("TopSpeed", Carspeed);
}


//Brake condition value on button
if (brake == 0) {
Brackvalue.text = "Use : 1000";
Brake1.text = "Brake = 60";
} else if (brake == 1) {
Brackvalue.text = "Use : 2000";
Brake1.text = "Brake = 70";
} else if (brake == 2) {
Brackvalue.text = "Use : 3000";
Brake1.text = "Brake = 80";
} else if (brake == 3) {
Brackvalue.text = "Use : 3500";
Brake1.text = "Brake = 85";
} else if (brake == 4) {
Brackvalue.text = "Use : 4000";
Brake1.text = "Brake = 90";
} else if (brake == 5) {
Brackvalue.text = "Use : 6000";
Brake1.text = "Brake = 100";
}


//Speed Button Visible and invisible...

if (InventryCoin >= 1000 && Speed2 == 0) {
Speed.interactable = true;

} else if (InventryCoin >= 2000 && Speed2 == 1) {
Speed.interactable = true;

} else if (InventryCoin >= 3000 && Speed2 == 2) {
Speed.interactable = true;

} else if (InventryCoin >= 3500 && Speed2 == 3) {
Speed.interactable = true;

}else if (InventryCoin >= 4000 && Speed2 == 4) {
Speed.interactable = true;

}else if (InventryCoin >= 6000 && Speed2 == 5) {
Speed.interactable = true;

}else {
Speed.interactable = false;

}


//Brake Button Visible and invisible...
if (InventryCoin >= 1000 && brake == 0) {
Brake.interactable = true;

} else if (InventryCoin >= 2000 && brake == 1) {
Brake.interactable = true;

} else if (InventryCoin >= 3000 && brake == 2) {
Brake.interactable = true;

} else if (InventryCoin >= 3500 && brake == 3) {
Brake.interactable = true;

} else if (InventryCoin >= 4000 && brake == 4) {
Brake.interactable = true;

} else if (InventryCoin >= 6000 && brake == 5) {
Brake.interactable = true;

} else {
Brake.interactable = false;

}


}
public void Backinventory()
{
Application.LoadLevel ("Main menu");
}

void LateUpdate(){
if (Input.GetKeyDown(KeyCode.Escape)) {

Application.LoadLevel ("Main Menu");

}
}
}

GearBox in Car


using UnityEngine;
using CnControls;
using System.Collections;

public class GearBox : MonoBehaviour {
public car car1;
public GameObject pointerSpeed;
public GameObject pointerRPM;

public float maxPitch=2.4f;
public float[] totalSteps;

public AudioClip rollingSound;
public AudioClip engineSound;
public AudioClip skidSound;

float highVolume = 1;

AudioSource rollAudio = null;
AudioSource engineAudio = null;
AudioSource skidAudio = null;

float currentPitch = 1;
float rollPitch=1;
float procentPitch;
float timeToShift=0;
float speed;

bool shiftinGear;
int currentGear = 1;

Quaternion pointRot;

void Awake(){

rollAudio = gameObject.AddComponent<AudioSource>();
rollAudio.loop = true;
rollAudio.clip = rollingSound;
rollAudio.volume = 0;
rollAudio.spatialBlend=0.8f;
rollAudio.Play();

engineAudio = gameObject.AddComponent<AudioSource>();
engineAudio.loop = true;
engineAudio.clip = engineSound;
engineAudio.volume = highVolume;
engineAudio.spatialBlend=0.8f;
engineAudio.Play();

skidAudio = gameObject.AddComponent<AudioSource>();
skidAudio.loop = true;
skidAudio.clip = skidSound;
skidAudio.volume = 0.1f;
skidAudio.spatialBlend=0.8f;
}

void Update(){
speed = GetComponent<Rigidbody>().velocity.magnitude*3.6f;
}

void FixedUpdate () {

procentPitch = Procents(maxPitch-1,1);
Transmission();
pointerSpeed.transform.localRotation=Quaternion.Euler(Mathf.Abs(speed)*-0.9f,-90,0);
pointerRPM.transform.localRotation=Quaternion.Euler(-70*(currentPitch-1),-90,0);
Sound();
}

void Sound(){
rollAudio.pitch = rollPitch;
if((speed/20)<0.7)
rollAudio.volume =speed/20;
engineAudio.pitch = currentPitch/2;
if(!shiftinGear&&engineAudio.volume <currentPitch)
engineAudio.volume +=0.05f;
}

float Procents(float value, float procents){
float newValue = (value/100)*procents;
return newValue;
}

float ProcentOfValue(float firstValue, float secondValue){
float newValue = (int)(firstValue / (secondValue/100));
return newValue;
}

void Transmission(){
float difference=0;
float tempSpeed=0;

if(CnInputManager.GetAxis("Vertical")>0){
if(!shiftinGear){
for (int i = 1; i<totalSteps.Length; i++)
{
difference=totalSteps[i+1]-totalSteps[i];
tempSpeed=speed-totalSteps[i];
if(speed>=totalSteps[currentGear+1]&&currentGear==i)
shiftinGear=true;
if(speed<totalSteps[i+1]){
break;
}
}
currentPitch =Mathf.Lerp(currentPitch, (1+(procentPitch*ProcentOfValue(tempSpeed, difference))), 0.1f);
if(currentPitch>maxPitch)
currentPitch=maxPitch;
timeToShift=0;
}

}
else{
currentPitch=Mathf.Lerp(currentPitch,1,0.05f);
for (int i = 0; i<totalSteps.Length; i++){
currentGear=i;
if(speed<totalSteps[i+1]){
break;
}
}
}

if(shiftinGear){
if(engineAudio.volume>0.2)
engineAudio.volume -=0.05f;
currentPitch=Mathf.Lerp(currentPitch,1,0.01f);
timeToShift+=Time.deltaTime;
if(timeToShift>0.5){
shiftinGear=false;
currentGear+=1;
}
}
if(currentPitch<1)
currentPitch=1;
rollPitch=1+(procentPitch*ProcentOfValue(speed,100));
}
}

Fader

using UnityEngine;
using System.Collections;

public class Fader : MonoBehaviour
{
public Texture2D fadeOutTexture;
public float fadeSpeed = 0.8f;

private int drawDepth = -1000;
private float alpha = 1.0f;
private int fadeDir = -1;


void OnGUI()
{

alpha += fadeDir * fadeSpeed * Time.deltaTime;

alpha = Mathf.Clamp01(alpha);

GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.depth = drawDepth;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height),fadeOutTexture);
}

public float BeginFade (int direction)

{

fadeDir = direction;
return fadeSpeed;

}

void OnLevelWasLoaded()

{

BeginFade(-1);

}

}

DropMe

using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DropMe : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
public Image containerImage;
public Image receivingImage;
private Color normalColor;
public Color highlightColor = Color.yellow;

public void OnEnable ()
{
if (containerImage != null)
normalColor = containerImage.color;
}

public void OnDrop(PointerEventData data)
{
containerImage.color = normalColor;

if (receivingImage == null)
return;

Sprite dropSprite = GetDropSprite (data);
if (dropSprite != null)
receivingImage.overrideSprite = dropSprite;
}

public void OnPointerEnter(PointerEventData data)
{
if (containerImage == null)
return;

Sprite dropSprite = GetDropSprite (data);
if (dropSprite != null)
containerImage.color = highlightColor;
}

public void OnPointerExit(PointerEventData data)
{
if (containerImage == null)
return;

containerImage.color = normalColor;
}

private Sprite GetDropSprite(PointerEventData data)
{
var originalObj = data.pointerDrag;
if (originalObj == null)
return null;

var dragMe = originalObj.GetComponent<DragMe>();
if (dragMe == null)
return null;

var srcImage = originalObj.GetComponent<Image>();
if (srcImage == null)
return null;

return srcImage.sprite;
}
}

DragPanel

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler {

private Vector2 originalLocalPointerPosition;
private Vector3 originalPanelLocalPosition;
private RectTransform panelRectTransform;
private RectTransform parentRectTransform;

void Awake () {
panelRectTransform = transform.parent as RectTransform;
parentRectTransform = panelRectTransform.parent as RectTransform;
}

public void OnPointerDown (PointerEventData data) {
originalPanelLocalPosition = panelRectTransform.localPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
}

public void OnDrag (PointerEventData data) {
if (panelRectTransform == null || parentRectTransform == null)
return;

Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition)) {
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;
}

ClampToWindow ();
}

// Clamp panel to area of parent
void ClampToWindow () {
Vector3 pos = panelRectTransform.localPosition;

Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;
Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max;

pos.x = Mathf.Clamp (panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);
pos.y = Mathf.Clamp (panelRectTransform.localPosition.y, minPosition.y, maxPosition.y);

panelRectTransform.localPosition = pos;
}
}

DragMe

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public bool dragOnSurfaces = true;

private Dictionary<int,GameObject> m_DraggingIcons = new Dictionary<int, GameObject>();
private Dictionary<int, RectTransform> m_DraggingPlanes = new Dictionary<int, RectTransform>();

public void OnBeginDrag(PointerEventData eventData)
{
var canvas = FindInParents<Canvas>(gameObject);
if (canvas == null)
return;

// We have clicked something that can be dragged.
// What we want to do is create an icon for this.
m_DraggingIcons[eventData.pointerId] = new GameObject("icon");

m_DraggingIcons[eventData.pointerId].transform.SetParent (canvas.transform, false);
m_DraggingIcons[eventData.pointerId].transform.SetAsLastSibling();

var image = m_DraggingIcons[eventData.pointerId].AddComponent<Image>();
// The icon will be under the cursor.
// We want it to be ignored by the event system.
var group = m_DraggingIcons[eventData.pointerId].AddComponent<CanvasGroup>();
group.blocksRaycasts = false;

image.sprite = GetComponent<Image>().sprite;
image.SetNativeSize();

if (dragOnSurfaces)
m_DraggingPlanes[eventData.pointerId] = transform as RectTransform;
else
m_DraggingPlanes[eventData.pointerId]  = canvas.transform as RectTransform;

SetDraggedPosition(eventData);
}

public void OnDrag(PointerEventData eventData)
{
if (m_DraggingIcons[eventData.pointerId] != null)
SetDraggedPosition(eventData);
}

private void SetDraggedPosition(PointerEventData eventData)
{
if (dragOnSurfaces && eventData.pointerEnter != null && eventData.pointerEnter.transform as RectTransform != null)
m_DraggingPlanes[eventData.pointerId] = eventData.pointerEnter.transform as RectTransform;

var rt = m_DraggingIcons[eventData.pointerId].GetComponent<RectTransform>();
Vector3 globalMousePos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlanes[eventData.pointerId], eventData.position, eventData.pressEventCamera, out globalMousePos))
{
rt.position = globalMousePos;
rt.rotation = m_DraggingPlanes[eventData.pointerId].rotation;
}
}

public void OnEndDrag(PointerEventData eventData)
{
if (m_DraggingIcons[eventData.pointerId] != null)
Destroy(m_DraggingIcons[eventData.pointerId]);

m_DraggingIcons[eventData.pointerId] = null;
}

static public T FindInParents<T>(GameObject go) where T : Component
{
if (go == null) return null;
var comp = go.GetComponent<T>();

if (comp != null)
return comp;

var t = go.transform.parent;
while (t != null && comp == null)
{
comp = t.gameObject.GetComponent<T>();
t = t.parent;
}
return comp;
}
}