Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

Commit c7bf30f

Browse files
authored
Upload of code snippets
1 parent e49fbbc commit c7bf30f

11 files changed

+770
-0
lines changed

CCamera.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using UnityEngine;
3+
4+
[RequireComponent(typeof(Camera))]
5+
public class CCamera : MonoBehaviour {
6+
7+
[Serializable]
8+
public abstract class Module : MonoBehaviour
9+
{
10+
protected Transform cameraTarget;
11+
public bool smoothCamera;
12+
public float smoothMoveSpeed;
13+
public float smoothRotationSpeed;
14+
15+
public void SetCameraTarget(Transform newTarget)
16+
{
17+
cameraTarget = newTarget;
18+
}
19+
20+
public virtual void UpdateCamera(out Vector3 cameraPos, out Quaternion cameraRot)
21+
{
22+
if (!cameraTarget)
23+
{
24+
cameraPos = Vector3.zero;
25+
cameraRot = Quaternion.identity;
26+
return;
27+
}
28+
cameraPos = cameraTarget.position;
29+
cameraRot = cameraTarget.rotation;
30+
}
31+
32+
public virtual void Initialize(Transform cameraTransform)
33+
{
34+
35+
}
36+
}
37+
38+
public Transform cameraTarget;
39+
40+
[SerializeField]
41+
public Module[] cameras;
42+
43+
private Module activeCamera;
44+
45+
private int activeCameraIndex;
46+
47+
private void Awake()
48+
{
49+
if (cameras == null
50+
|| cameras[0] == null)
51+
{
52+
Debug.LogWarning("No default camera module set up. Destroying " + gameObject + " because of this.");
53+
Destroy(gameObject);
54+
return;
55+
}
56+
57+
activeCameraIndex = 0;
58+
SwitchToCamera(activeCameraIndex);
59+
}
60+
61+
private void SwitchToCamera(int index)
62+
{
63+
cameras[index].SetCameraTarget(cameraTarget);
64+
cameras[index].Initialize(transform);
65+
activeCamera = cameras[index];
66+
}
67+
68+
private void LateUpdate()
69+
{
70+
if (!activeCamera) return;
71+
var cameraPos = transform.position;
72+
var cameraRot = transform.rotation;
73+
activeCamera.UpdateCamera(out cameraPos, out cameraRot);
74+
if (!activeCamera.smoothCamera)
75+
{
76+
transform.position = cameraPos;
77+
transform.rotation = cameraRot;
78+
}
79+
else
80+
{
81+
transform.position = Vector3.Lerp(transform.position, cameraPos, Time.deltaTime * activeCamera.smoothMoveSpeed);
82+
transform.rotation = Quaternion.Lerp(transform.rotation, cameraRot, Time.deltaTime * activeCamera.smoothRotationSpeed);
83+
}
84+
}
85+
86+
public void SwitchCamera()
87+
{
88+
if (activeCameraIndex < cameras.Length)
89+
activeCameraIndex++;
90+
else
91+
activeCameraIndex = 0;
92+
93+
SwitchToCamera(activeCameraIndex);
94+
}
95+
}
96+

CXRayView.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using UnityEngine;
4+
5+
public class CXRayView : MonoBehaviour {
6+
7+
public Material alphaMaterial;
8+
9+
public LayerMask blockLayer;
10+
public string blockTag;
11+
private Dictionary<GameObject, Material> hiddenObjects;
12+
13+
private void Awake()
14+
{
15+
hiddenObjects = new Dictionary<GameObject, Material>();
16+
}
17+
18+
private void LateUpdate()
19+
{
20+
var checkDir = PlayerController.ControlledCharacter.transform.position - transform.position;
21+
var checkDistance = checkDir.magnitude;
22+
checkDir.Normalize();
23+
24+
var blockers = Physics.RaycastAll(transform.position, checkDir, checkDistance, blockLayer);
25+
26+
if(blockers.Length > 0)
27+
{
28+
foreach (var t in blockers)
29+
{
30+
var hitObject = t.transform.gameObject;
31+
32+
if (hitObject.tag != blockTag)
33+
continue;
34+
35+
if (hiddenObjects.ContainsKey(hitObject))
36+
continue;
37+
38+
var renderer = hitObject.GetComponentInChildren<Renderer>();
39+
if (!renderer) continue;
40+
var oldMaterial = renderer.sharedMaterial;
41+
hiddenObjects.Add(hitObject, oldMaterial);
42+
renderer.material = alphaMaterial;
43+
renderer.material.mainTexture = oldMaterial.mainTexture;
44+
}
45+
}
46+
47+
var keys = new GameObject[hiddenObjects.Keys.Count];
48+
hiddenObjects.Keys.CopyTo(keys, 0);
49+
50+
foreach (var t in keys)
51+
{
52+
var bFound = blockers.Select(t1 => t1.transform.gameObject).Any(hitObject => hitObject == t);
53+
54+
if (bFound) continue;
55+
Material restoredMaterial;
56+
hiddenObjects.TryGetValue(t, out restoredMaterial);
57+
var renderer = t.GetComponentInChildren<Renderer>();
58+
renderer.material = restoredMaterial;
59+
hiddenObjects.Remove(t);
60+
}
61+
}
62+
}
63+

