Skip to content

Commit a15ab7b

Browse files
authored
Copying my Dropbox collection of Utility Scripts
1 parent 39ecb91 commit a15ab7b

29 files changed

+1723
-12
lines changed

ArrayLayout.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
[System.Serializable]
5+
public class ArrayLayout {
6+
7+
[System.Serializable]
8+
public struct rowData{
9+
public GameObject[] enemies;
10+
}
11+
12+
public rowData[] waves = new rowData[7]; //Grid of 7x7
13+
}

Attractor.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Attractor : MonoBehaviour
6+
{
7+
public string tagToAttract;
8+
public float attractForce, suckTime, suckOG, delay;
9+
public bool suckOn;
10+
MeshRenderer mr;
11+
12+
13+
private void Start()
14+
{
15+
mr = GetComponent<MeshRenderer>();
16+
17+
StartCoroutine(Suck());
18+
}
19+
20+
private void Update()
21+
{
22+
//reset it to grey if it's not actively sucking
23+
if (!suckOn)
24+
{
25+
if (mr.material.color != Color.grey)
26+
{
27+
mr.material.color = Color.grey;
28+
}
29+
}
30+
}
31+
public void Attract(float force)
32+
{
33+
foreach(GameObject go in GameObject.FindGameObjectsWithTag(tagToAttract))
34+
{
35+
go.GetComponent<Rigidbody>().velocity = force*(transform.position - go.transform.position).normalized;
36+
}
37+
mr.material.color = Color.green;
38+
39+
}
40+
public void Attract()
41+
{
42+
Attract(attractForce);
43+
44+
}
45+
46+
public IEnumerator Suck()
47+
{
48+
while (suckTime > 0)
49+
{
50+
suckOn = true;
51+
Attract(attractForce);
52+
suckTime -= Time.deltaTime;
53+
yield return null;
54+
}
55+
suckOn = false;
56+
mr.material.color = Color.grey;
57+
58+
yield return new WaitForSeconds(delay);
59+
suckTime = suckOG;
60+
StartCoroutine(Suck());
61+
}
62+
}

AttractorBasic.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Attractor : MonoBehaviour
6+
{
7+
public string tagToAttract;
8+
public float attractForce;
9+
10+
// Update is called once per frame
11+
void Update()
12+
{
13+
if (Input.GetKey(KeyCode.U))
14+
{
15+
Attract(attractForce);
16+
}
17+
}
18+
19+
public void Attract(float force)
20+
{
21+
foreach(GameObject go in GameObject.FindGameObjectsWithTag(tagToAttract))
22+
{
23+
go.GetComponent<Rigidbody>().velocity = force*(transform.position - go.transform.position);
24+
}
25+
}
26+
}

AutoSave.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="/search?Search=%23if&Mode=like">#if</a> UNITY_EDITOR using UnityEngine; using UnityEditor; [InitializeOnLoad] public class AutosaveOnRun: ScriptableObject { static AutosaveOnRun() { EditorApplication.playmodeStateChanged = () => { if(EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying) { Debug.Log("Auto-Saving scene before entering Play mode: " + EditorApplication.currentScene); EditorApplication.SaveScene(); EditorApplication.SaveAssets(); } }; } } #endif

