简体   繁体   English

如何向现有编队添加/销毁新对象?

[英]How can I add/destroy new objects to existing formation?

In the manager: 在经理中:

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

public class FormationsManager : MonoBehaviour
{
    public Transform squadMemeberPrefab;
    public int numberOfSquadMembers = 20;
    public int columns = 4;
    public int gaps = 10;
    public Formations formations;

    private int numofmembers;

    // Use this for initialization
    void Start()
    {
        numofmembers = numberOfSquadMembers;
        formations.Init(numberOfSquadMembers, columns, gaps);
        GenerateSquad();
    }

    // Update is called once per frame
    void Update()
    {
        if (numofmembers != numberOfSquadMembers)
        {
            GenerateSquad();
        }
    }

    private void GenerateSquad()
    {
        Transform go = squadMemeberPrefab;

        for (int i = 0; i < formations.newpositions.Count; i++)
        {
            go = Instantiate(squadMemeberPrefab);
            go.position = formations.newpositions[i];
            go.tag = "Squad Member";
            go.transform.parent = gameObject.transform;
        }
    }
}

And the Formations script: 和队形脚本:

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

public class Formations : MonoBehaviour
{
    public List<Vector3> newpositions;

    private int numberOfSquadMembers;
    private int columns;
    private int gaps;
    private List<Quaternion> quaternions;

    private Vector3 FormationSquarePositionCalculation(int index)
    {
        float posX = (index % columns) * gaps;
        float posY = (index / columns) * gaps;
        return new Vector3(posX, posY);
    }

    private void FormationSquare()
    {
        newpositions = new List<Vector3>();
        quaternions = new List<Quaternion>();

        for (int i = 0; i < numberOfSquadMembers; i++)
        { 
            Vector3 pos = FormationSquarePositionCalculation(i);
            Vector3 position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
            newpositions.Add(position);
        }
    }

    public void Init(int numberOfSquadMembers, int columns, int gaps)
    {
        this.numberOfSquadMembers = numberOfSquadMembers;
        this.columns = columns;
        this.gaps = gaps;
        FormationSquare();
    }
}

What I want to do is in the FormationsManager in the Update not only just calling GenerateSquad but to add the new once to the last/next position of the existing already formation. 我想做的是在Updates的FormationsManager中,不仅调用GenerateSquad,而且还要将新的一次添加到现有已经形成的队形的最后/下一个位置。

void Update()
        {
            if (numofmembers != numberOfSquadMembers)
            {
                GenerateSquad();
            }
        }

If the value of numberOfSquadMembers is 20 first time and then I changed it to 21 add new object to the end of the formation and same if I change the value of numberOfSquadMembers for example from 20 to 19 or from 21 to 5 destroy the amount of objects from the end and keep the formation shape. 如果numberOfSquadMembers的值是第一次20,然后我将其更改为21,则将新对象添加到编队的末尾;如果将numberOfSquadMembers的值从20更改为19,或者从21更改为5,则销毁对象的数量。从头到尾保持地层形状。

编队

The soldiers the last line is on the right side. 士兵们的最后一行在右侧。 So if I change the value to add more then add it to the right and if I change to less destroy from the right side. 因此,如果我更改值以增加更多,则将其添加到右侧,如果更改为从右侧减少更少的销毁。 The most left line of soldiers is the first. 最左边的士兵是第一行。

It is possible if you keep GameObject instances inside FormationsManager class, and then reuse them in GenerateSquad method. 可以将GameObject实例保留在FormationsManager类中,然后在GenerateSquad方法中重用它们。

In FormationsManager class, add and modify code as follows. 在FormationsManager类中,添加和修改代码,如下所示。

public GameObject squadMemeberPrefab;
List<GameObject> SquadMembers = new List<GameObject>();

void Update()
{
    if (numofmembers != numberOfSquadMembers)
    {
        numofmembers = numberOfSquadMembers;
        formations.Init(numberOfSquadMembers, columns, gaps);
        GenerateSquad();
    }
}

private void GenerateSquad()
{
    Transform go = squadMemeberPrefab;
    List<GameObject> newSquadMembers = new List<GameObject>();

    int i = 0;
    for (; i < formations.newpositions.Count; i++)
    {
        if (i < SquadMembers.Count)
            go = SquadMembers[i];
        else
        {
            go = Instantiate(squadMemeberPrefab);
            newSquadMembers.Add(go);
        }
        go.position = formations.newpositions[i];
        go.tag = "Squad Member";
        go.transform.parent = gameObject.transform;
    }

    for (; i < SquadMembers.Count; i++)
        Destroy(SquadMembers[i]);

    SquadMembers = newSquadMembers;
}

However, I recommend you to consider GameObject Pool (Object Pool), which can thoroughly resolve such object recycle problem. 但是,我建议您考虑使用GameObject Pool(对象池),它可以彻底解决此类对象回收问题。 For this purpose, you can use ClientScene.RegisterSpawnHandler. 为此,可以使用ClientScene.RegisterSpawnHandler。 Go to this Unity Documentation page and search text "GameObject pool". 转到此Unity文档页面并搜索文本“ GameObject pool”。 You can see an example code there. 您可以在此处看到示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在创建新对象之前销毁所有childs对象? - How can i destroy all the childs objects before creating new? 当按F更改格式时,如何防止添加越来越多的新对象? - How can I prevent from adding more and more new objects when pressing F to change formation? 如何销毁游戏对象的材质并使用编辑器脚本向其中添加新材质? - How can I destroy a material of a gameobject and add a new material to it using editor script? 如何将新的现有属性添加到我的控件中? - How can I add new existing property to my control? 如何一次销毁多个游戏对象? - How can I destroy multiple game objects at once? 如何在C#中销毁COM对象? - How can I destroy COM objects in C#? 如何使用滑块创建和销毁对象? - How can I create and destroy objects using a slider? C# 如何使用用户输入将对象添加到现有对象列表中 - C# How can I add objects to existing list of objects with user input 如何将新项目添加到现有项目并从新项目创建 dll 文件? - How can i add a new project to my existing already project and create a dll file from the new project? 如何将新对象添加到现有实体,将数据放入数据中并让EF将其添加到数据库中? - How can I add a new object to an existing Entity, PUT the data and have EF add it to my database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM