Unity通过读取文件实例化对象,并且每销毁一个对象,便生成一个新的对象

此案例就好像游戏刷副本,一幅图最多有6只怪物,每击杀一个怪物便会生成一个新的怪物.
在这里插入图片描述
很简单不用添加太多东西,三个预制体得在Resources文件中,因为脚本上写的是Resources 没有的话就创建一个.

首先呢建一个父类的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class AbsObjClass : MonoBehaviour {   //建立一个抽象类继承MonoBehaviour

    public abstract void AbsDeath();  //定义一个抽象方法
}

其次再建一个子类脚本 重写父类的抽象方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjClass : AbsObjClass   //继承父类AbsObjClass
{
    public override void AbsDeath()  //重写父类AbsDeath的方法
    {
        Destroy(gameObject);
    }
    void OnDestroy()
    {
        Debug.LogWarningFormat("Death  position is {0}", this.gameObject.transform.position);
    }
}

接下来就是CreateManager的脚本了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.IO;

class CopyInfo
{
    public string CopyName;
    public Vector3 CopyPosition;
    public Quaternion CopyRotation;

    public CopyInfo(string name,Vector3 position,Vector3 rotation)
    {
        CopyName = name;
        CopyPosition = position;
        CopyRotation = Quaternion.Euler(rotation);
    }
}
public class CreateManager : MonoBehaviour {

    private List<GameObject> _copyList;   //用来接游戏对象
    private List<CopyInfo> _copyInfoList;  //用来接读取的信息
    private string _path;  //定义路径
    //1. 读文件
    //2. 解析文件信息
    //3. 实例化
        //4. 实例化6额
        //4.1 在3s中销毁一个并且添加一个
	void Start () {
        _copyList = new List<GameObject>();
        _copyInfoList = new List<CopyInfo>();
        //streamingAssetsPath 文件名
        _path = Application.streamingAssetsPath + "/CopyInfo.txt";  //路径
        ReadFileToList(_copyInfoList, _path);    //调用下面读取信息方法
        Debug.LogWarning(_copyInfoList.Count);
        for(int i=0;i<6;i++)  //实例化6个物体
        {
            GameObject tmp = Resources.Load<GameObject>(_copyInfoList[i].CopyName);
            tmp.AddComponent<ObjClass>();
            _copyList.Add(Instantiate(tmp, _copyInfoList[i].CopyPosition, _copyInfoList[i].CopyRotation));
        }
        _copyInfoList.RemoveRange(0, 6);  //移除前6个物体的读取信息(int index,int count)
        StartCoroutine("IEMethod");
	}
    IEnumerator IEMethod()
    {
        while(_copyList.Count > 0)  //多态
        {
            yield return new WaitForSeconds(3);  //3S后开始删除一个物体
            int deleteIndex = Random.Range(0, _copyList.Count);  //随机一个物体下标
            AbsObjClass absObjClass = _copyList[deleteIndex].GetComponent<ObjClass>(); //父类等于子类对象
            absObjClass.AbsDeath(); //销毁物体   父类调用子类重写的方法
            _copyList.RemoveAt(deleteIndex);  //移除被摧毁物体的下标
            if(_copyInfoList.Count > 0)
            {
                GameObject tmp = Resources.Load<GameObject>(_copyInfoList[0].CopyName);
                tmp.AddComponent<ObjClass>();
                _copyList.Add(Instantiate(tmp, _copyInfoList[0].CopyPosition, _copyInfoList[0].CopyRotation)); //实例化一个新物体
                _copyInfoList.RemoveAt(0); //移除这个物体的读取信息
            }
        }
    }
	
	// Update is called once per frame
	void Update () {
		
	}
    private void ReadFileToList(List<CopyInfo> list,string path)
    {
        using (StreamReader reader = new StreamReader(path, Encoding.UTF8)) //读取文件
        {
            string tmpStr = string.Empty;
            while(! string.IsNullOrEmpty(tmpStr = reader.ReadLine()))
            {
                string[] tmpInfos = tmpStr.Split('_');  //定义字符串数组 用来接收读取一行的信息 并以_隔开
                list.Add(new CopyInfo(tmpInfos[0], StrToV3(tmpInfos[1]), StrToV3(tmpInfos[2])));
            }
        }
    }
    // 将string -->V3
    private Vector3 StrToV3(string str)
    {
        Vector3 tmp = new Vector3();
        string[] infos = str.Split(',');
        tmp.Set(
            System.Convert.ToSingle(infos[0]),
            System.Convert.ToSingle(infos[1]),
            System.Convert.ToSingle(infos[2])
            );
        return tmp;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43140883/article/details/83063849