/Users/north/Desktop/截屏2022-03-22 下午4.08.47.png
代码1DestroyOffScreen2脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOffScreen2 : MonoBehaviour
{
//移出屏幕外的距离,用于判断何时销毁对象
public float offest = 16f;
//用于标记是否在屏幕外
private bool offscreen;
//当前屏幕宽度
private float offscreenX = 0;
//将要销毁的克隆体游戏对象
private Rigidbody2D body2d;
private void Awake()
{
//获取准备销毁克隆体的游戏对象
body2d = GetComponent<Rigidbody2D>();
}
//创建消除克隆体的方法
public void OnOutOfBounds(int span)
{
offscreen = false;
//销毁游戏对象
<span class="kindle-cn-color">gameObjectUtil</span>.Destroy(gameObject);
}
// Start is called before the first frame update
void Start()
{
//移除屏幕的坐标位置值——屏幕实际宽度/2/摄像机缩放比例+移除屏幕的所需偏移值
offscreenX = (Screen.width / PixelPerfectCamera.pixelToUnit) / 2 + offest;
}
// Update is called once per frame
void Update()
{
//x轴实时坐标信息
var posX = transform.position.x;
//存储滚动方向
var dirX = body2d.velocity.x;
if (Mathf.Abs(posX) > offscreenX)
{
//大于判断则表示出屏幕
offscreen = true;
}
else
{
//小于判断则表示在屏幕内
offscreen = false;
}
if (offscreen)
{
//删除克隆体游戏对象
OnOutOfBounds();
}
}
}
代码2gameObjectUtil脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gameObjectUtil
{
//初始化静态方法
public static GameObject Instantiate
(GameObject prefab,Vector3 pos)
{
//实例变量
GameObject instance = null;
//初始化对象位置
instance = GameObject.Instantiate(prefab);
//初始化对象位置
instance.transform.position = pos;
//返回游戏对象实例
return instance;
}
public static void Destroy(GameObject gameobject)
{
//游戏对象的销毁
GameObject.Destroy(gameobject);
}
}
目标是:unity3d中将升级销毁游戏对象的接口