c# - 无法在 Unity 中实例化行为

标签 c# unity3d

我是 Unity 3D 的新手(非常新,实际上不到 8 小时)。

事实证明,我对 Unity 的陌生让我遇到了一个相当奇怪的问题。考虑以下两种行为:

行为 CamCaptureDialogBehavior:

using UnityEngine;
using System.Collections;

public class CamCaptureDialogBehavior : MonoBehaviour
{
        // 200x300 px window will apear in the center of the screen.
        private Rect windowRect = new Rect ((Screen.width - 200) / 2, (Screen.height - 300) / 2, 200, 300);
        // Only show it if needed.
        private bool show = false;

        public CamCaptureDialogBehavior ()
        {
        }
        // Use this for initialization
        void Start ()
        {

        }

        void OnGUI ()
        {
                if (show)
                        windowRect = GUI.Window (0, windowRect, DialogWindow, "Game Over");
        }

        void DialogWindow (int windowID)
        {
                float y = 20;
                GUI.Label (new Rect (5, y, windowRect.width, 20), "Title goes here");

                if (GUI.Button (new Rect (5, y, windowRect.width - 10, 20), "Ok")) {
                        Application.LoadLevel (0);
                        show = false;
                }
        }

        // To open the dialogue from outside of the script.
        public void Open ()
        {
                show = true;
        }
        // Update is called once per frame
        void Update ()
        {

        }
}

行为:PictureButtonBehavior:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class PictureButtonBehavior : MonoBehaviour
{
        private bool displayedGUI = false;
        private bool ShowThisGUI = false;

        void Start ()
        {
        }

        void Update ()
        {
                if (displayedGUI == true) {
                        Debug.Log (string.Format ("displayedGUI = {0}\r\n", displayedGUI));
                        displayedGUI = false;
                        ShowThisGUI = false;
                }
        }

        void OnGUI ()
        {
                if (ShowThisGUI) {
                        Debug.Log (string.Format ("ShowThisGUI = {0}\r\n", ShowThisGUI));
                        displayedGUI = true;
                        ShowThisGUI = false;
                        CamCaptureDialogBehavior ccdb = new CamCaptureDialogBehavior ();
                        if (ccdb != null) {
                                ccdb.enabled = true;
                                ccdb.Open ();
                        }
                }
        }

        public void OnClick ()
        {
                ShowThisGUI = true;
        }
}

CamCaptureDialogBehavior ccdb = new CamCaptureDialogBehavior ();处,ccdb总是null

在Unity/Mono中有没有一种独特的方法来实例化类?

或者,如何在 PictureButtonBehavior 中实例化 CamCaptureDialogBehavior 并能够显示由 CamCaptureDialogBehavior 表示的对话框。

最佳答案

您不能在 MonoBehaviours 上调用 new

您可以实例化附加了脚本的预制件。

GameObject g = Instantiate(prefab) as GameObject;

或者您可以将它们添加到现有的游戏对象中。

gameObject.AddComponent<ScriptName>();

接下来你会问什么是预制件。它非常简单,但在 Unity 中非常强大。 Short tutorial关于如何制作。

You can create a prefab by selecting Asset > Create Prefab and then dragging an object from the scene onto the “empty” prefab asset that appears. Simply dragging the prefab asset from the project view to the scene view will then create instances of the prefab.

关于c# - 无法在 Unity 中实例化行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390697/

相关文章:

c# - WCF 服务和自定义 CA

c# - AudioQueueEnqueueBufferWithParameters 是否在 Monotouch 中实现?

c# - 如何获取 Web 上文件夹的目录列表?

c# - 向 tableLayoutPanel 添加控件

c# - 如何过滤 AutoCompleteBox 中第一个匹配项的项目列表?

c# - 钩子(Hook)在没有委托(delegate)的情况下运行(反射)

c# - 让 child 不受 parent 轮换的影响 Unity

c# - 如何访问 UI 文本?

c# - 有没有办法使用 Vector3 制作 switch/case?

android - 如何在新进程中打开 Android Fragment?