C# 无法在Unity中实例化行为
我是Unity 3D的新手(非常新,事实上不到8小时) 事实证明,我对统一的陌生感给我带来了一个相当奇怪的问题。考虑以下两个行为: 行为C# 无法在Unity中实例化行为,c#,unity3d,C#,Unity3d,我是Unity 3D的新手(非常新,事实上不到8小时) 事实证明,我对统一的陌生感给我带来了一个相当奇怪的问题。考虑以下两个行为: 行为CamCaptureDialogBehavior: using UnityEngine; using System.Collections; public class CamCaptureDialogBehavior : MonoBehaviour { // 200x300 px window will apear in the center
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 ()
{
}
}
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;
}
}
行为:PictureButtonBehavior
:
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 ()
{
}
}
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
表示的对话框
您可以实例化附加了脚本的预置
GameObject g = Instantiate(prefab) as GameObject;
或者你可以将它们添加到已经存在的游戏对象中
gameObject.AddComponent<ScriptName>();
gameObject.AddComponent();
接下来你会问什么是预制件。这是一件非常简单的事情,但在统一中非常强大。关于如何制作一个
您可以通过选择资源>创建预置来创建预置,然后
将对象从场景拖动到“空”预设资源上
出现。只需将预制资源从项目视图拖动到
然后,“场景视图”将创建预置的实例
如果它源于单一行为,不要使用新的。使用initiate->我尝试使用initiate,但要使用initiate,首先需要有一个initiated值。它仍然返回空值。您可以使用资源。如果没有要克隆的实例,请加载:)我无法使其正常工作(我太新了:P),但感谢您为我指明了正确的资源。我已经解决了这个问题,但是动态创建寡妇。