退出游戏时如何将添加的对撞机保留在游戏对象上?

时间:2019-10-19 18:20:03

标签: c# unity3d

即使所有预制对象中已经有碰撞器,也向所有游戏对象中添加了网格碰撞器,也可以进行预制。问题是,当我退出游戏时停止玩游戏时,并没有保留我添加的对撞机。我该如何保持对撞机?

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

public class GetComponents : MonoBehaviour
{
    public Transform parent;
    public List<GameObject> allObjects = new List<GameObject>();

    private void Start()
    {
        string path = "e:/colliders.txt";
        StreamWriter writer = new StreamWriter(path, true);

        allObjects = FindObjectsOfType<GameObject>().ToList();

        Transform[] allChildren = parent.GetComponentsInChildren<Transform>();

        foreach (Transform child in allChildren)
        {
            if (child.name != "_Level" && child.name != "_Rocks" && child.name != "_Others" && child.name != "Base")
            {
                var colliders = child.GetComponents<Collider>();

                int length = colliders.Length;

                if (length == 0)
                {
                    writer.WriteLine(string.Format("{0} - No Colliders", child.name));
                    child.gameObject.AddComponent<MeshCollider>();
                }
                else
                {
                    //composes a list of the colliders types, this will print what you want e.g. "Wall1 - BoxCollider, MeshCollider"

                    string colliderTypes = string.Empty;

                    for (int i = 0; i < length; i++)
                    {
                        colliderTypes = string.Format("{0}{1}", colliderTypes, colliders[i].GetType().Name);

                        if (i != (length - 1))
                        {
                            colliderTypes = string.Format("{0}, ", colliderTypes);
                        }
                    }
                    //writer.WriteLine(string.Format("{0} - {1}", child.name, colliderTypes));
                }
            }
        }

        writer.Close();
    }
}

0 个答案:

没有答案