ButtonPressShrink.cs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.EventSystems;
5+
using UnityEngine.UI;
6+
7+
public class ButtonPressShrink : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
8+
public Vector3 shrinkSize;
9+
private Vector3 originalSize;
10+
public AudioSource audSource;
11+
public AudioClip[] audClips;
12+
Image img;
13+
public Color pointerDownColor;
14+
Color originalColor;
15+
16+
// Use this for initialization
17+
void Start () {
18+
originalSize = transform.localScale;
19+
img = GetComponent<Image>();
20+
originalColor = img.color;
21+
if (GetComponent<AudioSource>() != null)
22+
{
23+
audSource = GetComponent<AudioSource>();
24+
}else
25+
{
26+
audSource = gameObject.AddComponent<AudioSource>() as AudioSource; //add an audiosource if there isn't one on there already
27+
}
28+
audSource.playOnAwake = false; //otherwise it'll play whatever sound is in there
29+
}
30+
31+
// Update is called once per frame
32+
void Update () {
33+
34+
}
35+
public void OnPointerDown(PointerEventData data)
36+
{
37+
transform.localScale = shrinkSize;
38+
audSource.clip = audClips[0];//the click in sfx
39+
audSource.Play();
40+
img.color = pointerDownColor;
41+
42+
}
43+
44+
public void OnPointerUp(PointerEventData data)
45+
{
46+
transform.localScale = originalSize;
47+
audSource.clip = audClips[1];
48+
audSource.Play();
49+
img.color = originalColor;
50+
51+
}
52+
public void ShrinkMe()
53+
{
54+
}
55+
56+
public void ResetMySize()
57+
{
58+
}
59+
}

CamAngleEditor.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections;
2+
using UnityEngine;
3+
using UnityEditor;
4+
5+
[CustomEditor(typeof(CameraAngleSwitch))]
6+
public class CamAngleEditor : Editor {
7+
8+
public override void OnInspectorGUI()
9+
{
10+
DrawDefaultInspector();
11+
12+
CameraAngleSwitch script = (CameraAngleSwitch)target;
13+
if(GUILayout.Button("Set Coordinates"))
14+
{
15+
script.AddCoordinates();
16+
}
17+
if(GUILayout.Button("Delete Last"))
18+
{
19+
script.DeleteLastCoordinates();
20+
}
21+
}
22+
}

CameraAngleSwitch.cs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class CameraAngleSwitch : MonoBehaviour {
6+
public string bName;
7+
8+
public List<Vector3> rotations = new List<Vector3>(1);
9+
public List<Vector3> positions = new List<Vector3>(1);
10+
11+
public bool instantSwitch = false;
12+
public float lerpSpeed;
13+
int arrIndex = 0;
14+
Coroutine inst;
15+
// Use this for initialization
16+
void Start () {
17+
if(rotations[0] == Vector3.zero && positions[0] == Vector3.zero)
18+
{
19+
rotations[0] = transform.rotation.eulerAngles;
20+
positions[0] = transform.position;
21+
}
22+
if (rotations.Count != positions.Count)
23+
{
24+
print("ROTATIONS AND POSITIONS ARRAYS NOT EQUAL");
25+
}
26+
}
27+
28+
// Update is called once per frame
29+
void Update () {
30+
if (Input.GetButtonDown(bName))
31+
{
32+
if(arrIndex< rotations.Count-1)
33+
{
34+
arrIndex++;
35+
}
36+
else
37+
{
38+
arrIndex = 0;
39+
}
40+
41+
if (!instantSwitch)
42+
{
43+
if (inst != null)
44+
{
45+
StopCoroutine(inst); //stop any previous movement;
46+
}
47+
inst = StartCoroutine(LerpTo(positions[arrIndex], rotations[arrIndex], lerpSpeed));
48+
49+
50+
51+
}
52+
else
53+
{
54+
transform.position = positions[arrIndex];
55+
transform.eulerAngles = rotations[arrIndex];
56+
57+
}
58+
}
59+
}
60+
61+
public IEnumerator LerpTo(Vector3 pos, Vector3 rot, float time)
62+
{
63+
float elapsedTime = 0;
64+
65+
while (elapsedTime < time)
66+
{
67+
transform.position = Vector3.Lerp(transform.position, pos, elapsedTime/time);
68+
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, rot, elapsedTime / time);
69+
elapsedTime += Time.deltaTime;
70+
yield return new WaitForEndOfFrame();
71+
72+
}
73+
}
74+
75+
public void AddCoordinates()
76+
{
77+
positions.Add(transform.position);
78+
rotations.Add(transform.eulerAngles);
79+
}
80+
public void DeleteLastCoordinates()
81+
{
82+
positions.RemoveAt(positions.Count-1);
83+
rotations.RemoveAt(positions.Count-1);
84+
85+
}
86+
}

0 commit comments

Comments
 (0)