69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
|
|
public class Utils {
|
|
public static Rect GetCameraRect2D(Camera cam){
|
|
float pixWidth = (cam.orthographicSize * Camera.main.aspect) * 2;
|
|
float pixHeight = cam.orthographicSize * 2;
|
|
|
|
return new Rect(
|
|
Camera.main.transform.localPosition.x - (pixWidth * 0.5f),
|
|
Camera.main.transform.localPosition.y + (pixHeight * 0.5f),
|
|
pixWidth, pixHeight);
|
|
}
|
|
|
|
public static Rect RectFromBounds(Bounds b){
|
|
return new Rect (b.center.x - b.size.x * 0.5f, b.center.y + b.size.y * 0.5f, b.size.x, b.size.y);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ParallaxBackground : MonoBehaviour {
|
|
public float scrollSpeed = 0.5f;
|
|
public bool KeepTextureAspect = true;
|
|
public Camera cam;
|
|
public float Scale = 0.01f;
|
|
public float LerpSpeed = 0.5f;
|
|
// Use this for initialization
|
|
void Start () {
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
|
|
Vector2 offset = gameObject.renderer.sharedMaterial.mainTextureOffset;
|
|
Vector3 mousePos = Input.mousePosition;
|
|
Vector2 screenCenter = new Vector2 (Screen.width / 2, Screen.height / 2);
|
|
Vector2 mouseDir = new Vector2 (mousePos.x, mousePos.y) - screenCenter;
|
|
Vector2 nextOffset = mouseDir * -Scale;
|
|
|
|
gameObject.renderer.sharedMaterial.mainTextureOffset = Vector2.Lerp (offset, nextOffset, LerpSpeed * Time.deltaTime);
|
|
//offset.x += tcScrollSpeed * Time.deltaTime;
|
|
//gameObject.renderer.sharedMaterial.mainTextureOffset =offset;
|
|
|
|
|
|
/*float textureAspect = gameObject.renderer.sharedMaterial.mainTexture.width / gameObject.renderer.sharedMaterial.mainTexture.height;
|
|
float textureScale = cam.aspect / textureAspect;
|
|
var camRect = Utils.GetCameraRect2D (cam);
|
|
float tcScrollSpeed = textureScale * scrollSpeed;
|
|
|
|
if (Application.isPlaying) {
|
|
Vector2 offset = gameObject.renderer.sharedMaterial.mainTextureOffset;
|
|
offset.x += tcScrollSpeed * Time.deltaTime;
|
|
gameObject.renderer.sharedMaterial.mainTextureOffset =offset;
|
|
}
|
|
|
|
|
|
if (KeepTextureAspect) {
|
|
|
|
gameObject.renderer.sharedMaterial.mainTextureScale = new Vector2(textureScale, 1);
|
|
}
|
|
|
|
transform.localScale = new Vector3 (camRect.width, camRect.height, 1);*/
|
|
}
|
|
}
|