简体   繁体   English

面板在NGUI中隐藏和显示

[英]panel hide and show in NGUI

I am new to NGUI and unity 3d . 我是NGUIunity 3d NGUI I have two panels in a ui root. 我在ui root中有两个面板。 its named as firstPanel and secondPanel . 它被命名为firstPanelsecondPanel secondPanel is deactivated in scene. secondPanel在场景中停用。 In firstPanel i have so many buttons and one is a play button, that is image button. firstPanel我有很多按钮,一个是play按钮,即图像按钮。 While Clicking on play button, firstPanel should get hide and secondPanel should show .I adde a new Script to play button and written code in it 在单击play按钮时, firstPanel应该隐藏secondPanel应该显示 。我添加了一个新的脚本来play按钮并在其中编写代码

void OnClick(){
    GameObject panel2  = GameObject.Find("secondPanel");
    NGUITools.SetActive(panel2,true);       
    GameObject panel1  = GameObject.Find("firstPanel");         
    NGUITools.SetActive(panel1,false);

}

But I get this Error : "NullReferenceException" In which script of ngui i have to edit and how can i do it? 但我得到这个错误"NullReferenceException"在哪个ngui脚本编辑,我该怎么办呢? please help me to solve this issue Thanks in advance. 请帮我解决这个问题在此先感谢。

If your panels are named as Panel1 and Panel2, you will not find them by using GameObject.Find("secondPanel") and GameObject.Find("firstPanel"). 如果您的面板命名为Panel1和Panel2,则使用GameObject.Find(“secondPanel”)和GameObject.Find(“firstPanel”)找不到它们。 If "Panel1" and "Panel2" is the only name in the game scene(No other Panel1 or Panel2), then you can try to use 如果“Panel1”和“Panel2”是游戏场景中唯一的名称(没有其他Panel1或Panel2),那么您可以尝试使用

void OnClick(){
  GameObject panel2  = GameObject.Find("Panel2");
  NGUITools.SetActive(panel2,true);       
  GameObject panel1  = GameObject.Find("Panel1");         
  NGUITools.SetActive(panel1,false);

}

GameObject.Find("Something") cannot find any disabled game object, so you cannot use by this way. GameObject.Find(“Something”)找不到任何禁用的游戏对象,因此你无法通过这种方式使用。 You can try to add reflection to your button code: 您可以尝试为按钮代码添加反射:

public GameObject pannel1 = null;
public GameObject pannel2 = null;

and set them to right panel in the scene editor window. 并在场景编辑器窗口中将它们设置为右侧面板。

Another way, first you need to keep both of your panels active in your scene, then add code to your button script like this: 另一种方法,首先你需要在场景中保持两个面板都处于活动状态,然后将代码添加到按钮脚本中,如下所示:

private GameObject panel1 = null;
private GameObject panel2 = null;
void Start()
{
    panel1 = GameObject.Find("Panel1");
    panel2 = GameObject.Find("Panel2");
}

void OnClick()
{
    panel2.SetActiveRecursively(true);
    panel1.SetActiveRecursively(false);
}

GameObject.Find(string name) function is not very efficient in Unity3D, so do not try to use it in Update() or every time you click your button. GameObject.Find(字符串名称)函数在Unity3D中效率不高,因此不要尝试在Update()或每次单击按钮时使用它。

I know this thread is old but if anyone else is having an issue calling panels when they are inactive you can prefab your panel and use the NGUITools.AddChild to call your panel. 我知道这个帖子已经过时了但是如果其他人在面板处于非活动状态时调用面板有问题,你可以预制面板并使用NGUITools.AddChild来调用你的面板。 It would look something like this. 它看起来像这样。

public GameObject parent;
public GameObject child;

void Spawn () {

NGUITools.AddChild (parent, child);

}

Assign your UIRoot to parent (or whatever you want to add the panel to), and assign your panel to child in the editor and you're all set! 将您的UIRoot分配给父级(或者您想要添加面板的任何内容),并在编辑器中将您的面板分配给子级,并且您已完成设置! I hope this helps someone out, this is a common problem when first working with NGUI. 我希望这可以帮助别人,这是第一次使用NGUI时的常见问题。

Happy Coding :) 快乐编码:)

你不能使用GameObject.Find()来找到一个禁用的游戏对象,它返回null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM