【问题标题】:Unity make instantiated object toggle appear or disappearUnity 使实例化对象切换出现或消失
【发布时间】:2016-06-21 01:56:15
【问题描述】:

所以我有一个简单的项目,我用多个方块制作了一个游戏板,它有一个可以打开或关闭的网格。

不幸的是,它不能那样工作。这是我的董事会经理代码:

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

namespace BoardHandle {
    [RequireComponent(typeof(SpriteRenderer))]
    public class BoardManager : MonoBehaviour { 

        //2d list that represents the board
        List<List<GameObject>> board = new List<List<GameObject>> ();

        //Gamebjects import
        public GameObject GrassTile;
        public GameObject Grid;

        //UI buttons import
        public Toggle GridToggle;

        //Miscellanious variables that could be edited in inspector
        public int rows;
        public int cols;
        public float tileWidth;
        public float tileHeight;


        void Start() {

            GrassTile.transform.localScale = new Vector3 (tileWidth, tileHeight, 0f);
            Grid.transform.localScale = new Vector3 (tileWidth, tileHeight, 0f);

            //Adds all of the stuff to the game board
            for (int x = 0; x < cols; x++) {
                board.Add (new List<GameObject> ());
                for (int y = 0; y < rows; y++) {
                    board [x].Add (GrassTile);
                }
            }


            //Makes board bits all go to the screen
            for (int x = 0; x < board.Count; x++) {
                for (int y = 0; y < board[x].Count; y++) {

                    //Makes the grid. And it stays there currently until the game ends.
                    Instantiate (Grid, 
                                 new Vector3 (x * tileWidth, y * tileHeight, 0),
                                 Quaternion.identity); 

                    //This is the actual board, filled with grass tiles. I wnat to keep this.
                    Instantiate (board [x] [y], 
                                 new Vector3 (x * tileWidth, y * tileHeight, 0), 
                                 Quaternion.identity);
                }
            }
        }


        void Update() {

        }


    }
}

我的游戏编程经验来自 Pygame。在 Pygame 中,背景必须不断地在自身上重新生成,所以如果我想让网格消失,我需要做的就是每帧停止一次 blitting,然后一帧之后它就消失了。在统一中,由于对象可以移动而无需在代码中重新生成背景,因此网格仅在一次实例化后就保持不变。我只是想知道一种方法,当 GridToggle.isOn 为真时,网格被实例化并将一直保留在那里,直到 GridToggle.isOn 为假,当网格将不再存在时,直到它再次打开。谢谢!

【问题讨论】:

    标签: c# unity3d toggle instantiation


    【解决方案1】:

    在 Unity 中,可以停用游戏对象并禁用组件。

    如果一个游戏对象被停用,它被认为是“不存在”。如果您只想禁用渲染,您也可以禁用图块的渲染器组件。听起来第一个选项就是您想要的。

    停用游戏对象也会停用其子游戏对象。要禁用板及其内容,我建议将实例化对象作为板的子级。您可以通过 GameObject 的变换来做到这一点:

    instantiatedGameObject.transform.parent = this.transform;
    

    由于您使用的是 Toggle,您可以为其添加事件侦听器:

    GridToggle.onValueChanged.AddListener((value) => this.SetActive(value));
    

    【讨论】:

    • 我会试试这个。谢谢!
    • 酷。但我想知道,如何从另一个脚本远程禁用渲染器。网格由单独的脚本管理,它是一个预制件。
    • 所以你想用另一个脚本来控制它而不是 Toggle?如果您使用 BoardHandle 引用了 GameObject,您可以简单地从其他脚本中停用它。
    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2016-01-14
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多