如何从 Unity 中的多个游戏对象制作游戏对象?

时间:2021-01-15 19:35:15

标签: c# android visual-studio unity3d object

我编写了一个很棒的音乐游戏。不幸的是,有一个问题。在我的游戏中有太多对象,我的智能手机无法正确加载游戏。我无处知道如何从许多对象中制作一个对象。如果有人知道怎么做,我会很高兴!

我的脚本:

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

public class line : MonoBehaviour
{
    
    //Please attach me to the line, then set  dline,tail,mcamera,road,decorates,dieeff.
    //You can add more to make this better!
    public GameObject dline, tail, mcamera, road, decorates, dieeff;

    private GameObject tempgo, tempdia, tempcr;

    public bool direction, alive, start, load, roadmaker;

    public float cameraspeed, temprm;

    public Vector3 offset, tempcrgo;

    private GameObject[] dia, cr;

    List<GameObject> go = new List<GameObject>();

    void Start()
    {
        start = false;
        load = true;
        alive = true;
        //find diamonds and crowns
        dia = GameObject.FindGameObjectsWithTag("dia");
        cr = GameObject.FindGameObjectsWithTag("crown");
        //
    }

    void Update()
    {
        if (load == true)
        {
            //save the initial distance between camera and line
            offset = mcamera.transform.position - dline.transform.position;
            cameraspeed = 0.03f;
            load = false;
            //
        }
        //restart
        if (Input.GetKeyDown(KeyCode.R) == true)
        {
            SceneManager.LoadScene("main");
        }
        //
        if (start == false && alive == true)
        {//start game
            if (Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
            {
                start = true;
                dline.GetComponent<AudioSource>().enabled = true;
            }

        }//

        if (start == true)
        {//turn
            if (Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
            {
                if (direction == true)
                {
                    direction = false;
                }
                else
                {
                    direction = true;
                }
            }
        }//
         //build road/wall more efficiently
        if (roadmaker)
        {
            temprm += 1;
            //why there is temprm?to reduce the amount of roads
            //why build one when click?to ensure there is road/wall when line turns(or it will look weird)
            if (temprm % 6 == 1 || Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
            {
                GameObject.Instantiate(road, dline.transform.position + new Vector3(3, 0, -3), dline.transform.rotation);
                GameObject.Instantiate(road, dline.transform.position + new Vector3(-3, 0, 3), dline.transform.rotation);
            }// // //
        }
        else
        {//when you play ...I dont put the part in Update but if(roadmaker) because these actions will create new gameobject and interfere the copying of road
            GameObject.Instantiate(tail, dline.transform.position, dline.transform.rotation);
            if (Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
            {
                if (direction == true)
                {
                    go.Add(GameObject.Instantiate(decorates, dline.transform.position + new Vector3(8, -9, -5), dline.transform.rotation));
                }
                else
                {
                    go.Add(GameObject.Instantiate(decorates, dline.transform.position + new Vector3(-5, -9, 8), dline.transform.rotation));
                }
            }
        }//
    }

    void FixedUpdate()
    {//smooth camera
        mcamera.transform.position = Vector3.Lerp(mcamera.transform.position, offset + dline.transform.position, cameraspeed);
        //
        //some 'animations' .I didnt use animation component because I dont know how it works in Unity :(
        foreach (GameObject tempgo in go)
        {
            tempgo.transform.position += new Vector3(0, 0.2f, 0);
        }

        foreach (GameObject tempdia in dia)
        {
            tempdia.transform.localEulerAngles += new Vector3(0, 2, 0);
            if (Mathf.Abs(dline.transform.position.x - tempdia.transform.position.x) < 1 && Mathf.Abs(dline.transform.position.z - tempdia.transform.position.z) < 1 && tempdia.transform.localScale.z > 0)
            {
                tempdia.transform.localScale -= new Vector3(0.3f, 0.3f, 0.3f);
            }
        }
        foreach (GameObject tempcr in cr)
        {
            tempcr.transform.localEulerAngles += new Vector3(0, 2, 0);
            if (Mathf.Abs(dline.transform.position.x - tempcr.transform.position.x) < 2 && Mathf.Abs(dline.transform.position.z - tempcr.transform.position.z) < 2 && tempcr.transform.localScale.z > 0)
            {
                tempcr.transform.localScale -= new Vector3(0.26f, 0.26f, 0.26f);
                tempcrgo = tempcr.transform.position;
            }
            if (tempcr.transform.localScale.z <= 0)
            {
                tempcrgo += new Vector3(Random.Range(-2f, 2f), Random.Range(-1f, 2f), Random.Range(-2f, 2f));
                tempcr.GetComponent<Light>().enabled = true;
                tempcr.transform.position = Vector3.Lerp(tempcr.transform.position, tempcrgo, 0.02f);

            }
        }
        //
        if (start == true && alive == true)
        {//how the line move
            if (direction == true)
            {
                dline.transform.position += new Vector3(0.3f, 0, 0);
            }
            else
            {
                dline.transform.position += new Vector3(0, 0, 0.3f);
            }
        }//
    }

    void OnCollisionEnter(Collision x)
    {//when die...
        if (x.collider.tag == "wall")
        {
            alive = false;
            mcamera.GetComponent<AudioSource>().enabled = true;
            dieeff.SetActive(true);
        }// 
         //when complete the level...
        if (x.collider.tag == "Finish")
        {
            offset = offset + offset + offset + offset;
            cameraspeed = 0.01f;
        }//
    }
}

`

视频链接:

https://drive.google.com/file/d/1dqIqu6tx5thNbCJ1jNndXEJY_a9JJGGe/view?usp=sharing

1 个答案:

答案 0 :(得分:0)

你没有给我们任何关于你的游戏的信息。有点像钢琴砖?如果是这样,您应该销毁在对象超出相机范围时实例化的对象。你也做一切更新。小心点!它适用于每一帧而不是每一秒。对于每秒 30 个更新呼叫的标准移动设备。另外,如果你想生成很多对象,你应该使用特殊的算法,比如 Object Pooling In Unity

您还可以在更新时设置每个变量。真的有必要吗?我不认为,但你从来没有给我们任何关于你的游戏的线索。祝你好运!

相关问题