유니티 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에서 클래스를 인스턴스화하는 데 필요한 확실한 방법이 있습니까?
또는 CamCaptureDialogBehavior
을 PictureButtonBehavior
에 어떻게 인스턴스화하고 CamCaptureDialogBehavior
으로 표시되는 대화 상자를 표시 할 수 있습니까?
Monobehaviour에서 파생되는 경우 new를 사용하지 마십시오. intiantiate 사용 -> http://docs.unity3d.com/ScriptReference/Object.Instantiate.html – OMGtechy
나는 initiate를 사용하려했지만 initiate를 사용하기 위해 처음부터 초기화 된 값이 필요합니다. 그것은 여전히 나 null을 반환합니다. – Abhinav
복제 할 인스턴스가 없다면 Resources.Load를 사용할 수 있습니다. – OMGtechy