1、unity自带触发事件
unity的每一个Collider对象都有类似OnMouseDown、OnMouseOver等事件、此事件是存在于MonoBehaviour脚本里的,而MonoBehaviour是每个脚本的基类,根据手册可知有这些事件(鼠标在GUIElement(GUI元素)或Collider(碰撞体)上点击时调用OnMouseDown),是触发器也行!
2、销毁对象
为了不占用系统资源或者为了避免造成内存泄漏,所以当我们销毁一个对象的时候,我们要对此对象引用的东西进行处理!
对此对象脚本引入的脚本置为null,对此对象脚本引入的对象进行Destory,然后再将自己销毁!
3、震屏效果
新建一个脚本CameraShake.cs
using UnityEngine; using System.Collections; public class CameraShake : MonoBehaviour { public static bool startShake = false; public static float seconds = 0.0f; public static bool started = false; public static float quake = 0.2f; private Vector3 camPOS; public bool is2D; void Start() { camPOS = transform.position; } void LateUpdate() { if (startShake) { transform.position = Random.insideUnitSphere * quake; if (is2D) transform.position = new Vector3(transform.position.x, transform.position.y, camPOS.z); } if (started) { StartCoroutine(WaitForSecond(seconds)); started = false; } } public static void shakeFor(float a, float b) { seconds = a; started = true; quake = b; } IEnumerator WaitForSecond(float a) { camPOS = transform.position; startShake = true; yield return new WaitForSeconds(a); startShake = false; transform.position = camPOS; } }
此脚本挂在摄像机上,然后在需要的地方调用即可:CameraShake.shakeFor(0.5f,0.1f)
时间: 2024-10-13 22:53:36