CameraScript.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class CCamera : MonoBehaviour {
5+
6+
CameraSpot[] cameraSpots;
7+
ParticleEmitter emitter;
8+
9+
void Awake()
10+
{
11+
int i = 1;
12+
int spotCount = GameObject.FindGameObjectsWithTag("CameraSpot").Length;
13+
14+
cameraSpots = new CameraSpot[spotCount];
15+
16+
while (GameObject.Find("CameraSpot" + i.ToString()) != null && i <= spotCount)
17+
{
18+
cameraSpots[i-1] = GameObject.Find("CameraSpot" + i.ToString()).GetComponent<CameraSpot>();
19+
i++;
20+
}
21+
22+
transform.position = cameraSpots[0].position;
23+
transform.rotation = cameraSpots[0].rotation;
24+
25+
emitter = GetComponentInChildren<ParticleEmitter>();
26+
}
27+
28+
void Start()
29+
{
30+
GetComponent<Camera>().orthographicSize *= UIScaler.uiScaleHeight;
31+
}
32+
33+
public void SwitchCamera(int spot)
34+
{
35+
transform.position = cameraSpots[spot-1].position;
36+
transform.rotation = cameraSpots[spot-1].rotation;
37+
}
38+
39+
public void EnableTransition()
40+
{
41+
emitter.emit = true;
42+
AudioManager.Manager.PlayFX(AudioManager.AudioType.BUBBLESOUND);
43+
Invoke("DisableTransition", 2.0f);
44+
}
45+
46+
void DisableTransition()
47+
{
48+
emitter.emit = false;
49+
}
50+
}

CameraSpot.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class CameraSpot : MonoBehaviour {
5+
6+
public Vector3 position { get; private set; }
7+
public Transform defaultLookAt;
8+
public Quaternion rotation { get; private set; }
9+
10+
void Awake()
11+
{
12+
position = transform.position;
13+
rotation = Quaternion.LookRotation(defaultLookAt.position - position);
14+
}
15+
}

FaceCamera.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using UnityEngine;
2+
3+
public class FaceCamera : MonoBehaviour {
4+
private Transform cameraTransform;
5+
6+
private void Start()
7+
{
8+
cameraTransform = Camera.main.transform;
9+
}
10+
11+
private void LateUpdate()
12+
{
13+
transform.LookAt(cameraTransform);
14+
}
15+
}
16+

GameCamera.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using UnityEngine;
2+
3+
public class GameCamera : MonoBehaviour
4+
{
5+
private Transform _cameraTarget;
6+
7+
public float cameraDistance;
8+
9+
private Vector3 _startDirection;
10+
11+
private void Start()
12+
{
13+
_cameraTarget = GameObject.FindGameObjectWithTag(
14+
"Player").transform;
15+
_startDirection = Quaternion.Euler(
16+
transform.eulerAngles) * Vector3.forward;
17+
}
18+
19+
private void LateUpdate()
20+
{
21+
Vector3 desiredCameraPosition;
22+
Quaternion desiredCameraRotation;
23+
24+
CalculateCameraTransform(
25+
out desiredCameraPosition,
26+
out desiredCameraRotation);
27+
var myTransform = transform;
28+
myTransform.position = desiredCameraPosition;
29+
myTransform.rotation = desiredCameraRotation;
30+
}
31+
32+
private void CalculateCameraTransform(
33+
out Vector3 cameraPosition,
34+
out Quaternion cameraRotation)
35+
{
36+
var position = _cameraTarget.position;
37+
cameraPosition = position - _startDirection * cameraDistance;
38+
cameraRotation = Quaternion.LookRotation(
39+
position - cameraPosition);
40+
}
41+
}

Gradient.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using UnityEngine;
2+
3+
[RequireComponent(typeof(Camera))]
4+
public class Gradient : MonoBehaviour
5+
{
6+
public Color lightColor;
7+
public Color darkColor;
8+
public Color darkestColor;
9+
public int gradientLayer = 7;
10+
public float stepSize = 0.05f;
11+
12+
Color topColor;
13+
Color bottomColor;
14+
Color topTargetColor;
15+
Color bottomTargetColor;
16+
17+
Mesh gradientMesh;
18+
19+
void Awake()
20+
{
21+
gradientLayer = Mathf.Clamp(gradientLayer, 0, 31);
22+
23+
GetComponent<Camera>().clearFlags = CameraClearFlags.Depth;
24+
GetComponent<Camera>().cullingMask = GetComponent<Camera>().cullingMask & ~(1 << gradientLayer);
25+
26+
Camera gradientCamera = new GameObject("Gradient Camera", typeof(Camera)).GetComponent<Camera>();
27+
gradientCamera.depth = GetComponent<Camera>().depth - 1;
28+
gradientCamera.cullingMask = 1 << gradientLayer;
29+
30+
gradientMesh = new Mesh();
31+
gradientMesh.vertices = new Vector3[4] { new Vector3(-100f, .577f, 1f), new Vector3(100f, .577f, 1f), new Vector3(-100f, -.577f, 1f), new Vector3(100f, -.577f, 1f) };
32+
gradientMesh.colors = new Color[4] { lightColor, lightColor, darkColor, darkColor };
33+
34+
topColor = lightColor;
35+
bottomColor = darkColor;
36+
topTargetColor = lightColor;
37+
bottomTargetColor = darkColor;
38+
gradientMesh.triangles = new int[6] { 0, 1, 2, 1, 3, 2 };
39+
40+
Material gradientMaterial = new Material("Shader \"Vertex Color Only\"{Subshader{BindChannels{Bind \"vertex\", vertex Bind \"color\", color}Pass{}}}");
41+
42+
GameObject BackGround = new GameObject("BackGround", typeof(MeshFilter), typeof(MeshRenderer));
43+
44+
((MeshFilter)BackGround.GetComponent(typeof(MeshFilter))).mesh = gradientMesh;
45+
BackGround.GetComponent<Renderer>().material = gradientMaterial;
46+
BackGround.layer = gradientLayer;
47+
}
48+
49+
public void DeepenGradient()
50+
{
51+
topTargetColor = bottomColor;
52+
53+
bottomTargetColor.r -= stepSize;
54+
if (bottomTargetColor.r < darkestColor.r)
55+
bottomTargetColor.r = darkestColor.r;
56+
57+
bottomTargetColor.g -= stepSize;
58+
if (bottomTargetColor.g < darkestColor.g)
59+
bottomTargetColor.g = darkestColor.g;
60+
61+
bottomTargetColor.b -= stepSize;
62+
if (bottomTargetColor.b < darkestColor.b)
63+
bottomTargetColor.b = darkestColor.b;
64+
65+
topTargetColor.a = 1.0f;
66+
bottomTargetColor.a = 1.0f;
67+
}
68+
69+
public void LightenGradient()
70+
{
71+
bottomTargetColor = darkColor;
72+
topTargetColor = lightColor;
73+
}
74+
75+
void MaintainGradient()
76+
{
77+
bool bChangedColors = false;
78+
79+
if (topColor != topTargetColor)
80+
{
81+
topColor = Color.Lerp(topColor, topTargetColor, Time.deltaTime * stepSize);
82+
bChangedColors = true;
83+
}
84+
85+
if (bottomColor != bottomTargetColor)
86+
{
87+
bottomColor = Color.Lerp(bottomColor, bottomTargetColor, Time.deltaTime * stepSize);
88+
bChangedColors = true;
89+
}
90+
91+
if (bChangedColors)
92+
{
93+
Color[] colors = new Color[4] { topColor, topColor, bottomColor, bottomColor };
94+
gradientMesh.colors = colors;
95+
}
96+
}
97+
98+
void Update()
99+
{
100+
MaintainGradient();
101+
}
102+
}

0 commit comments

Comments
 (0)